You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
7.6 KiB

9 years ago
  1. /* inet_XtoX.c
  2. * Simple implementation of the following functions:
  3. * inet_ntop(), inet_ntoa(), inet_pton(), inet_aton().
  4. *
  5. * Differences from traditional implementaitons:
  6. * o modifies destination buffers even on error return.
  7. * o no fancy (hex, or 1.2) input support in inet_aton()
  8. * o inet_aton() does not accept junk after an IP address.
  9. * o inet_ntop(AF_INET) requires at least 16 bytes in dest,
  10. * and inet_ntop(AF_INET6) at least 40 bytes
  11. * (traditional inet_ntop() will try to fit anyway)
  12. *
  13. * Compile with -Dinet_XtoX_prefix=pfx_ to have pfx_*() instead of inet_*()
  14. * Compile with -Dinet_XtoX_no_ntop or -Dinet_XtoX_no_pton
  15. * to disable net2str or str2net conversions.
  16. *
  17. * #define inet_XtoX_prototypes and #include "this_file.c"
  18. * to get function prototypes only (but not for inet_ntoa()).
  19. * #define inet_XtoX_decl to be `static' for static visibility,
  20. * or use __declspec(dllexport) or somesuch...
  21. *
  22. * Compile with -DTEST to test against stock implementation.
  23. *
  24. * Written by Michael Tokarev. Public domain.
  25. */
  26. #ifdef inet_XtoX_prototypes
  27. struct in_addr;
  28. #else
  29. #include <errno.h>
  30. #ifdef TEST
  31. # include <netinet/in.h>
  32. # include <sys/socket.h>
  33. # include <arpa/inet.h>
  34. # include <stdio.h>
  35. # include <stdlib.h>
  36. # include <unistd.h>
  37. # include <string.h>
  38. # undef inet_XtoX_prefix
  39. # define inet_XtoX_prefix mjt_inet_
  40. # undef inet_XtoX_no_ntop
  41. # undef inet_XtoX_no_pton
  42. #endif /* TEST */
  43. #endif /* inet_XtoX_prototypes */
  44. #ifndef inet_XtoX_prefix
  45. # define inet_XtoX_prefix inet_
  46. #endif
  47. #ifndef inet_XtoX_decl
  48. # define inet_XtoX_decl /* empty */
  49. #endif
  50. #define cc2_(x,y) cc2__(x,y)
  51. #define cc2__(x,y) x##y
  52. #define fn(x) cc2_(inet_XtoX_prefix,x)
  53. #ifndef inet_XtoX_no_ntop
  54. inet_XtoX_decl const char *
  55. fn(ntop)(int af, const void *src, char *dst, int size);
  56. #ifndef inet_XtoX_prototypes
  57. static int mjt_ntop4(const void *_src, char *dst, int size) {
  58. unsigned i, x, r;
  59. char *p;
  60. const unsigned char *s = _src;
  61. if (size < 4*4) /* for simplicity, disallow non-max-size buffer */
  62. return 0;
  63. for (i = 0, p = dst; i < 4; ++i) {
  64. if (i) *p++ = '.';
  65. x = r = s[i];
  66. if (x > 99) { *p++ = (char)(r / 100 + '0'); r %= 100; }
  67. if (x > 9) { *p++ = (char)(r / 10 + '0'); r %= 10; }
  68. *p++ = (char)(r + '0');
  69. }
  70. *p = '\0';
  71. return 1;
  72. }
  73. static char *hexc(char *p, unsigned x) {
  74. static char hex[16] = "0123456789abcdef";
  75. if (x > 0x0fff) *p++ = hex[(x >>12) & 15];
  76. if (x > 0x00ff) *p++ = hex[(x >> 8) & 15];
  77. if (x > 0x000f) *p++ = hex[(x >> 4) & 15];
  78. *p++ = hex[x & 15];
  79. return p;
  80. }
  81. static int mjt_ntop6(const void *_src, char *dst, int size) {
  82. unsigned i;
  83. unsigned short w[8];
  84. unsigned bs = 0, cs = 0;
  85. unsigned bl = 0, cl = 0;
  86. char *p;
  87. const unsigned char *s = _src;
  88. if (size < 40) /* for simplicity, disallow non-max-size buffer */
  89. return 0;
  90. for(i = 0; i < 8; ++i, s += 2) {
  91. w[i] = (((unsigned short)(s[0])) << 8) | s[1];
  92. if (!w[i]) {
  93. if (!cl++) cs = i;
  94. }
  95. else {
  96. if (cl > bl) bl = cl, bs = cs;
  97. }
  98. }
  99. if (cl > bl) bl = cl, bs = cs;
  100. p = dst;
  101. if (bl == 1)
  102. bl = 0;
  103. if (bl) {
  104. for(i = 0; i < bs; ++i) {
  105. if (i) *p++ = ':';
  106. p = hexc(p, w[i]);
  107. }
  108. *p++ = ':';
  109. i += bl;
  110. if (i == 8)
  111. *p++ = ':';
  112. }
  113. else
  114. i = 0;
  115. for(; i < 8; ++i) {
  116. if (i) *p++ = ':';
  117. if (i == 6 && !bs && (bl == 6 || (bl == 5 && w[5] == 0xffff)))
  118. return mjt_ntop4(s - 4, p, size - (p - dst));
  119. p = hexc(p, w[i]);
  120. }
  121. *p = '\0';
  122. return 1;
  123. }
  124. inet_XtoX_decl const char *
  125. fn(ntop)(int af, const void *src, char *dst, int size) {
  126. switch(af) {
  127. /* don't use AF_*: don't mess with headers */
  128. case 2: /* AF_INET */ if (mjt_ntop4(src, dst, size)) return dst; break;
  129. case 10: /* AF_INET6 */ if (mjt_ntop6(src, dst, size)) return dst; break;
  130. default: errno = EAFNOSUPPORT; return (char*)0;
  131. }
  132. errno = ENOSPC;
  133. return (char*)0;
  134. }
  135. inet_XtoX_decl const char *
  136. fn(ntoa)(struct in_addr addr) {
  137. static char buf[4*4];
  138. mjt_ntop4(&addr, buf, sizeof(buf));
  139. return buf;
  140. }
  141. #endif /* inet_XtoX_prototypes */
  142. #endif /* inet_XtoX_no_ntop */
  143. #ifndef inet_XtoX_no_pton
  144. inet_XtoX_decl int fn(pton)(int af, const char *src, void *dst);
  145. inet_XtoX_decl int fn(aton)(const char *src, struct in_addr *addr);
  146. #ifndef inet_XtoX_prototypes
  147. static int mjt_pton4(const char *c, void *dst) {
  148. unsigned char *a = dst;
  149. unsigned n, o;
  150. for (n = 0; n < 4; ++n) {
  151. if (*c < '0' || *c > '9')
  152. return 0;
  153. o = *c++ - '0';
  154. while(*c >= '0' && *c <= '9')
  155. if ((o = o * 10 + (*c++ - '0')) > 255)
  156. return 0;
  157. if (*c++ != (n == 3 ? '\0' : '.'))
  158. return 0;
  159. *a++ = (unsigned char)o;
  160. }
  161. return 1;
  162. }
  163. static int mjt_pton6(const char *c, void *dst) {
  164. unsigned short w[8], *a = w, *z, *i;
  165. unsigned v, o;
  166. const char *sc;
  167. unsigned char *d = dst;
  168. if (*c != ':') z = (unsigned short*)0;
  169. else if (*++c != ':') return 0;
  170. else ++c, z = a;
  171. i = 0;
  172. for(;;) {
  173. v = 0;
  174. sc = c;
  175. for(;;) {
  176. if (*c >= '0' && *c <= '9') o = *c - '0';
  177. else if (*c >= 'a' && *c <= 'f') o = *c - 'a' + 10;
  178. else if (*c >= 'A' && *c <= 'F') o = *c - 'A' + 10;
  179. else break;
  180. v = (v << 4) | o;
  181. if (v > 0xffff) return 0;
  182. ++c;
  183. }
  184. if (sc == c) {
  185. if (z == a && !*c)
  186. break;
  187. else
  188. return 0;
  189. }
  190. if (*c == ':') {
  191. if (a >= w + 8)
  192. return 0;
  193. *a++ = v;
  194. if (*++c == ':') {
  195. if (z)
  196. return 0;
  197. z = a;
  198. if (!*++c)
  199. break;
  200. }
  201. }
  202. else if (!*c) {
  203. if (a >= w + 8)
  204. return 0;
  205. *a++ = v;
  206. break;
  207. }
  208. else if (*c == '.') {
  209. if (a > w + 6)
  210. return 0;
  211. if (!mjt_pton4(sc, d))
  212. return 0;
  213. *a++ = ((unsigned)(d[0]) << 8) | d[1];
  214. *a++ = ((unsigned)(d[2]) << 8) | d[3];
  215. break;
  216. }
  217. else
  218. return 0;
  219. }
  220. v = w + 8 - a;
  221. if ((v && !z) || (!v && z))
  222. return 0;
  223. for(i = w; ; ++i) {
  224. if (i == z)
  225. while(v--) { *d++ = '\0'; *d++ = '\0'; }
  226. if (i >= a)
  227. break;
  228. *d++ = (unsigned char)((*i >> 8) & 255);
  229. *d++ = (unsigned char)(*i & 255);
  230. }
  231. return 1;
  232. }
  233. inet_XtoX_decl int fn(pton)(int af, const char *src, void *dst) {
  234. switch(af) {
  235. /* don't use AF_*: don't mess with headers */
  236. case 2 /* AF_INET */: return mjt_pton4(src, dst);
  237. case 10 /* AF_INET6 */: return mjt_pton6(src, dst);
  238. default: errno = EAFNOSUPPORT; return -1;
  239. }
  240. }
  241. inet_XtoX_decl int fn(aton)(const char *src, struct in_addr *addr) {
  242. return mjt_pton4(src, addr);
  243. }
  244. #endif /* inet_XtoX_prototypes */
  245. #endif /* inet_XtoX_no_pton */
  246. #ifdef TEST
  247. int main(int argc, char **argv) {
  248. int i;
  249. char n0[16], n1[16];
  250. char p0[64], p1[64];
  251. int af = AF_INET;
  252. int pl = sizeof(p0);
  253. int r0, r1;
  254. const char *s0, *s1;
  255. while((i = getopt(argc, argv, "46a:p:")) != EOF) switch(i) {
  256. case '4': af = AF_INET; break;
  257. case '6': af = AF_INET6; break;
  258. case 'a': case 'p': pl = atoi(optarg); break;
  259. default: return 1;
  260. }
  261. for(i = optind; i < argc; ++i) {
  262. char *a = argv[i];
  263. printf("%s:\n", a);
  264. r0 = inet_pton(af, a, n0);
  265. printf(" p2n stock: %s\n",
  266. (r0 < 0 ? "(notsupp)" : !r0 ? "(inval)" : fn(ntop)(af,n0,p0,sizeof(p0))));
  267. r1 = fn(pton)(af, a, n1);
  268. printf(" p2n this : %s\n",
  269. (r1 < 0 ? "(notsupp)" : !r1 ? "(inval)" : fn(ntop)(af,n1,p1,sizeof(p1))));
  270. if ((r0 > 0) != (r1 > 0) ||
  271. (r0 > 0 && r1 > 0 && memcmp(n0, n1, af == AF_INET ? 4 : 16) != 0))
  272. printf(" DIFFER!\n");
  273. s0 = inet_ntop(af, n1, p0, pl);
  274. printf(" n2p stock: %s\n", s0 ? s0 : "(inval)");
  275. s1 = fn(ntop)(af, n1, p1, pl);
  276. printf(" n2p this : %s\n", s1 ? s1 : "(inval)");
  277. if ((s0 != 0) != (s1 != 0) ||
  278. (s0 && s1 && strcmp(s0, s1) != 0))
  279. printf(" DIFFER!\n");
  280. }
  281. return 0;
  282. }
  283. #endif /* TEST */