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