{"thread":{"id":"4235","subject":"[PATCH] adding NO_INET_NTOP and compat/inet_ntop.c for systems without inet_ntop() (old Cygwin).","startedAt":"2006-05-21T21:37:00Z","lastAt":"2006-05-21T21:37:00Z","messageCount":1,"participants":["iler.ml@gmail.com"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"20406","messageId":"0IZM00JO4XE2JI20@mxout5.netvision.net.il","threadId":"4235","inReplyTo":null,"subject":"[PATCH] adding NO_INET_NTOP and compat/inet_ntop.c for systems without inet_ntop() (old Cygwin).","fromName":"","fromEmail":"iler.ml@gmail.com","sentAt":"2006-05-21T21:37:00Z","receivedAt":"2006-05-21T21:37:00Z","isPatch":true,"sender":{"key":"iler.ml@gmail.com","avatar":null},"body":"For systems which lack inet_ntop(), this adds compat/inet_ntop.c,\nand related build constant, NO_INET_NTOP. Older Cygwin(s) lack \ninet_ntop().\n\nSigned-off-by: Yakov Lerner <iler.ml@gmail.com>\n\n---\n\n Makefile           |    3 +\n compat/inet_ntop.c |  200 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 203 insertions(+), 0 deletions(-)\n create mode 100644 compat/inet_ntop.c\n\n7cd8cb466db39e9253d77db1f07f49bce6901731\ndiff --git a/Makefile b/Makefile\nindex 8ce27a6..fffb4ab 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -412,6 +412,9 @@ else\n \tALL_CFLAGS += -Dsockaddr_storage=sockaddr_in6\n endif\n endif\n+ifdef NO_INET_NTOP\n+\tLIB_OBJS += compat/inet_ntop.o\n+endif\n \n ifdef NO_ICONV\n \tALL_CFLAGS += -DNO_ICONV\ndiff --git a/compat/inet_ntop.c b/compat/inet_ntop.c\nnew file mode 100644\nindex 0000000..ec8c1bf\n--- /dev/null\n+++ b/compat/inet_ntop.c\n@@ -0,0 +1,200 @@\n+/*\n+ * Copyright (c) 1996-1999 by Internet Software Consortium.\n+ *\n+ * Permission to use, copy, modify, and distribute this software for any\n+ * purpose with or without fee is hereby granted, provided that the above\n+ * copyright notice and this permission notice appear in all copies.\n+ *\n+ * THE SOFTWARE IS PROVIDED \"AS IS\" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\n+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\n+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\n+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\n+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\n+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\n+ * SOFTWARE.\n+ */\n+\n+#include <errno.h>\n+#include <sys/types.h>\n+#include <sys/socket.h>\n+#include <sys/socket.h>\n+#include <netinet/in.h>\n+#include <arpa/inet.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n+#ifndef NS_INADDRSZ\n+#define NS_INADDRSZ\t4\n+#endif\n+#ifndef NS_IN6ADDRSZ\n+#define NS_IN6ADDRSZ\t16\n+#endif\n+#ifndef NS_INT16SZ\n+#define NS_INT16SZ\t2\n+#endif\n+\n+/*\n+ * WARNING: Don't even consider trying to compile this on a system where\n+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.\n+ */\n+\n+/* const char *\n+ * inet_ntop4(src, dst, size)\n+ *\tformat an IPv4 address\n+ * return:\n+ *\t`dst' (as a const)\n+ * notes:\n+ *\t(1) uses no statics\n+ *\t(2) takes a u_char* not an in_addr as input\n+ * author:\n+ *\tPaul Vixie, 1996.\n+ */\n+static const char *\n+inet_ntop4(src, dst, size)\n+\tconst u_char *src;\n+\tchar *dst;\n+\tsize_t size;\n+{\n+\tstatic const char fmt[] = \"%u.%u.%u.%u\";\n+\tchar tmp[sizeof \"255.255.255.255\"];\n+\tint nprinted;\n+\n+\tnprinted = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);\n+\tif (nprinted < 0)\n+\t\treturn (NULL);\t/* we assume \"errno\" was set by \"snprintf()\" */\n+\tif ((size_t)nprinted > size) {\n+\t\terrno = ENOSPC;\n+\t\treturn (NULL);\n+\t}\n+\tstrcpy(dst, tmp);\n+\treturn (dst);\n+}\n+\n+#ifndef NO_IPV6\n+/* const char *\n+ * inet_ntop6(src, dst, size)\n+ *\tconvert IPv6 binary address into presentation (printable) format\n+ * author:\n+ *\tPaul Vixie, 1996.\n+ */\n+static const char *\n+inet_ntop6(src, dst, size)\n+\tconst u_char *src;\n+\tchar *dst;\n+\tsize_t size;\n+{\n+\t/*\n+\t * Note that int32_t and int16_t need only be \"at least\" large enough\n+\t * to contain a value of the specified size.  On some systems, like\n+\t * Crays, there is no such thing as an integer variable with 16 bits.\n+\t * Keep this in mind if you think this function should have been coded\n+\t * to use pointer overlays.  All the world's not a VAX.\n+\t */\n+\tchar tmp[sizeof \"ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255\"], *tp;\n+\tstruct { int base, len; } best, cur;\n+\tu_int words[NS_IN6ADDRSZ / NS_INT16SZ];\n+\tint i;\n+\n+\t/*\n+\t * Preprocess:\n+\t *\tCopy the input (bytewise) array into a wordwise array.\n+\t *\tFind the longest run of 0x00's in src[] for :: shorthanding.\n+\t */\n+\tmemset(words, '\\0', sizeof words);\n+\tfor (i = 0; i < NS_IN6ADDRSZ; i++)\n+\t\twords[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));\n+\tbest.base = -1;\n+\tcur.base = -1;\n+\tfor (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {\n+\t\tif (words[i] == 0) {\n+\t\t\tif (cur.base == -1)\n+\t\t\t\tcur.base = i, cur.len = 1;\n+\t\t\telse\n+\t\t\t\tcur.len++;\n+\t\t} else {\n+\t\t\tif (cur.base != -1) {\n+\t\t\t\tif (best.base == -1 || cur.len > best.len)\n+\t\t\t\t\tbest = cur;\n+\t\t\t\tcur.base = -1;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tif (cur.base != -1) {\n+\t\tif (best.base == -1 || cur.len > best.len)\n+\t\t\tbest = cur;\n+\t}\n+\tif (best.base != -1 && best.len < 2)\n+\t\tbest.base = -1;\n+\n+\t/*\n+\t * Format the result.\n+\t */\n+\ttp = tmp;\n+\tfor (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {\n+\t\t/* Are we inside the best run of 0x00's? */\n+\t\tif (best.base != -1 && i >= best.base &&\n+\t\t    i < (best.base + best.len)) {\n+\t\t\tif (i == best.base)\n+\t\t\t\t*tp++ = ':';\n+\t\t\tcontinue;\n+\t\t}\n+\t\t/* Are we following an initial run of 0x00s or any real hex? */\n+\t\tif (i != 0)\n+\t\t\t*tp++ = ':';\n+\t\t/* Is this address an encapsulated IPv4? */\n+\t\tif (i == 6 && best.base == 0 &&\n+\t\t    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {\n+\t\t\tif (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))\n+\t\t\t\treturn (NULL);\n+\t\t\ttp += strlen(tp);\n+\t\t\tbreak;\n+\t\t}\n+\t\ttp += snprintf(tp, sizeof tmp - (tp - tmp), \"%x\", words[i]);\n+\t}\n+\t/* Was it a trailing run of 0x00's? */\n+\tif (best.base != -1 && (best.base + best.len) ==\n+\t    (NS_IN6ADDRSZ / NS_INT16SZ))\n+\t\t*tp++ = ':';\n+\t*tp++ = '\\0';\n+\n+\t/*\n+\t * Check for overflow, copy, and we're done.\n+\t */\n+\tif ((size_t)(tp - tmp) > size) {\n+\t\terrno = ENOSPC;\n+\t\treturn (NULL);\n+\t}\n+\tstrcpy(dst, tmp);\n+\treturn (dst);\n+}\n+#endif\n+\n+/* char *\n+ * inet_ntop(af, src, dst, size)\n+ *\tconvert a network format address to presentation format.\n+ * return:\n+ *\tpointer to presentation format address (`dst'), or NULL (see errno).\n+ * author:\n+ *\tPaul Vixie, 1996.\n+ */\n+const char *\n+inet_ntop(af, src, dst, size)\n+\tint af;\n+\tconst void *src;\n+\tchar *dst;\n+\tsize_t size;\n+{\n+\tswitch (af) {\n+\tcase AF_INET:\n+\t\treturn (inet_ntop4(src, dst, size));\n+#ifndef NO_IPV6\n+\tcase AF_INET6:\n+\t\treturn (inet_ntop6(src, dst, size));\n+#endif\n+\tdefault:\n+\t\terrno = EAFNOSUPPORT;\n+\t\treturn (NULL);\n+\t}\n+\t/* NOTREACHED */\n+}\n-- \n1.3.GIT\n"}]}