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