]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/libsa/udp.c
MFV: less: Do not trust st_size if it equals zero
[FreeBSD/FreeBSD.git] / stand / libsa / udp.c
1 /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $    */
2
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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/param.h>
37 #include <sys/socket.h>
38
39 #include <string.h>
40
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44 #include <netinet/in_systm.h>
45
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
48 #include <netinet/udp.h>
49 #include <netinet/udp_var.h>
50
51 #include "stand.h"
52 #include "net.h"
53
54 /* Caller must leave room for ethernet, ip and udp headers in front!! */
55 ssize_t
56 sendudp(struct iodesc *d, void *pkt, size_t len)
57 {
58         ssize_t cc;
59         struct udpiphdr *ui;
60         struct udphdr *uh;
61
62 #ifdef NET_DEBUG
63         if (debug) {
64                 printf("sendudp: d=%lx called.\n", (long)d);
65                 if (d) {
66                         printf("saddr: %s:%d",
67                             inet_ntoa(d->myip), ntohs(d->myport));
68                         printf(" daddr: %s:%d\n",
69                             inet_ntoa(d->destip), ntohs(d->destport));
70                 }
71         }
72 #endif
73
74         ui = (struct udpiphdr *)pkt - 1;
75         bzero(ui, sizeof(*ui));
76
77         uh = (struct udphdr *)pkt - 1;
78         len += sizeof(*uh);
79
80         uh->uh_sport = d->myport;
81         uh->uh_dport = d->destport;
82         uh->uh_ulen = htons(len);
83
84         ui->ui_pr = IPPROTO_UDP;
85         ui->ui_len = uh->uh_ulen;
86         ui->ui_src = d->myip;
87         ui->ui_dst = d->destip;
88
89 #ifndef UDP_NO_CKSUM
90         uh->uh_sum = in_cksum(ui, len + sizeof (struct ip));
91 #endif
92
93         cc = sendip(d, uh, len, IPPROTO_UDP);
94         if (cc == -1)
95                 return (-1);
96         if (cc != len)
97                 panic("sendudp: bad write (%zd != %zd)", cc, len);
98         return (cc - sizeof(*uh));
99 }
100
101 /*
102  * Receive a UDP packet and validate it is for us.
103  */
104 ssize_t
105 readudp(struct iodesc *d, void **pkt, void **payload, time_t tleft)
106 {
107         ssize_t n;
108         struct udphdr *uh;
109         void *ptr;
110
111 #ifdef NET_DEBUG
112         if (debug)
113                 printf("readudp: called\n");
114 #endif
115
116         uh = NULL;
117         ptr = NULL;
118         n = readip(d, &ptr, (void **)&uh, tleft, IPPROTO_UDP);
119         if (n == -1 || n < sizeof(*uh) || n != ntohs(uh->uh_ulen)) {
120                 free(ptr);
121                 return (-1);
122         }
123
124         if (uh->uh_dport != d->myport) {
125 #ifdef NET_DEBUG
126                 if (debug)
127                         printf("readudp: bad dport %d != %d\n",
128                                 d->myport, ntohs(uh->uh_dport));
129 #endif
130                 free(ptr);
131                 return (-1);
132         }
133
134 #ifndef UDP_NO_CKSUM
135         if (uh->uh_sum) {
136                 struct udpiphdr *ui;
137                 struct ip *ip;
138                 struct ip tip;
139
140                 n = ntohs(uh->uh_ulen) + sizeof(*ip);
141
142                 /* Check checksum (must save and restore ip header) */
143                 ip = (struct ip *)uh - 1;
144                 tip = *ip;
145                 ui = (struct udpiphdr *)ip;
146                 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
147                 ui->ui_len = uh->uh_ulen;
148                 if (in_cksum(ui, n) != 0) {
149 #ifdef NET_DEBUG
150                         if (debug)
151                                 printf("readudp: bad cksum\n");
152 #endif
153                         free(ptr);
154                         return (-1);
155                 }
156                 *ip = tip;
157         }
158 #endif
159         if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
160 #ifdef NET_DEBUG
161                 if (debug)
162                         printf("readudp: bad udp len %d < %d\n",
163                                 ntohs(uh->uh_ulen), (int)sizeof(*uh));
164 #endif
165                 free(ptr);
166                 return (-1);
167         }
168
169         n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ? 
170             ntohs(uh->uh_ulen) - sizeof(*uh) : n;
171         *pkt = ptr;
172         *payload = (void *)((uintptr_t)uh + sizeof(*uh));
173         return (n);
174 }