sys/lib/libsa/net.c

http://www.minix3.org/ · C · 141 lines · 72 code · 15 blank · 54 comment · 13 complexity · fcccc217b54a467d550ce5c814d00296 MD5 · raw file

  1. /* $NetBSD: net.c,v 1.35 2009/04/11 10:57:55 lukem Exp $ */
  2. /*
  3. * Copyright (c) 1992 Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This software was developed by the Computer Systems Engineering group
  7. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  8. * contributed to Berkeley.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. All advertising materials mentioning features or use of this software
  19. * must display the following acknowledgement:
  20. * This product includes software developed by the University of
  21. * California, Lawrence Berkeley Laboratory and its contributors.
  22. * 4. Neither the name of the University nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
  39. */
  40. #include <sys/param.h>
  41. #include <sys/socket.h>
  42. #ifdef _STANDALONE
  43. #include <lib/libkern/libkern.h>
  44. #else
  45. #include <string.h>
  46. #endif
  47. #include <net/if.h>
  48. #include <net/if_ether.h>
  49. #include <netinet/in.h>
  50. #include <netinet/in_systm.h>
  51. #include <netinet/ip.h>
  52. #ifdef _STANDALONE
  53. #include "stand.h"
  54. #define delay()
  55. #else
  56. #include <errno.h>
  57. #include <stdio.h>
  58. #include <unistd.h>
  59. #define panic printf
  60. #define delay() usleep(100000)
  61. #define getsecs() time(NULL)
  62. #endif
  63. #include "net.h"
  64. /*
  65. * Send a packet and wait for a reply, with exponential backoff.
  66. *
  67. * The send routine must return the actual number of bytes written,
  68. * or -1 on error.
  69. *
  70. * The receive routine can indicate success by returning the number of
  71. * bytes read; it can return 0 to indicate EOF; it can return -1 with a
  72. * non-zero errno to indicate failure; finally, it can return -1 with a
  73. * zero errno to indicate it isn't done yet.
  74. */
  75. ssize_t
  76. sendrecv(struct iodesc *d,
  77. ssize_t (*sproc)(struct iodesc *, void *, size_t),
  78. void *sbuf, size_t ssize,
  79. ssize_t (*rproc)(struct iodesc *, void *, size_t, saseconds_t),
  80. void *rbuf, size_t rsize)
  81. {
  82. ssize_t cc;
  83. satime_t t, tlast;
  84. saseconds_t tmo, tleft;
  85. #ifdef NET_DEBUG
  86. if (debug)
  87. printf("sendrecv: called\n");
  88. #endif
  89. tmo = MINTMO;
  90. tlast = 0;
  91. tleft = 0;
  92. t = getsecs();
  93. for (;;) {
  94. if (tleft <= 0) {
  95. if (tmo >= MAXTMO) {
  96. errno = ETIMEDOUT;
  97. return -1;
  98. }
  99. cc = (*sproc)(d, sbuf, ssize);
  100. tleft = tmo;
  101. tmo <<= 1;
  102. if (tmo > MAXTMO)
  103. tmo = MAXTMO;
  104. if (cc == -1) {
  105. /* Error on transmit; wait before retrying */
  106. while ((getsecs() - t) < tmo)
  107. delay();
  108. tleft = 0;
  109. continue;
  110. }
  111. if ((size_t)cc < ssize)
  112. panic("sendrecv: short write! (%zd < %zu)",
  113. cc, ssize);
  114. tlast = t;
  115. }
  116. /* Try to get a packet and process it. */
  117. cc = (*rproc)(d, rbuf, rsize, tleft);
  118. /* Return on data, EOF or real error. */
  119. if (cc != -1 || errno != 0)
  120. return cc;
  121. /* Timed out or didn't get the packet we're waiting for */
  122. t = getsecs();
  123. tleft -= t - tlast;
  124. tlast = t;
  125. }
  126. }