loadlwipv6 inlined
[vde.git] / vde-2 / src / slirpvde / socket.c
blob676eac2e7adb354d673a5df1c14a6f59a8f94550
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
3 *
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #define WANT_SYS_IOCTL_H
9 #include <config.h>
10 #include <vde.h>
11 #include <vdecommon.h>
13 #include "slirp.h"
14 #include "ip_icmp.h"
15 #include "main.h"
17 void
18 so_init()
20 /* Nothing yet */
24 struct socket *
25 solookup(head, laddr, lport, faddr, fport)
26 struct socket *head;
27 struct in_addr laddr;
28 u_int lport;
29 struct in_addr faddr;
30 u_int fport;
32 struct socket *so;
34 for (so = head->so_next; so != head; so = so->so_next) {
35 if (so->so_lport == lport &&
36 so->so_laddr.s_addr == laddr.s_addr &&
37 so->so_faddr.s_addr == faddr.s_addr &&
38 so->so_fport == fport)
39 break;
42 if (so == head)
43 return (struct socket *)NULL;
44 return so;
49 * Create a new socket, initialise the fields
50 * It is the responsibility of the caller to
51 * insque() it into the correct linked-list
53 struct socket *
54 socreate()
56 struct socket *so;
58 so = (struct socket *)malloc(sizeof(struct socket));
59 if(so) {
60 memset(so, 0, sizeof(struct socket));
61 so->so_state = SS_NOFDREF;
62 so->s = -1;
64 return(so);
68 * remque and free a socket, clobber cache
70 void
71 sofree(so)
72 struct socket *so;
74 if (so->so_emu==EMU_RSH && so->extra) {
75 sofree(so->extra);
76 so->extra=NULL;
78 if (so == tcp_last_so)
79 tcp_last_so = &tcb;
80 else if (so == udp_last_so)
81 udp_last_so = &udb;
83 m_free(so->so_m);
85 if(so->so_next && so->so_prev)
86 remque(so); /* crashes if so is not in a queue */
88 free(so);
92 * Read from so's socket into sb_snd, updating all relevant sbuf fields
93 * NOTE: This will only be called if it is select()ed for reading, so
94 * a read() of 0 (or less) means it's disconnected
96 int
97 soread(so)
98 struct socket *so;
100 int n, nn, lss, total;
101 struct sbuf *sb = &so->so_snd;
102 int len = sb->sb_datalen - sb->sb_cc;
103 struct iovec iov[2];
104 int mss = so->so_tcpcb->t_maxseg;
106 DEBUG_CALL("soread");
107 DEBUG_ARG("so = %lx", (long )so);
110 * No need to check if there's enough room to read.
111 * soread wouldn't have been called if there weren't
114 len = sb->sb_datalen - sb->sb_cc;
116 iov[0].iov_base = sb->sb_wptr;
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;
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;
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 = read(so->s, iov[0].iov_base, iov[0].iov_len);
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(sototcpcb(so));
168 return -1;
172 #ifndef HAVE_READV
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
182 if (n == 2 && nn == iov[0].iov_len)
183 nn += read(so->s, iov[1].iov_base, iov[1].iov_len);
185 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
186 #endif
188 /* Update fields */
189 sb->sb_cc += nn;
190 sb->sb_wptr += nn;
191 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
192 sb->sb_wptr -= sb->sb_datalen;
193 return nn;
197 * Get urgent data
199 * When the socket is created, we set it SO_OOBINLINE,
200 * so when OOB data arrives, we soread() it and everything
201 * in the send buffer is sent as urgent data
203 void
204 sorecvoob(so)
205 struct socket *so;
207 struct tcpcb *tp = sototcpcb(so);
209 DEBUG_CALL("sorecvoob");
210 DEBUG_ARG("so = %lx", (long)so);
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.
220 soread(so);
221 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
222 tp->t_force = 1;
223 tcp_output(tp);
224 tp->t_force = 0;
228 * Send urgent data
229 * There's a lot duplicated code here, but...
232 sosendoob(so)
233 struct socket *so;
235 struct sbuf *sb = &so->so_rcv;
236 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
238 int n, len;
240 DEBUG_CALL("sosendoob");
241 DEBUG_ARG("so = %lx", (long)so);
242 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
244 if (so->so_urgc > 2048)
245 so->so_urgc = 2048; /* XXXX */
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;
252 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
253 } else {
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
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;
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));
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;
283 return n;
287 * Write data from so_rcv to so's socket,
288 * updating all sbuf field as necessary
291 sowrite(so)
292 struct socket *so;
294 int n,nn;
295 struct sbuf *sb = &so->so_rcv;
296 int len = sb->sb_cc;
297 struct iovec iov[2];
299 DEBUG_CALL("sowrite");
300 DEBUG_ARG("so = %lx", (long)so);
302 if (so->so_urgc) {
303 sosendoob(so);
304 if (sb->sb_cc == 0)
305 return 0;
309 * No need to check if there's something to write,
310 * sowrite wouldn't have been called otherwise
313 len = sb->sb_cc;
315 iov[0].iov_base = sb->sb_rptr;
316 if (sb->sb_rptr < sb->sb_wptr) {
317 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
318 /* Should never succeed, but... */
319 if (iov[0].iov_len > len) iov[0].iov_len = len;
320 n = 1;
321 } else {
322 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
323 if (iov[0].iov_len > len) iov[0].iov_len = len;
324 len -= iov[0].iov_len;
325 if (len) {
326 iov[1].iov_base = sb->sb_data;
327 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
328 if (iov[1].iov_len > len) iov[1].iov_len = len;
329 n = 2;
330 } else
331 n = 1;
333 /* Check if there's urgent data to send, and if so, send it */
335 #ifdef HAVE_READV
336 nn = writev(so->s, (const struct iovec *)iov, n);
338 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
339 #else
340 nn = write(so->s, iov[0].iov_base, iov[0].iov_len);
341 #endif
342 /* This should never happen, but people tell me it does *shrug* */
343 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
344 return 0;
346 if (nn <= 0) {
347 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
348 so->so_state, errno));
349 sofcantsendmore(so);
350 tcp_sockclosed(sototcpcb(so));
351 return -1;
354 #ifndef HAVE_READV
355 if (n == 2 && nn == iov[0].iov_len)
356 nn += write(so->s, iov[1].iov_base, iov[1].iov_len);
357 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
358 #endif
360 /* Update sbuf */
361 sb->sb_cc -= nn;
362 sb->sb_rptr += nn;
363 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
364 sb->sb_rptr -= sb->sb_datalen;
367 * If in DRAIN mode, and there's no more data, set
368 * it CANTSENDMORE
370 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
371 sofcantsendmore(so);
373 return nn;
377 * recvfrom() a UDP socket
379 void
380 sorecvfrom(so)
381 struct socket *so;
383 struct sockaddr_in addr;
384 socklen_t addrlen = sizeof(struct sockaddr_in);
386 DEBUG_CALL("sorecvfrom");
387 DEBUG_ARG("so = %lx", (long)so);
389 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
390 char buff[256];
391 int len;
393 len = recvfrom(so->s, buff, 256, 0,
394 (struct sockaddr *)&addr, &addrlen);
395 /* XXX Check if reply is "correct"? */
397 if(len == -1 || len == 0) {
398 u_char code=ICMP_UNREACH_PORT;
400 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
401 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
403 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
404 errno,strerror(errno)));
405 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
406 } else {
407 icmp_reflect(so->so_m);
408 so->so_m = 0; /* Don't m_free() it again! */
410 /* No need for this socket anymore, udp_detach it */
411 udp_detach(so);
412 } else { /* A "normal" UDP packet */
413 struct mbuf *m;
414 int len, n;
416 if (!(m = m_get())) return;
417 m->m_data += if_maxlinkhdr;
420 * XXX Shouldn't FIONREAD packets destined for port 53,
421 * but I don't know the max packet size for DNS lookups
423 len = M_FREEROOM(m);
424 /* if (so->so_fport != htons(53)) { */
425 ioctl(so->s, FIONREAD, &n);
427 if (n > len) {
428 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
429 m_inc(m, n);
430 len = M_FREEROOM(m);
432 /* } */
434 m->m_len = recvfrom(so->s, m->m_data, len, 0,
435 (struct sockaddr *)&addr, &addrlen);
436 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
437 m->m_len, errno,strerror(errno)));
438 if(m->m_len<0) {
439 u_char code=ICMP_UNREACH_PORT;
441 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
442 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
444 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
445 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
446 m_free(m);
447 } else {
449 * Hack: domain name lookup will be used the most for UDP,
450 * and since they'll only be used once there's no need
451 * for the 4 minute (or whatever) timeout... So we time them
452 * out much quicker (10 seconds for now...)
454 if (so->so_expire) {
455 if (so->so_fport == htons(53))
456 so->so_expire = curtime + SO_EXPIREFAST;
457 else
458 so->so_expire = curtime + SO_EXPIRE;
461 /* if (m->m_len == len) {
462 * m_inc(m, MINCSIZE);
463 * m->m_len = 0;
468 * If this packet was destined for CTL_ADDR,
469 * make it look like that's where it came from, done by udp_output
471 udp_output(so, m, &addr);
472 } /* rx error */
473 } /* if ping packet */
477 * sendto() a socket
480 sosendto(so, m)
481 struct socket *so;
482 struct mbuf *m;
484 int ret;
485 struct sockaddr_in addr;
487 DEBUG_CALL("sosendto");
488 DEBUG_ARG("so = %lx", (long)so);
489 DEBUG_ARG("m = %lx", (long)m);
491 addr.sin_family = AF_INET;
492 if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
493 /* It's an alias */
494 switch(ntohl(so->so_faddr.s_addr) & 0xff) {
495 case CTL_DNS:
496 addr.sin_addr = dns_addr;
497 break;
498 case CTL_ALIAS:
499 default:
500 addr.sin_addr = loopback_addr;
501 break;
503 } else
504 addr.sin_addr = so->so_faddr;
505 addr.sin_port = so->so_fport;
507 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)));
509 /* Don't care what port we get */
510 ret = sendto(so->s, m->m_data, m->m_len, 0,
511 (struct sockaddr *)&addr, sizeof (struct sockaddr));
512 if (ret < 0)
513 return -1;
516 * Kill the socket if there's no reply in 4 minutes,
517 * but only if it's an expirable socket
519 if (so->so_expire)
520 so->so_expire = curtime + SO_EXPIRE;
521 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
522 return 0;
526 * XXX This should really be tcp_listen
528 struct socket *
529 solisten(port, laddr, lport, flags)
530 u_int port;
531 u_int32_t laddr;
532 u_int lport;
533 int flags;
535 struct sockaddr_in addr;
536 struct socket *so;
537 int s;
538 socklen_t addrlen = sizeof(addr), opt = 1;
540 DEBUG_CALL("solisten");
541 DEBUG_ARG("port = %d", port);
542 DEBUG_ARG("laddr = %x", laddr);
543 DEBUG_ARG("lport = %d", lport);
544 DEBUG_ARG("flags = %x", flags);
546 if ((so = socreate()) == NULL) {
547 /* free(so); Not sofree() ??? free(NULL) == NOP */
548 return NULL;
551 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
552 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
553 free(so);
554 return NULL;
556 insque(so,&tcb);
559 * SS_FACCEPTONCE sockets must time out.
561 if (flags & SS_FACCEPTONCE)
562 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
564 so->so_state = (SS_FACCEPTCONN|flags);
565 so->so_lport = lport; /* Kept in network format */
566 so->so_laddr.s_addr = laddr; /* Ditto */
568 addr.sin_family = AF_INET;
569 addr.sin_addr.s_addr = INADDR_ANY;
570 addr.sin_port = port;
572 if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) ||
573 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
574 (listen(s,1) < 0)) {
575 int tmperrno = errno; /* Don't clobber the real reason we failed */
577 close(s);
578 sofree(so);
579 /* Restore the real errno */
580 errno = tmperrno;
581 return NULL;
583 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
584 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
586 getsockname(s,(struct sockaddr *)&addr,&addrlen);
587 so->so_fport = addr.sin_port;
588 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
589 so->so_faddr = our_addr;
590 else
591 so->so_faddr = addr.sin_addr;
593 so->s = s;
594 return so;
598 * Data is available in so_rcv
599 * Just write() the data to the socket
600 * XXX not yet...
602 void
603 sorwakeup(so)
604 struct socket *so;
606 /* sowrite(so); */
607 /* FD_CLR(so->s,&writefds); */
611 * Data has been freed in so_snd
612 * We have room for a read() if we want to
613 * For now, don't read, it'll be done in the main loop
615 void
616 sowwakeup(so)
617 struct socket *so;
619 /* Nothing, yet */
623 * Various session state calls
624 * XXX Should be #define's
625 * The socket state stuff needs work, these often get call 2 or 3
626 * times each when only 1 was needed
628 void
629 soisfconnecting(so)
630 register struct socket *so;
632 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
633 SS_FCANTSENDMORE|SS_FWDRAIN);
634 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
637 void
638 soisfconnected(so)
639 register struct socket *so;
641 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
642 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
645 void
646 sofcantrcvmore(so)
647 struct socket *so;
649 if ((so->so_state & SS_NOFDREF) == 0) {
650 shutdown(so->s,0);
651 if (global_writefds != NULL)
652 FD_CLR(so->s, global_writefds);
654 so->so_state &= ~(SS_ISFCONNECTING);
655 if (so->so_state & SS_FCANTSENDMORE)
656 so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
657 else
658 so->so_state |= SS_FCANTRCVMORE;
661 void
662 sofcantsendmore(so)
663 struct socket *so;
665 if ((so->so_state & SS_NOFDREF) == 0) {
666 shutdown(so->s,1); /* send FIN to fhost */
667 if (global_readfds != NULL)
668 FD_CLR(so->s, global_readfds);
669 if (global_xfds != NULL)
670 FD_CLR(so->s, global_xfds);
673 so->so_state &= ~(SS_ISFCONNECTING);
674 if (so->so_state & SS_FCANTRCVMORE)
675 so->so_state = SS_NOFDREF; /* as above */
676 else
677 so->so_state |= SS_FCANTSENDMORE;
680 void
681 soisfdisconnected(so)
682 struct socket *so;
684 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
685 /* close(so->s); */
686 /* so->so_state = SS_ISFDISCONNECTED; */
688 * XXX Do nothing ... ?
693 * Set write drain mode
694 * Set CANTSENDMORE once all data has been write()n
696 void
697 sofwdrain(so)
698 struct socket *so;
700 if (so->so_rcv.sb_cc)
701 so->so_state |= SS_FWDRAIN;
702 else
703 sofcantsendmore(so);