net.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* $OpenBSD: net.c,v 1.18 2015/07/16 16:12:15 mpi Exp $ */
  2. /* $NetBSD: net.c,v 1.14 1996/10/13 02:29:02 christos Exp $ */
  3. /*
  4. * Copyright (c) 1992 Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * This software was developed by the Computer Systems Engineering group
  8. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  9. * contributed to Berkeley.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. All advertising materials mentioning features or use of this software
  20. * must display the following acknowledgement:
  21. * This product includes software developed by the University of
  22. * California, Lawrence Berkeley Laboratory and its contributors.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
  40. */
  41. #include <sys/param.h>
  42. #include <sys/socket.h>
  43. #include <net/if.h>
  44. #include <netinet/in.h>
  45. #include <netinet/if_ether.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/ip_var.h>
  48. #include "stand.h"
  49. #include "net.h"
  50. /*
  51. * Send a packet and wait for a reply, with exponential backoff.
  52. *
  53. * The send routine must return the actual number of bytes written.
  54. *
  55. * The receive routine can indicate success by returning the number of
  56. * bytes read; it can return 0 to indicate EOF; it can return -1 with a
  57. * non-zero errno to indicate failure; finally, it can return -1 with a
  58. * zero errno to indicate it isn't done yet.
  59. */
  60. ssize_t
  61. sendrecv(struct iodesc *d, ssize_t (*sproc)(struct iodesc *, void *, size_t),
  62. void *sbuf, size_t ssize,
  63. ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t),
  64. void *rbuf, size_t rsize)
  65. {
  66. ssize_t cc;
  67. time_t t, tmo, tlast;
  68. long tleft;
  69. #ifdef NET_DEBUG
  70. if (debug)
  71. printf("sendrecv: called\n");
  72. #endif
  73. tmo = MINTMO;
  74. tlast = tleft = 0;
  75. t = getsecs();
  76. for (;;) {
  77. if (tleft <= 0) {
  78. if (tmo >= MAXTMO) {
  79. errno = ETIMEDOUT;
  80. return -1;
  81. }
  82. cc = (*sproc)(d, sbuf, ssize);
  83. if (cc < 0 || (size_t)cc < ssize)
  84. panic("sendrecv: short write! (%d < %d)",
  85. cc, ssize);
  86. tleft = tmo;
  87. tmo <<= 1;
  88. if (tmo > MAXTMO)
  89. tmo = MAXTMO;
  90. tlast = t;
  91. }
  92. /* Try to get a packet and process it. */
  93. cc = (*rproc)(d, rbuf, rsize, tleft);
  94. /* Return on data, EOF or real error. */
  95. if (cc != -1 || errno != 0)
  96. return (cc);
  97. /* Timed out or didn't get the packet we're waiting for */
  98. t = getsecs();
  99. tleft -= t - tlast;
  100. tlast = t;
  101. }
  102. }
  103. /*
  104. * Like inet_addr() in the C library, but we only accept base-10.
  105. * Return values are in network order.
  106. */
  107. u_int32_t
  108. inet_addr(const char *cp)
  109. {
  110. u_long val;
  111. int n;
  112. char c;
  113. u_int parts[4];
  114. u_int *pp = parts;
  115. for (;;) {
  116. /*
  117. * Collect number up to ``.''.
  118. * Values are specified as for C:
  119. * 0x=hex, 0=octal, other=decimal.
  120. */
  121. val = 0;
  122. while ((c = *cp) != '\0') {
  123. if (c >= '0' && c <= '9') {
  124. val = (val * 10) + (c - '0');
  125. cp++;
  126. continue;
  127. }
  128. break;
  129. }
  130. if (*cp == '.') {
  131. /*
  132. * Internet format:
  133. * a.b.c.d
  134. * a.b.c (with c treated as 16-bits)
  135. * a.b (with b treated as 24 bits)
  136. */
  137. if (pp >= parts + 3 || val > 0xff)
  138. goto bad;
  139. *pp++ = val, cp++;
  140. } else
  141. break;
  142. }
  143. /*
  144. * Check for trailing characters.
  145. */
  146. if (*cp != '\0')
  147. goto bad;
  148. /*
  149. * Concoct the address according to
  150. * the number of parts specified.
  151. */
  152. n = pp - parts + 1;
  153. switch (n) {
  154. case 1: /* a -- 32 bits */
  155. break;
  156. case 2: /* a.b -- 8.24 bits */
  157. if (val > 0xffffff)
  158. goto bad;
  159. val |= parts[0] << 24;
  160. break;
  161. case 3: /* a.b.c -- 8.8.16 bits */
  162. if (val > 0xffff)
  163. goto bad;
  164. val |= (parts[0] << 24) | (parts[1] << 16);
  165. break;
  166. case 4: /* a.b.c.d -- 8.8.8.8 bits */
  167. if (val > 0xff)
  168. goto bad;
  169. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  170. break;
  171. }
  172. return (htonl(val));
  173. bad:
  174. return (htonl(INADDR_NONE));
  175. }
  176. const char *
  177. inet_ntoa(struct in_addr ia)
  178. {
  179. return (intoa(ia.s_addr));
  180. }
  181. /* Similar to inet_ntoa() */
  182. const char *
  183. intoa(u_int32_t addr)
  184. {
  185. char *cp;
  186. u_int byte;
  187. int n;
  188. static char buf[sizeof(".255.255.255.255")];
  189. addr = ntohl(addr);
  190. cp = &buf[sizeof buf];
  191. *--cp = '\0';
  192. n = 4;
  193. do {
  194. byte = addr & 0xff;
  195. *--cp = byte % 10 + '0';
  196. byte /= 10;
  197. if (byte > 0) {
  198. *--cp = byte % 10 + '0';
  199. byte /= 10;
  200. if (byte > 0)
  201. *--cp = byte + '0';
  202. }
  203. *--cp = '.';
  204. addr >>= 8;
  205. } while (--n > 0);
  206. return (cp+1);
  207. }
  208. static const char *
  209. number(const char *s, int *n)
  210. {
  211. for (*n = 0; isdigit(*s); s++)
  212. *n = (*n * 10) + *s - '0';
  213. return s;
  214. }
  215. u_int32_t
  216. ip_convertaddr(const char *p)
  217. {
  218. #define IP_ANYADDR 0
  219. u_int32_t addr = 0, n;
  220. if (p == (char *)0 || *p == '\0')
  221. return IP_ANYADDR;
  222. p = number(p, &n);
  223. addr |= (n << 24) & 0xff000000;
  224. if (*p == '\0' || *p++ != '.')
  225. return IP_ANYADDR;
  226. p = number(p, &n);
  227. addr |= (n << 16) & 0xff0000;
  228. if (*p == '\0' || *p++ != '.')
  229. return IP_ANYADDR;
  230. p = number(p, &n);
  231. addr |= (n << 8) & 0xff00;
  232. if (*p == '\0' || *p++ != '.')
  233. return IP_ANYADDR;
  234. p = number(p, &n);
  235. addr |= n & 0xff;
  236. if (*p != '\0')
  237. return IP_ANYADDR;
  238. return htonl(addr);
  239. }