ipv6spec.c.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. ipv6spec.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='ipv4spec.c.html' title=' ipv4spec.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='keys.c.html' title=' keys.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * size_t ipv6spec (char const * string, byte memory []);
  24. *
  25. * memory.h
  26. *
  27. * encode a 16-byte memory region with the binary equivalent of an
  28. * ipv6 address string; ipv6 addresses are defined as 8 16-bit hex
  29. * numbers separated with colons; two consecutive colons represent
  30. * one or more 0000 fields;
  31. *
  32. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  33. * Copyright (c) 2001-2006 by Charles Maier Associates;
  34. * Licensed under the Internet Software Consortium License;
  35. *
  36. *--------------------------------------------------------------------*/
  37. #ifndef IPV6SPEC_SOURCE
  38. #define IPV6SPEC_SOURCE
  39. #include &lt;memory.h&gt;
  40. #include &lt;ctype.h&gt;
  41. #include &lt;errno.h&gt;
  42. #include &quot;../tools/memory.h&quot;
  43. #include &quot;../tools/number.h&quot;
  44. #include &quot;../tools/error.h&quot;
  45. size_t ipv6spec (char const * string, void * memory)
  46. {
  47. char const * number = string;
  48. byte * origin = (byte *)(memory);
  49. byte * offset = (byte *)(memory);
  50. byte * extent = offset + IPv6_LEN;
  51. byte * marker = offset + IPv6_LEN;
  52. unsigned radix = RADIX_HEX;
  53. unsigned digit = 0;
  54. while ((*number) &amp;&amp; (offset &lt; extent))
  55. {
  56. uint32_t value = 0;
  57. if (offset &gt; origin)
  58. {
  59. if (*number == HEX_EXTENDER)
  60. {
  61. number++;
  62. }
  63. if (*number == HEX_EXTENDER)
  64. {
  65. marker = offset;
  66. }
  67. }
  68. while ((digit = todigit (*number)) &lt; radix)
  69. {
  70. value *= radix;
  71. value += digit;
  72. if (value &gt;&gt; 16)
  73. {
  74. error (1, ERANGE, &quot;IPv6 '%s' field %d exceeds 16 bits&quot;, string, 1 + ((unsigned)(offset - origin) &gt;&gt; 1));
  75. }
  76. number++;
  77. }
  78. *offset++ = (byte)(value &gt;&gt; 8);
  79. *offset++ = (byte)(value &gt;&gt; 0);
  80. }
  81. #if defined (WIN32)
  82. while (isspace (*number))
  83. {
  84. number++;
  85. }
  86. #endif
  87. if (*number)
  88. {
  89. error (1, EINVAL, &quot;IPv6 '%s' includes trash '%s'&quot;, string, number);
  90. }
  91. if (offset &lt; extent)
  92. {
  93. while (offset &gt; marker)
  94. {
  95. *--extent = *--offset;
  96. }
  97. while (extent &gt; offset)
  98. {
  99. *--extent = 0;
  100. }
  101. }
  102. if (offset &lt; marker)
  103. {
  104. error (1, EINVAL, &quot;IPv6 '%s' has only %d fields&quot;, string, (unsigned)(offset - origin) &gt;&gt; 1);
  105. }
  106. return (offset - origin);
  107. }
  108. /*====================================================================*
  109. * demo/test program;
  110. *--------------------------------------------------------------------*/
  111. #if 0
  112. #include &lt;stdio.h&gt;
  113. char const * program_name = &quot;ipv6spec&quot;;
  114. int main (int argc, char * argv [])
  115. {
  116. byte memory [16];
  117. char string [48];
  118. while (*++argv)
  119. {
  120. ipv6spec (* argv, memory);
  121. hexdecode (memory, sizeof (memory), string, sizeof (string));
  122. printf (&quot;%s %s\n&quot;, string, * argv);
  123. }
  124. return (0);
  125. }
  126. #endif
  127. /*====================================================================*
  128. *
  129. *--------------------------------------------------------------------*/
  130. #endif
  131. </pre>
  132. <div class='footerlink'>
  133. [<a href='ipv4spec.c.html' title=' ipv4spec.c '>PREV</a>]
  134. [<a href='toolkit.html' title=' Index '>HOME</a>]
  135. [<a href='keys.c.html' title=' keys.c '>NEXT</a>]
  136. </div>
  137. </body>
  138. </html>