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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/libraries/nacl_io/inet_ntoa.cc
diff --git a/native_client_sdk/src/libraries/nacl_io/inet_ntoa.cc b/native_client_sdk/src/libraries/nacl_io/inet_ntoa.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e5305fe88056da74a38f0de785eec7132858f926
--- /dev/null
+++ b/native_client_sdk/src/libraries/nacl_io/inet_ntoa.cc
@@ -0,0 +1,58 @@
+#include <arpa/inet.h>
Sam Clegg 2013/08/01 20:50:53 Missing copyright.
torinmr 2013/08/01 22:04:00 Done.
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+static inline uint8_t get_byte(const void *addr, int byte) {
Sam Clegg 2013/08/01 20:50:53 We use the "char* " rather than "char *" style.
torinmr 2013/08/01 22:04:00 Done.
+ const char *buf = static_cast<const char*>(addr);
+ return static_cast<uint8_t>(buf[byte]);
+}
+
+char *inet_ntoa(struct in_addr in) {
Sam Clegg 2013/08/01 20:50:53 Are these functions missing under glibc? If not m
+ static char addr[INET_ADDRSTRLEN];
+ snprintf(addr, INET_ADDRSTRLEN, "%u.%u.%u.%u",
+ get_byte(&in, 0), get_byte(&in, 1),
+ get_byte(&in, 2), get_byte(&in, 3));
+ return addr;
+}
+
+const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) {
+ if (AF_INET == af) {
+ if (size < INET_ADDRSTRLEN) {
+ errno = ENOSPC;
+ return NULL;
+ }
+ struct in_addr in;
+ memcpy(&in, src, sizeof(in));
+ char *result = inet_ntoa(in);
+ memcpy(dst, result, strlen(result) + 1);
+ return dst;
+ }
+
+ if (AF_INET6 == af) {
+ if (size < INET6_ADDRSTRLEN) {
+ errno = ENOSPC;
+ return NULL;
+ }
+ const uint8_t *tuples = static_cast<const uint8_t*>(src);
+ std::stringstream output;
+ for (int i = 0; i < 8; i++) {
+ uint16_t tuple = (tuples[2*i] << 8) + tuples[2*i+1];
+ output << std::hex << tuple;
+ if (i < 7) {
+ output << ":";
+ }
+ }
+ memcpy(dst, output.str().c_str(), output.str().size() + 1);
+ return dst;
+ }
+
+ errno = EAFNOSUPPORT;
+ return NULL;
+}

Powered by Google App Engine
This is Rietveld 408576698