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