1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 | #include "ip_icmp.h"
|
---|
11 | #include "main.h"
|
---|
12 | #ifdef __sun__
|
---|
13 | #include <sys/filio.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | void
|
---|
17 | so_init()
|
---|
18 | {
|
---|
19 | /* Nothing yet */
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | struct socket *
|
---|
24 | solookup(head, laddr, lport, faddr, fport)
|
---|
25 | struct socket *head;
|
---|
26 | struct in_addr laddr;
|
---|
27 | u_int lport;
|
---|
28 | struct in_addr faddr;
|
---|
29 | u_int fport;
|
---|
30 | {
|
---|
31 | struct socket *so;
|
---|
32 |
|
---|
33 | for (so = head->so_next; so != head; so = so->so_next) {
|
---|
34 | if (so->so_lport == lport &&
|
---|
35 | so->so_laddr.s_addr == laddr.s_addr &&
|
---|
36 | so->so_faddr.s_addr == faddr.s_addr &&
|
---|
37 | so->so_fport == fport)
|
---|
38 | break;
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (so == head)
|
---|
42 | return (struct socket *)NULL;
|
---|
43 | return so;
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Create a new socket, initialise the fields
|
---|
49 | * It is the responsibility of the caller to
|
---|
50 | * insque() it into the correct linked-list
|
---|
51 | */
|
---|
52 | struct socket *
|
---|
53 | socreate()
|
---|
54 | {
|
---|
55 | struct socket *so;
|
---|
56 |
|
---|
57 | so = (struct socket *)malloc(sizeof(struct socket));
|
---|
58 | if(so) {
|
---|
59 | memset(so, 0, sizeof(struct socket));
|
---|
60 | so->so_state = SS_NOFDREF;
|
---|
61 | so->s = -1;
|
---|
62 | }
|
---|
63 | return(so);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * remque and free a socket, clobber cache
|
---|
68 | */
|
---|
69 | void
|
---|
70 | sofree(PNATState pData, struct socket *so)
|
---|
71 | {
|
---|
72 | if (so->so_emu==EMU_RSH && so->extra) {
|
---|
73 | sofree(pData, so->extra);
|
---|
74 | so->extra=NULL;
|
---|
75 | }
|
---|
76 | if (so == tcp_last_so)
|
---|
77 | tcp_last_so = &tcb;
|
---|
78 | else if (so == udp_last_so)
|
---|
79 | udp_last_so = &udb;
|
---|
80 |
|
---|
81 | m_free(pData, so->so_m);
|
---|
82 |
|
---|
83 | if(so->so_next && so->so_prev)
|
---|
84 | remque(pData, so); /* crashes if so is not in a queue */
|
---|
85 |
|
---|
86 | free(so);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Read from so's socket into sb_snd, updating all relevant sbuf fields
|
---|
91 | * NOTE: This will only be called if it is select()ed for reading, so
|
---|
92 | * a read() of 0 (or less) means it's disconnected
|
---|
93 | */
|
---|
94 | int
|
---|
95 | soread(PNATState pData, struct socket *so)
|
---|
96 | {
|
---|
97 | int n, nn, lss, total;
|
---|
98 | struct sbuf *sb = &so->so_snd;
|
---|
99 | int len = sb->sb_datalen - sb->sb_cc;
|
---|
100 | struct iovec iov[2];
|
---|
101 | int mss = so->so_tcpcb->t_maxseg;
|
---|
102 |
|
---|
103 | DEBUG_CALL("soread");
|
---|
104 | DEBUG_ARG("so = %lx", (long )so);
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * No need to check if there's enough room to read.
|
---|
108 | * soread wouldn't have been called if there weren't
|
---|
109 | */
|
---|
110 |
|
---|
111 | len = sb->sb_datalen - sb->sb_cc;
|
---|
112 |
|
---|
113 | iov[0].iov_base = sb->sb_wptr;
|
---|
114 | iov[1].iov_base = 0;
|
---|
115 | iov[1].iov_len = 0;
|
---|
116 | if (sb->sb_wptr < sb->sb_rptr) {
|
---|
117 | iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
|
---|
118 | /* Should never succeed, but... */
|
---|
119 | if (iov[0].iov_len > len)
|
---|
120 | iov[0].iov_len = len;
|
---|
121 | if (iov[0].iov_len > mss)
|
---|
122 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
123 | n = 1;
|
---|
124 | } else {
|
---|
125 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
|
---|
126 | /* Should never succeed, but... */
|
---|
127 | if (iov[0].iov_len > len) iov[0].iov_len = len;
|
---|
128 | len -= iov[0].iov_len;
|
---|
129 | if (len) {
|
---|
130 | iov[1].iov_base = sb->sb_data;
|
---|
131 | iov[1].iov_len = sb->sb_rptr - sb->sb_data;
|
---|
132 | if(iov[1].iov_len > len)
|
---|
133 | iov[1].iov_len = len;
|
---|
134 | total = iov[0].iov_len + iov[1].iov_len;
|
---|
135 | if (total > mss) {
|
---|
136 | lss = total%mss;
|
---|
137 | if (iov[1].iov_len > lss) {
|
---|
138 | iov[1].iov_len -= lss;
|
---|
139 | n = 2;
|
---|
140 | } else {
|
---|
141 | lss -= iov[1].iov_len;
|
---|
142 | iov[0].iov_len -= lss;
|
---|
143 | n = 1;
|
---|
144 | }
|
---|
145 | } else
|
---|
146 | n = 2;
|
---|
147 | } else {
|
---|
148 | if (iov[0].iov_len > mss)
|
---|
149 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
150 | n = 1;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | #ifdef HAVE_READV
|
---|
155 | nn = readv(so->s, (struct iovec *)iov, n);
|
---|
156 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
157 | #else
|
---|
158 | nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
|
---|
159 | #endif
|
---|
160 | if (nn <= 0) {
|
---|
161 | if (nn < 0 && (errno == EINTR || errno == EAGAIN))
|
---|
162 | return 0;
|
---|
163 | else {
|
---|
164 | DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
|
---|
165 | sofcantrcvmore(so);
|
---|
166 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
167 | return -1;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | #ifndef HAVE_READV
|
---|
172 | /*
|
---|
173 | * If there was no error, try and read the second time round
|
---|
174 | * We read again if n = 2 (ie, there's another part of the buffer)
|
---|
175 | * and we read as much as we could in the first read
|
---|
176 | * We don't test for <= 0 this time, because there legitimately
|
---|
177 | * might not be any more data (since the socket is non-blocking),
|
---|
178 | * a close will be detected on next iteration.
|
---|
179 | * A return of -1 wont (shouldn't) happen, since it didn't happen above
|
---|
180 | */
|
---|
181 | if (n == 2 && nn == iov[0].iov_len) {
|
---|
182 | int ret;
|
---|
183 | ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
|
---|
184 | if (ret > 0)
|
---|
185 | nn += ret;
|
---|
186 | }
|
---|
187 |
|
---|
188 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | /* Update fields */
|
---|
192 | sb->sb_cc += nn;
|
---|
193 | sb->sb_wptr += nn;
|
---|
194 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
|
---|
195 | sb->sb_wptr -= sb->sb_datalen;
|
---|
196 | return nn;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Get urgent data
|
---|
201 | *
|
---|
202 | * When the socket is created, we set it SO_OOBINLINE,
|
---|
203 | * so when OOB data arrives, we soread() it and everything
|
---|
204 | * in the send buffer is sent as urgent data
|
---|
205 | */
|
---|
206 | void
|
---|
207 | sorecvoob(PNATState pData, struct socket *so)
|
---|
208 | {
|
---|
209 | struct tcpcb *tp = sototcpcb(so);
|
---|
210 |
|
---|
211 | DEBUG_CALL("sorecvoob");
|
---|
212 | DEBUG_ARG("so = %lx", (long)so);
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * We take a guess at how much urgent data has arrived.
|
---|
216 | * In most situations, when urgent data arrives, the next
|
---|
217 | * read() should get all the urgent data. This guess will
|
---|
218 | * be wrong however if more data arrives just after the
|
---|
219 | * urgent data, or the read() doesn't return all the
|
---|
220 | * urgent data.
|
---|
221 | */
|
---|
222 | soread(pData, so);
|
---|
223 | tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
|
---|
224 | tp->t_force = 1;
|
---|
225 | tcp_output(pData, tp);
|
---|
226 | tp->t_force = 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Send urgent data
|
---|
231 | * There's a lot duplicated code here, but...
|
---|
232 | */
|
---|
233 | int
|
---|
234 | sosendoob(so)
|
---|
235 | struct socket *so;
|
---|
236 | {
|
---|
237 | struct sbuf *sb = &so->so_rcv;
|
---|
238 | char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
|
---|
239 |
|
---|
240 | int n, len;
|
---|
241 |
|
---|
242 | DEBUG_CALL("sosendoob");
|
---|
243 | DEBUG_ARG("so = %lx", (long)so);
|
---|
244 | DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
|
---|
245 |
|
---|
246 | if (so->so_urgc > 2048)
|
---|
247 | so->so_urgc = 2048; /* XXXX */
|
---|
248 |
|
---|
249 | if (sb->sb_rptr < sb->sb_wptr) {
|
---|
250 | /* We can send it directly */
|
---|
251 | n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
252 | so->so_urgc -= n;
|
---|
253 |
|
---|
254 | DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
|
---|
255 | } else {
|
---|
256 | /*
|
---|
257 | * Since there's no sendv or sendtov like writev,
|
---|
258 | * we must copy all data to a linear buffer then
|
---|
259 | * send it all
|
---|
260 | */
|
---|
261 | len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
262 | if (len > so->so_urgc) len = so->so_urgc;
|
---|
263 | memcpy(buff, sb->sb_rptr, len);
|
---|
264 | so->so_urgc -= len;
|
---|
265 | if (so->so_urgc) {
|
---|
266 | n = sb->sb_wptr - sb->sb_data;
|
---|
267 | if (n > so->so_urgc) n = so->so_urgc;
|
---|
268 | memcpy((buff + len), sb->sb_data, n);
|
---|
269 | so->so_urgc -= n;
|
---|
270 | len += n;
|
---|
271 | }
|
---|
272 | n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
273 | #ifdef DEBUG
|
---|
274 | if (n != len)
|
---|
275 | DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
|
---|
276 | #endif
|
---|
277 | DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
|
---|
278 | }
|
---|
279 |
|
---|
280 | sb->sb_cc -= n;
|
---|
281 | sb->sb_rptr += n;
|
---|
282 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
283 | sb->sb_rptr -= sb->sb_datalen;
|
---|
284 |
|
---|
285 | return n;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * Write data from so_rcv to so's socket,
|
---|
290 | * updating all sbuf field as necessary
|
---|
291 | */
|
---|
292 | int
|
---|
293 | sowrite(PNATState pData, struct socket *so)
|
---|
294 | {
|
---|
295 | int n,nn;
|
---|
296 | struct sbuf *sb = &so->so_rcv;
|
---|
297 | int len = sb->sb_cc;
|
---|
298 | struct iovec iov[2];
|
---|
299 |
|
---|
300 | DEBUG_CALL("sowrite");
|
---|
301 | DEBUG_ARG("so = %lx", (long)so);
|
---|
302 |
|
---|
303 | if (so->so_urgc) {
|
---|
304 | sosendoob(so);
|
---|
305 | if (sb->sb_cc == 0)
|
---|
306 | return 0;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * No need to check if there's something to write,
|
---|
311 | * sowrite wouldn't have been called otherwise
|
---|
312 | */
|
---|
313 |
|
---|
314 | len = sb->sb_cc;
|
---|
315 |
|
---|
316 | iov[0].iov_base = sb->sb_rptr;
|
---|
317 | iov[1].iov_base = 0;
|
---|
318 | iov[1].iov_len = 0;
|
---|
319 | if (sb->sb_rptr < sb->sb_wptr) {
|
---|
320 | iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
|
---|
321 | /* Should never succeed, but... */
|
---|
322 | if (iov[0].iov_len > len) iov[0].iov_len = len;
|
---|
323 | n = 1;
|
---|
324 | } else {
|
---|
325 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
326 | if (iov[0].iov_len > len) iov[0].iov_len = len;
|
---|
327 | len -= iov[0].iov_len;
|
---|
328 | if (len) {
|
---|
329 | iov[1].iov_base = sb->sb_data;
|
---|
330 | iov[1].iov_len = sb->sb_wptr - sb->sb_data;
|
---|
331 | if (iov[1].iov_len > len) iov[1].iov_len = len;
|
---|
332 | n = 2;
|
---|
333 | } else
|
---|
334 | n = 1;
|
---|
335 | }
|
---|
336 | /* Check if there's urgent data to send, and if so, send it */
|
---|
337 |
|
---|
338 | #ifdef HAVE_READV
|
---|
339 | nn = writev(so->s, (const struct iovec *)iov, n);
|
---|
340 |
|
---|
341 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
342 | #else
|
---|
343 | nn = send(so->s, iov[0].iov_base, iov[0].iov_len,0);
|
---|
344 | #endif
|
---|
345 | /* This should never happen, but people tell me it does *shrug* */
|
---|
346 | if (nn < 0 && (errno == EAGAIN || errno == EINTR))
|
---|
347 | return 0;
|
---|
348 |
|
---|
349 | if (nn <= 0) {
|
---|
350 | DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
|
---|
351 | so->so_state, errno));
|
---|
352 | sofcantsendmore(so);
|
---|
353 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
354 | return -1;
|
---|
355 | }
|
---|
356 |
|
---|
357 | #ifndef HAVE_READV
|
---|
358 | if (n == 2 && nn == iov[0].iov_len) {
|
---|
359 | int ret;
|
---|
360 | ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
|
---|
361 | if (ret > 0)
|
---|
362 | nn += ret;
|
---|
363 | }
|
---|
364 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
365 | #endif
|
---|
366 |
|
---|
367 | /* Update sbuf */
|
---|
368 | sb->sb_cc -= nn;
|
---|
369 | sb->sb_rptr += nn;
|
---|
370 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
371 | sb->sb_rptr -= sb->sb_datalen;
|
---|
372 |
|
---|
373 | /*
|
---|
374 | * If in DRAIN mode, and there's no more data, set
|
---|
375 | * it CANTSENDMORE
|
---|
376 | */
|
---|
377 | if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
|
---|
378 | sofcantsendmore(so);
|
---|
379 |
|
---|
380 | return nn;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /*
|
---|
384 | * recvfrom() a UDP socket
|
---|
385 | */
|
---|
386 | void
|
---|
387 | sorecvfrom(PNATState pData, struct socket *so)
|
---|
388 | {
|
---|
389 | struct sockaddr_in addr;
|
---|
390 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
391 |
|
---|
392 | DEBUG_CALL("sorecvfrom");
|
---|
393 | DEBUG_ARG("so = %lx", (long)so);
|
---|
394 |
|
---|
395 | if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
|
---|
396 | char buff[256];
|
---|
397 | int len;
|
---|
398 |
|
---|
399 | len = recvfrom(so->s, buff, 256, 0,
|
---|
400 | (struct sockaddr *)&addr, &addrlen);
|
---|
401 | /* XXX Check if reply is "correct"? */
|
---|
402 |
|
---|
403 | if(len == -1 || len == 0) {
|
---|
404 | u_char code=ICMP_UNREACH_PORT;
|
---|
405 |
|
---|
406 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
|
---|
407 | else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
|
---|
408 |
|
---|
409 | DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
|
---|
410 | errno,strerror(errno)));
|
---|
411 | icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
412 | } else {
|
---|
413 | icmp_reflect(pData, so->so_m);
|
---|
414 | so->so_m = 0; /* Don't m_free() it again! */
|
---|
415 | }
|
---|
416 | /* No need for this socket anymore, udp_detach it */
|
---|
417 | udp_detach(pData, so);
|
---|
418 | } else { /* A "normal" UDP packet */
|
---|
419 | struct mbuf *m;
|
---|
420 | int len, n;
|
---|
421 |
|
---|
422 | if (!(m = m_get(pData))) return;
|
---|
423 | m->m_data += if_maxlinkhdr;
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * XXX Shouldn't FIONREAD packets destined for port 53,
|
---|
427 | * but I don't know the max packet size for DNS lookups
|
---|
428 | */
|
---|
429 | len = M_FREEROOM(m);
|
---|
430 | /* if (so->so_fport != htons(53)) { */
|
---|
431 | ioctlsocket(so->s, FIONREAD, &n);
|
---|
432 |
|
---|
433 | if (n > len) {
|
---|
434 | n = (m->m_data - m->m_dat) + m->m_len + n + 1;
|
---|
435 | m_inc(m, n);
|
---|
436 | len = M_FREEROOM(m);
|
---|
437 | }
|
---|
438 | /* } */
|
---|
439 |
|
---|
440 | m->m_len = recvfrom(so->s, m->m_data, len, 0,
|
---|
441 | (struct sockaddr *)&addr, &addrlen);
|
---|
442 | DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
|
---|
443 | m->m_len, errno,strerror(errno)));
|
---|
444 | if(m->m_len<0) {
|
---|
445 | u_char code=ICMP_UNREACH_PORT;
|
---|
446 |
|
---|
447 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
|
---|
448 | else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
|
---|
449 |
|
---|
450 | DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
|
---|
451 | icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
452 | m_free(pData, m);
|
---|
453 | } else {
|
---|
454 | /*
|
---|
455 | * Hack: domain name lookup will be used the most for UDP,
|
---|
456 | * and since they'll only be used once there's no need
|
---|
457 | * for the 4 minute (or whatever) timeout... So we time them
|
---|
458 | * out much quicker (10 seconds for now...)
|
---|
459 | */
|
---|
460 | if (so->so_expire) {
|
---|
461 | if (so->so_fport == htons(53))
|
---|
462 | so->so_expire = curtime + SO_EXPIREFAST;
|
---|
463 | else
|
---|
464 | so->so_expire = curtime + SO_EXPIRE;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /* if (m->m_len == len) {
|
---|
468 | * m_inc(m, MINCSIZE);
|
---|
469 | * m->m_len = 0;
|
---|
470 | * }
|
---|
471 | */
|
---|
472 |
|
---|
473 | /*
|
---|
474 | * If this packet was destined for CTL_ADDR,
|
---|
475 | * make it look like that's where it came from, done by udp_output
|
---|
476 | */
|
---|
477 | udp_output(pData, so, m, &addr);
|
---|
478 | } /* rx error */
|
---|
479 | } /* if ping packet */
|
---|
480 | }
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * sendto() a socket
|
---|
484 | */
|
---|
485 | int
|
---|
486 | sosendto(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
487 | {
|
---|
488 | int ret;
|
---|
489 | struct sockaddr_in addr;
|
---|
490 | #if 0
|
---|
491 | struct sockaddr_in host_addr;
|
---|
492 | #endif
|
---|
493 |
|
---|
494 | DEBUG_CALL("sosendto");
|
---|
495 | DEBUG_ARG("so = %lx", (long)so);
|
---|
496 | DEBUG_ARG("m = %lx", (long)m);
|
---|
497 |
|
---|
498 | addr.sin_family = AF_INET;
|
---|
499 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr) {
|
---|
500 | /* It's an alias */
|
---|
501 | uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
502 | switch(last_byte) {
|
---|
503 | #if 0
|
---|
504 | /* handle this case at 'default:' */
|
---|
505 | case CTL_BROADCAST:
|
---|
506 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
507 | # if 0
|
---|
508 | /* Send the packet to host to fully emulate broadcast */
|
---|
509 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
510 | * the packet twice for some reason. And I cannot find any place
|
---|
511 | * in the man pages which states that sending a broadcast does not
|
---|
512 | * reach the host itself. */
|
---|
513 | host_addr.sin_family = AF_INET;
|
---|
514 | host_addr.sin_port = so->so_fport;
|
---|
515 | host_addr.sin_addr = our_addr;
|
---|
516 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
517 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
518 | # endif
|
---|
519 | break;
|
---|
520 | #endif
|
---|
521 | case CTL_DNS:
|
---|
522 | if (!get_dns_addr(pData, &dns_addr))
|
---|
523 | addr.sin_addr = dns_addr;
|
---|
524 | else
|
---|
525 | addr.sin_addr = loopback_addr;
|
---|
526 | break;
|
---|
527 | case CTL_ALIAS:
|
---|
528 | default:
|
---|
529 | if (last_byte == ~pData->netmask)
|
---|
530 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
531 | else
|
---|
532 | addr.sin_addr = loopback_addr;
|
---|
533 | break;
|
---|
534 | }
|
---|
535 | } else
|
---|
536 | addr.sin_addr = so->so_faddr;
|
---|
537 | addr.sin_port = so->so_fport;
|
---|
538 |
|
---|
539 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
---|
540 |
|
---|
541 | /* Don't care what port we get */
|
---|
542 | ret = sendto(so->s, m->m_data, m->m_len, 0,
|
---|
543 | (struct sockaddr *)&addr, sizeof (struct sockaddr));
|
---|
544 | if (ret < 0)
|
---|
545 | return -1;
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * Kill the socket if there's no reply in 4 minutes,
|
---|
549 | * but only if it's an expirable socket
|
---|
550 | */
|
---|
551 | if (so->so_expire)
|
---|
552 | so->so_expire = curtime + SO_EXPIRE;
|
---|
553 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
554 | return 0;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * XXX This should really be tcp_listen
|
---|
559 | */
|
---|
560 | struct socket *
|
---|
561 | solisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
562 | {
|
---|
563 | struct sockaddr_in addr;
|
---|
564 | struct socket *so;
|
---|
565 | socklen_t addrlen = sizeof(addr);
|
---|
566 | int s, opt = 1;
|
---|
567 |
|
---|
568 | DEBUG_CALL("solisten");
|
---|
569 | DEBUG_ARG("port = %d", port);
|
---|
570 | DEBUG_ARG("laddr = %x", laddr);
|
---|
571 | DEBUG_ARG("lport = %d", lport);
|
---|
572 | DEBUG_ARG("flags = %x", flags);
|
---|
573 |
|
---|
574 | if ((so = socreate()) == NULL) {
|
---|
575 | /* free(so); Not sofree() ??? free(NULL) == NOP */
|
---|
576 | return NULL;
|
---|
577 | }
|
---|
578 |
|
---|
579 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
580 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL) {
|
---|
581 | free(so);
|
---|
582 | return NULL;
|
---|
583 | }
|
---|
584 | insque(pData, so,&tcb);
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * SS_FACCEPTONCE sockets must time out.
|
---|
588 | */
|
---|
589 | if (flags & SS_FACCEPTONCE)
|
---|
590 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
591 |
|
---|
592 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
593 | so->so_lport = lport; /* Kept in network format */
|
---|
594 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
595 |
|
---|
596 | addr.sin_family = AF_INET;
|
---|
597 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
598 | addr.sin_port = port;
|
---|
599 |
|
---|
600 | if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) ||
|
---|
601 | (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
|
---|
602 | (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
|
---|
603 | (listen(s,1) < 0)) {
|
---|
604 | #ifdef RT_OS_WINDOWS
|
---|
605 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
606 | closesocket(s);
|
---|
607 | sofree(pData, so);
|
---|
608 | /* Restore the real errno */
|
---|
609 | WSASetLastError(tmperrno);
|
---|
610 | #else
|
---|
611 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
612 | close(s);
|
---|
613 | sofree(pData, so);
|
---|
614 | /* Restore the real errno */
|
---|
615 | errno = tmperrno;
|
---|
616 | #endif
|
---|
617 | return NULL;
|
---|
618 | }
|
---|
619 | setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
|
---|
620 |
|
---|
621 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
622 | so->so_fport = addr.sin_port;
|
---|
623 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
624 | so->so_faddr = alias_addr;
|
---|
625 | else
|
---|
626 | so->so_faddr = addr.sin_addr;
|
---|
627 |
|
---|
628 | so->s = s;
|
---|
629 | return so;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Data is available in so_rcv
|
---|
634 | * Just write() the data to the socket
|
---|
635 | * XXX not yet...
|
---|
636 | */
|
---|
637 | void
|
---|
638 | sorwakeup(so)
|
---|
639 | struct socket *so;
|
---|
640 | {
|
---|
641 | /* sowrite(so); */
|
---|
642 | /* FD_CLR(so->s,&writefds); */
|
---|
643 | }
|
---|
644 |
|
---|
645 | /*
|
---|
646 | * Data has been freed in so_snd
|
---|
647 | * We have room for a read() if we want to
|
---|
648 | * For now, don't read, it'll be done in the main loop
|
---|
649 | */
|
---|
650 | void
|
---|
651 | sowwakeup(so)
|
---|
652 | struct socket *so;
|
---|
653 | {
|
---|
654 | /* Nothing, yet */
|
---|
655 | }
|
---|
656 |
|
---|
657 | /*
|
---|
658 | * Various session state calls
|
---|
659 | * XXX Should be #define's
|
---|
660 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
661 | * times each when only 1 was needed
|
---|
662 | */
|
---|
663 | void
|
---|
664 | soisfconnecting(so)
|
---|
665 | register struct socket *so;
|
---|
666 | {
|
---|
667 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
668 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
669 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
670 | }
|
---|
671 |
|
---|
672 | void
|
---|
673 | soisfconnected(so)
|
---|
674 | register struct socket *so;
|
---|
675 | {
|
---|
676 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
677 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
678 | }
|
---|
679 |
|
---|
680 | void
|
---|
681 | sofcantrcvmore(so)
|
---|
682 | struct socket *so;
|
---|
683 | {
|
---|
684 | if ((so->so_state & SS_NOFDREF) == 0) {
|
---|
685 | shutdown(so->s,0);
|
---|
686 | }
|
---|
687 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
688 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
689 | so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
|
---|
690 | else
|
---|
691 | so->so_state |= SS_FCANTRCVMORE;
|
---|
692 | }
|
---|
693 |
|
---|
694 | void
|
---|
695 | sofcantsendmore(so)
|
---|
696 | struct socket *so;
|
---|
697 | {
|
---|
698 | if ((so->so_state & SS_NOFDREF) == 0) {
|
---|
699 | shutdown(so->s,1); /* send FIN to fhost */
|
---|
700 | }
|
---|
701 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
702 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
703 | so->so_state = SS_NOFDREF; /* as above */
|
---|
704 | else
|
---|
705 | so->so_state |= SS_FCANTSENDMORE;
|
---|
706 | }
|
---|
707 |
|
---|
708 | void
|
---|
709 | soisfdisconnected(so)
|
---|
710 | struct socket *so;
|
---|
711 | {
|
---|
712 | /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
|
---|
713 | /* close(so->s); */
|
---|
714 | /* so->so_state = SS_ISFDISCONNECTED; */
|
---|
715 | /*
|
---|
716 | * XXX Do nothing ... ?
|
---|
717 | */
|
---|
718 | }
|
---|
719 |
|
---|
720 | /*
|
---|
721 | * Set write drain mode
|
---|
722 | * Set CANTSENDMORE once all data has been write()n
|
---|
723 | */
|
---|
724 | void
|
---|
725 | sofwdrain(so)
|
---|
726 | struct socket *so;
|
---|
727 | {
|
---|
728 | if (so->so_rcv.sb_cc)
|
---|
729 | so->so_state |= SS_FWDRAIN;
|
---|
730 | else
|
---|
731 | sofcantsendmore(so);
|
---|
732 | }
|
---|
733 |
|
---|