Build fix for !CONFIG_GDBSTUB case
[qemu-kvm/fedora.git] / slirp / socket.c
blob7bc0dc5f325a97aa6f61d3554ec18deff51ec993
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
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
15 static void sofcantrcvmore(struct socket *so);
16 static void sofcantsendmore(struct socket *so);
18 #if 0
19 static void
20 so_init()
22 /* Nothing yet */
24 #endif
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;
34 struct socket *so;
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;
44 if (so == head)
45 return (struct socket *)NULL;
46 return so;
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
55 struct socket *
56 socreate()
58 struct socket *so;
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;
66 return(so);
70 * remque and free a socket, clobber cache
72 void
73 sofree(so)
74 struct socket *so;
76 if (so->so_emu==EMU_RSH && so->extra) {
77 sofree(so->extra);
78 so->extra=NULL;
80 if (so == tcp_last_so)
81 tcp_last_so = &tcb;
82 else if (so == udp_last_so)
83 udp_last_so = &udb;
85 m_free(so->so_m);
87 if(so->so_next && so->so_prev)
88 remque(so); /* crashes if so is not in a queue */
90 free(so);
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
98 int
99 soread(so)
100 struct socket *so;
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;
108 DEBUG_CALL("soread");
109 DEBUG_ARG("so = %lx", (long )so);
112 * No need to check if there's enough room to read.
113 * soread wouldn't have been called if there weren't
116 len = sb->sb_datalen - sb->sb_cc;
118 iov[0].iov_base = sb->sb_wptr;
119 iov[1].iov_base = NULL;
120 iov[1].iov_len = 0;
121 if (sb->sb_wptr < sb->sb_rptr) {
122 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
123 /* Should never succeed, but... */
124 if (iov[0].iov_len > len)
125 iov[0].iov_len = len;
126 if (iov[0].iov_len > mss)
127 iov[0].iov_len -= iov[0].iov_len%mss;
128 n = 1;
129 } else {
130 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
131 /* Should never succeed, but... */
132 if (iov[0].iov_len > len) iov[0].iov_len = len;
133 len -= iov[0].iov_len;
134 if (len) {
135 iov[1].iov_base = sb->sb_data;
136 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
137 if(iov[1].iov_len > len)
138 iov[1].iov_len = len;
139 total = iov[0].iov_len + iov[1].iov_len;
140 if (total > mss) {
141 lss = total%mss;
142 if (iov[1].iov_len > lss) {
143 iov[1].iov_len -= lss;
144 n = 2;
145 } else {
146 lss -= iov[1].iov_len;
147 iov[0].iov_len -= lss;
148 n = 1;
150 } else
151 n = 2;
152 } else {
153 if (iov[0].iov_len > mss)
154 iov[0].iov_len -= iov[0].iov_len%mss;
155 n = 1;
159 #ifdef HAVE_READV
160 nn = readv(so->s, (struct iovec *)iov, n);
161 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
162 #else
163 nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
164 #endif
165 if (nn <= 0) {
166 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
167 return 0;
168 else {
169 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
170 sofcantrcvmore(so);
171 tcp_sockclosed(sototcpcb(so));
172 return -1;
176 #ifndef HAVE_READV
178 * If there was no error, try and read the second time round
179 * We read again if n = 2 (ie, there's another part of the buffer)
180 * and we read as much as we could in the first read
181 * We don't test for <= 0 this time, because there legitimately
182 * might not be any more data (since the socket is non-blocking),
183 * a close will be detected on next iteration.
184 * A return of -1 wont (shouldn't) happen, since it didn't happen above
186 if (n == 2 && nn == iov[0].iov_len) {
187 int ret;
188 ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
189 if (ret > 0)
190 nn += ret;
193 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
194 #endif
196 /* Update fields */
197 sb->sb_cc += nn;
198 sb->sb_wptr += nn;
199 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
200 sb->sb_wptr -= sb->sb_datalen;
201 return nn;
205 * Get urgent data
207 * When the socket is created, we set it SO_OOBINLINE,
208 * so when OOB data arrives, we soread() it and everything
209 * in the send buffer is sent as urgent data
211 void
212 sorecvoob(so)
213 struct socket *so;
215 struct tcpcb *tp = sototcpcb(so);
217 DEBUG_CALL("sorecvoob");
218 DEBUG_ARG("so = %lx", (long)so);
221 * We take a guess at how much urgent data has arrived.
222 * In most situations, when urgent data arrives, the next
223 * read() should get all the urgent data. This guess will
224 * be wrong however if more data arrives just after the
225 * urgent data, or the read() doesn't return all the
226 * urgent data.
228 soread(so);
229 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
230 tp->t_force = 1;
231 tcp_output(tp);
232 tp->t_force = 0;
236 * Send urgent data
237 * There's a lot duplicated code here, but...
240 sosendoob(so)
241 struct socket *so;
243 struct sbuf *sb = &so->so_rcv;
244 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
246 int n, len;
248 DEBUG_CALL("sosendoob");
249 DEBUG_ARG("so = %lx", (long)so);
250 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
252 if (so->so_urgc > 2048)
253 so->so_urgc = 2048; /* XXXX */
255 if (sb->sb_rptr < sb->sb_wptr) {
256 /* We can send it directly */
257 n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
258 so->so_urgc -= n;
260 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
261 } else {
263 * Since there's no sendv or sendtov like writev,
264 * we must copy all data to a linear buffer then
265 * send it all
267 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
268 if (len > so->so_urgc) len = so->so_urgc;
269 memcpy(buff, sb->sb_rptr, len);
270 so->so_urgc -= len;
271 if (so->so_urgc) {
272 n = sb->sb_wptr - sb->sb_data;
273 if (n > so->so_urgc) n = so->so_urgc;
274 memcpy((buff + len), sb->sb_data, n);
275 so->so_urgc -= n;
276 len += n;
278 n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
279 #ifdef DEBUG
280 if (n != len)
281 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
282 #endif
283 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
286 sb->sb_cc -= n;
287 sb->sb_rptr += n;
288 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
289 sb->sb_rptr -= sb->sb_datalen;
291 return n;
295 * Write data from so_rcv to so's socket,
296 * updating all sbuf field as necessary
299 sowrite(so)
300 struct socket *so;
302 int n,nn;
303 struct sbuf *sb = &so->so_rcv;
304 int len = sb->sb_cc;
305 struct iovec iov[2];
307 DEBUG_CALL("sowrite");
308 DEBUG_ARG("so = %lx", (long)so);
310 if (so->so_urgc) {
311 sosendoob(so);
312 if (sb->sb_cc == 0)
313 return 0;
317 * No need to check if there's something to write,
318 * sowrite wouldn't have been called otherwise
321 len = sb->sb_cc;
323 iov[0].iov_base = sb->sb_rptr;
324 iov[1].iov_base = NULL;
325 iov[1].iov_len = 0;
326 if (sb->sb_rptr < sb->sb_wptr) {
327 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
328 /* Should never succeed, but... */
329 if (iov[0].iov_len > len) iov[0].iov_len = len;
330 n = 1;
331 } else {
332 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
333 if (iov[0].iov_len > len) iov[0].iov_len = len;
334 len -= iov[0].iov_len;
335 if (len) {
336 iov[1].iov_base = sb->sb_data;
337 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
338 if (iov[1].iov_len > len) iov[1].iov_len = len;
339 n = 2;
340 } else
341 n = 1;
343 /* Check if there's urgent data to send, and if so, send it */
345 #ifdef HAVE_READV
346 nn = writev(so->s, (const struct iovec *)iov, n);
348 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
349 #else
350 nn = send(so->s, iov[0].iov_base, iov[0].iov_len,0);
351 #endif
352 /* This should never happen, but people tell me it does *shrug* */
353 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
354 return 0;
356 if (nn <= 0) {
357 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
358 so->so_state, errno));
359 sofcantsendmore(so);
360 tcp_sockclosed(sototcpcb(so));
361 return -1;
364 #ifndef HAVE_READV
365 if (n == 2 && nn == iov[0].iov_len) {
366 int ret;
367 ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
368 if (ret > 0)
369 nn += ret;
371 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
372 #endif
374 /* Update sbuf */
375 sb->sb_cc -= nn;
376 sb->sb_rptr += nn;
377 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
378 sb->sb_rptr -= sb->sb_datalen;
381 * If in DRAIN mode, and there's no more data, set
382 * it CANTSENDMORE
384 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
385 sofcantsendmore(so);
387 return nn;
391 * recvfrom() a UDP socket
393 void
394 sorecvfrom(so)
395 struct socket *so;
397 struct sockaddr_in addr;
398 socklen_t addrlen = sizeof(struct sockaddr_in);
400 DEBUG_CALL("sorecvfrom");
401 DEBUG_ARG("so = %lx", (long)so);
403 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
404 char buff[256];
405 int len;
407 len = recvfrom(so->s, buff, 256, 0,
408 (struct sockaddr *)&addr, &addrlen);
409 /* XXX Check if reply is "correct"? */
411 if(len == -1 || len == 0) {
412 u_char code=ICMP_UNREACH_PORT;
414 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
415 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
417 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
418 errno,strerror(errno)));
419 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
420 } else {
421 icmp_reflect(so->so_m);
422 so->so_m = 0; /* Don't m_free() it again! */
424 /* No need for this socket anymore, udp_detach it */
425 udp_detach(so);
426 } else { /* A "normal" UDP packet */
427 struct mbuf *m;
428 int len, n;
430 if (!(m = m_get())) return;
431 m->m_data += IF_MAXLINKHDR;
434 * XXX Shouldn't FIONREAD packets destined for port 53,
435 * but I don't know the max packet size for DNS lookups
437 len = M_FREEROOM(m);
438 /* if (so->so_fport != htons(53)) { */
439 ioctlsocket(so->s, FIONREAD, &n);
441 if (n > len) {
442 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
443 m_inc(m, n);
444 len = M_FREEROOM(m);
446 /* } */
448 m->m_len = recvfrom(so->s, m->m_data, len, 0,
449 (struct sockaddr *)&addr, &addrlen);
450 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
451 m->m_len, errno,strerror(errno)));
452 if(m->m_len<0) {
453 u_char code=ICMP_UNREACH_PORT;
455 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
456 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
458 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
459 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
460 m_free(m);
461 } else {
463 * Hack: domain name lookup will be used the most for UDP,
464 * and since they'll only be used once there's no need
465 * for the 4 minute (or whatever) timeout... So we time them
466 * out much quicker (10 seconds for now...)
468 if (so->so_expire) {
469 if (so->so_fport == htons(53))
470 so->so_expire = curtime + SO_EXPIREFAST;
471 else
472 so->so_expire = curtime + SO_EXPIRE;
475 /* if (m->m_len == len) {
476 * m_inc(m, MINCSIZE);
477 * m->m_len = 0;
482 * If this packet was destined for CTL_ADDR,
483 * make it look like that's where it came from, done by udp_output
485 udp_output(so, m, &addr);
486 } /* rx error */
487 } /* if ping packet */
491 * sendto() a socket
494 sosendto(so, m)
495 struct socket *so;
496 struct mbuf *m;
498 int ret;
499 struct sockaddr_in addr;
501 DEBUG_CALL("sosendto");
502 DEBUG_ARG("so = %lx", (long)so);
503 DEBUG_ARG("m = %lx", (long)m);
505 addr.sin_family = AF_INET;
506 if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
507 /* It's an alias */
508 switch(ntohl(so->so_faddr.s_addr) & 0xff) {
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;
517 } else
518 addr.sin_addr = so->so_faddr;
519 addr.sin_port = so->so_fport;
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)));
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;
530 * Kill the socket if there's no reply in 4 minutes,
531 * but only if it's an expirable socket
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;
540 * XXX This should really be tcp_listen
542 struct socket *
543 solisten(port, laddr, lport, flags)
544 u_int port;
545 u_int32_t laddr;
546 u_int lport;
547 int flags;
549 struct sockaddr_in addr;
550 struct socket *so;
551 int s, opt = 1;
552 socklen_t addrlen = sizeof(addr);
554 DEBUG_CALL("solisten");
555 DEBUG_ARG("port = %d", port);
556 DEBUG_ARG("laddr = %x", laddr);
557 DEBUG_ARG("lport = %d", lport);
558 DEBUG_ARG("flags = %x", flags);
560 if ((so = socreate()) == NULL) {
561 /* free(so); Not sofree() ??? free(NULL) == NOP */
562 return NULL;
565 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
566 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
567 free(so);
568 return NULL;
570 insque(so,&tcb);
573 * SS_FACCEPTONCE sockets must time out.
575 if (flags & SS_FACCEPTONCE)
576 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
578 so->so_state = (SS_FACCEPTCONN|flags);
579 so->so_lport = lport; /* Kept in network format */
580 so->so_laddr.s_addr = laddr; /* Ditto */
582 addr.sin_family = AF_INET;
583 addr.sin_addr.s_addr = INADDR_ANY;
584 addr.sin_port = port;
586 if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) ||
587 (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
588 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
589 (listen(s,1) < 0)) {
590 int tmperrno = errno; /* Don't clobber the real reason we failed */
592 close(s);
593 sofree(so);
594 /* Restore the real errno */
595 #ifdef _WIN32
596 WSASetLastError(tmperrno);
597 #else
598 errno = tmperrno;
599 #endif
600 return NULL;
602 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
604 getsockname(s,(struct sockaddr *)&addr,&addrlen);
605 so->so_fport = addr.sin_port;
606 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
607 so->so_faddr = alias_addr;
608 else
609 so->so_faddr = addr.sin_addr;
611 so->s = s;
612 return so;
615 #if 0
617 * Data is available in so_rcv
618 * Just write() the data to the socket
619 * XXX not yet...
621 static void
622 sorwakeup(so)
623 struct socket *so;
625 /* sowrite(so); */
626 /* FD_CLR(so->s,&writefds); */
630 * Data has been freed in so_snd
631 * We have room for a read() if we want to
632 * For now, don't read, it'll be done in the main loop
634 static void
635 sowwakeup(so)
636 struct socket *so;
638 /* Nothing, yet */
640 #endif
643 * Various session state calls
644 * XXX Should be #define's
645 * The socket state stuff needs work, these often get call 2 or 3
646 * times each when only 1 was needed
648 void
649 soisfconnecting(so)
650 register struct socket *so;
652 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
653 SS_FCANTSENDMORE|SS_FWDRAIN);
654 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
657 void
658 soisfconnected(so)
659 register struct socket *so;
661 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
662 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
665 static void
666 sofcantrcvmore(struct socket *so)
668 if ((so->so_state & SS_NOFDREF) == 0) {
669 shutdown(so->s,0);
670 if(global_writefds) {
671 FD_CLR(so->s,global_writefds);
674 so->so_state &= ~(SS_ISFCONNECTING);
675 if (so->so_state & SS_FCANTSENDMORE)
676 so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
677 else
678 so->so_state |= SS_FCANTRCVMORE;
681 static void
682 sofcantsendmore(struct socket *so)
684 if ((so->so_state & SS_NOFDREF) == 0) {
685 shutdown(so->s,1); /* send FIN to fhost */
686 if (global_readfds) {
687 FD_CLR(so->s,global_readfds);
689 if (global_xfds) {
690 FD_CLR(so->s,global_xfds);
693 so->so_state &= ~(SS_ISFCONNECTING);
694 if (so->so_state & SS_FCANTRCVMORE)
695 so->so_state = SS_NOFDREF; /* as above */
696 else
697 so->so_state |= SS_FCANTSENDMORE;
700 void
701 soisfdisconnected(so)
702 struct socket *so;
704 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
705 /* close(so->s); */
706 /* so->so_state = SS_ISFDISCONNECTED; */
708 * XXX Do nothing ... ?
713 * Set write drain mode
714 * Set CANTSENDMORE once all data has been write()n
716 void
717 sofwdrain(so)
718 struct socket *so;
720 if (so->so_rcv.sb_cc)
721 so->so_state |= SS_FWDRAIN;
722 else
723 sofcantsendmore(so);