ipv4spec.c.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ipv4spec.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='int6kwait.c.html' title=' int6kwait.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='ipv6spec.c.html' title=' ipv6spec.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * size_t ipv4spec (char const *string, void * memory);
  24. *
  25. * memory.h
  26. *
  27. * encode a 4-byte memory region with the equivalent of an IPv4
  28. * dotted decimal string; all field delimiters must be present
  29. * but individual fields may have leading zeros or be empty;
  30. *
  31. * 0.0.0.0 0x00, 0x00, 0x00, 0x00
  32. * 127...1 0x7F, 0x00, 0x00, 0x01
  33. * 192.168.099.000 0xC0, 0xA8, 0x63, 0x00
  34. *
  35. *. released 2005 by charles maier associates ltd. for public use;
  36. *: compiled on debian gnu/linux with gcc 2.95 compiler;
  37. *; licensed under the gnu public license version two;
  38. *
  39. *--------------------------------------------------------------------*/
  40. #ifndef IPV4SPEC_SOURCE
  41. #define IPV4SPEC_SOURCE
  42. #include &lt;ctype.h&gt;
  43. #include &quot;../tools/memory.h&quot;
  44. #include &quot;../tools/number.h&quot;
  45. #include &quot;../tools/error.h&quot;
  46. size_t ipv4spec (char const * string, void * memory)
  47. {
  48. char const * number = string;
  49. byte * origin = (byte *)(memory);
  50. byte * offset = (byte *)(memory);
  51. byte * extent = offset + IPv4_LEN;
  52. unsigned radix = RADIX_DEC;
  53. unsigned digit = 0;
  54. while ((*number) &amp;&amp; (offset &lt; extent))
  55. {
  56. unsigned value = 0;
  57. if (offset &gt; origin)
  58. {
  59. if (*number == DEC_EXTENDER)
  60. {
  61. number++;
  62. }
  63. }
  64. while ((digit = todigit (*number)) &lt; radix)
  65. {
  66. value *= radix;
  67. value += digit;
  68. if (value &gt;&gt; 8)
  69. {
  70. error (1, ERANGE, &quot;IPv4 '%s' octet %d exceeds 8 bits&quot;, string, (unsigned)(offset - origin) + 1);
  71. }
  72. number++;
  73. }
  74. *offset++ = value;
  75. }
  76. #if defined (WIN32)
  77. while (isspace (*number))
  78. {
  79. number++;
  80. }
  81. #endif
  82. if (offset &lt; extent)
  83. {
  84. error (1, EINVAL, &quot;IPv4 '%s' has only %d octets&quot;, string, (unsigned)(offset - origin));
  85. }
  86. if (*number)
  87. {
  88. error (1, EINVAL, &quot;IPv4 '%s' contains trash '%s'&quot;, string, number);
  89. }
  90. return (offset - origin);
  91. }
  92. /*====================================================================*
  93. * demo/test program;
  94. *--------------------------------------------------------------------*/
  95. #if 0
  96. #include &lt;stdio.h&gt;
  97. char const * program_name = &quot;ipv4spec&quot;;
  98. int main (int argc, char * argv [])
  99. {
  100. byte memory [4];
  101. char string [16];
  102. while (*++argv)
  103. {
  104. ipv4spec (* argv, memory);
  105. hexdecode (memory, sizeof (memory), string, sizeof (string));
  106. printf (&quot;%s %s\n&quot;, string, * argv);
  107. }
  108. return (0);
  109. }
  110. #endif
  111. /*====================================================================*
  112. *
  113. *--------------------------------------------------------------------*/
  114. #endif
  115. </pre>
  116. <div class='footerlink'>
  117. [<a href='int6kwait.c.html' title=' int6kwait.c '>PREV</a>]
  118. [<a href='toolkit.html' title=' Index '>HOME</a>]
  119. [<a href='ipv6spec.c.html' title=' ipv6spec.c '>NEXT</a>]
  120. </div>
  121. </body>
  122. </html>