sys/lib/libsa/udp.c

http://www.minix3.org/ · C · 136 lines · 74 code · 18 blank · 44 comment · 14 complexity · de5e2d060d79000cd3957f5bcd202bca MD5 · raw file

  1. /* $NetBSD: udp.c,v 1.11 2011/05/11 16:23:40 zoltan 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. #include <netinet/ip_var.h>
  53. #include <netinet/udp.h>
  54. #include <netinet/udp_var.h>
  55. #include "stand.h"
  56. #include "net.h"
  57. /* Caller must leave room for ethernet, ip and udp headers in front!! */
  58. ssize_t
  59. sendudp(struct iodesc *d, void *pkt, size_t len)
  60. {
  61. ssize_t cc;
  62. struct udphdr *uh;
  63. #ifdef NET_DEBUG
  64. if (debug) {
  65. printf("sendudp: d=%lx called.\n", (long)d);
  66. if (d) {
  67. printf("saddr: %s:%d",
  68. inet_ntoa(d->myip), ntohs(d->myport));
  69. printf(" daddr: %s:%d\n",
  70. inet_ntoa(d->destip), ntohs(d->destport));
  71. }
  72. }
  73. #endif
  74. uh = (struct udphdr *)pkt - 1;
  75. len += sizeof(*uh);
  76. (void)memset(uh, 0, sizeof(*uh));
  77. uh->uh_sport = d->myport;
  78. uh->uh_dport = d->destport;
  79. uh->uh_ulen = htons(len);
  80. cc = sendip(d, uh, len, IPPROTO_UDP);
  81. if (cc == -1)
  82. return -1;
  83. if ((size_t)cc != len)
  84. panic("sendudp: bad write (%zd != %zu)", cc, len);
  85. return (cc - sizeof(*uh));
  86. }
  87. /*
  88. * Receive a UDP packet and validate it is for us.
  89. * Caller leaves room for the headers (Ether, IP, UDP)
  90. */
  91. ssize_t
  92. readudp(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
  93. {
  94. ssize_t n;
  95. struct udphdr *uh;
  96. uh = (struct udphdr *)pkt - 1;
  97. n = readip(d, uh, len + sizeof(*uh), tleft, IPPROTO_UDP);
  98. if (n == -1 || (size_t)n < sizeof(*uh))
  99. return -1;
  100. if (uh->uh_dport != d->myport) {
  101. #ifdef NET_DEBUG
  102. if (debug)
  103. printf("readudp: bad dport %d != %d\n",
  104. d->myport, ntohs(uh->uh_dport));
  105. #endif
  106. return -1;
  107. }
  108. if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
  109. #ifdef NET_DEBUG
  110. if (debug)
  111. printf("readudp: bad udp len %d < %d\n",
  112. ntohs(uh->uh_ulen), (int)sizeof(*uh));
  113. #endif
  114. return -1;
  115. }
  116. n -= sizeof(*uh);
  117. return n;
  118. }