Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/inet_ntoa.cc

Issue 21610003: Prepared newlib toolchain for socket implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@headers
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "nacl_io/ossocket.h"
6
7 #ifdef PROVIDES_SOCKET_API
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <iostream>
14 #include <sstream>
15 #include <string>
16
17 static inline uint8_t get_byte(const void* addr, int byte) {
18 const char* buf = static_cast<const char*>(addr);
19 return static_cast<uint8_t>(buf[byte]);
20 }
21
22 char* inet_ntoa(struct in_addr in) {
23 static char addr[INET_ADDRSTRLEN];
24 snprintf(addr, INET_ADDRSTRLEN, "%u.%u.%u.%u",
25 get_byte(&in, 0), get_byte(&in, 1),
26 get_byte(&in, 2), get_byte(&in, 3));
27 return addr;
28 }
29
30 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) {
31 if (AF_INET == af) {
32 if (size < INET_ADDRSTRLEN) {
33 errno = ENOSPC;
34 return NULL;
35 }
36 struct in_addr in;
37 memcpy(&in, src, sizeof(in));
38 char* result = inet_ntoa(in);
39 memcpy(dst, result, strlen(result) + 1);
40 return dst;
41 }
42
43 if (AF_INET6 == af) {
44 if (size < INET6_ADDRSTRLEN) {
45 errno = ENOSPC;
46 return NULL;
47 }
48 const uint8_t* tuples = static_cast<const uint8_t*>(src);
49 std::stringstream output;
50 for (int i = 0; i < 8; i++) {
51 uint16_t tuple = (tuples[2*i] << 8) + tuples[2*i+1];
52 output << std::hex << tuple;
53 if (i < 7) {
54 output << ":";
55 }
56 }
57 memcpy(dst, output.str().c_str(), output.str().size() + 1);
58 return dst;
59 }
60
61 errno = EAFNOSUPPORT;
62 return NULL;
63 }
64
65 #endif // PROVIDES_SOCKET_API
OLDNEW
« no previous file with comments | « native_client_sdk/src/build_tools/sdk_files.list ('k') | native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698