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