2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #define WANT_SYS_IOCTL_H
12 #include <sys/filio.h>
15 static void sofcantrcvmore(struct socket *so);
16 static void sofcantsendmore(struct socket *so);
27 solookup(head, laddr, lport, faddr, fport)
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)
45 return (struct socket *)NULL;
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
60 so = (struct socket *)malloc(sizeof(struct socket));
62 memset(so, 0, sizeof(struct socket));
63 so->so_state = SS_NOFDREF;
70 * remque and free a socket, clobber cache
76 if (so->so_emu==EMU_RSH && so->extra) {
80 if (so == tcp_last_so)
82 else if (so == udp_last_so)
87 if(so->so_next && so->so_prev)
88 remque(so); /* crashes if so is not in a queue */
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
102 int n, nn, lss, total;
103 struct sbuf *sb = &so->so_snd;
104 int len = sb->sb_datalen - sb->sb_cc;
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;
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;
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;
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;
142 if (iov[1].iov_len > lss) {
143 iov[1].iov_len -= lss;
146 lss -= iov[1].iov_len;
147 iov[0].iov_len -= lss;
153 if (iov[0].iov_len > mss)
154 iov[0].iov_len -= iov[0].iov_len%mss;
160 nn = readv(so->s, (struct iovec *)iov, n);
161 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
163 nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
166 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
169 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
171 tcp_sockclosed(sototcpcb(so));
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) {
188 ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
193 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
199 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
200 sb->sb_wptr -= sb->sb_datalen;
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
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
229 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
237 * There's a lot duplicated code here, but...
243 struct sbuf *sb = &so->so_rcv;
244 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
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)); */
260 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
263 * Since there's no sendv or sendtov like writev,
264 * we must copy all data to a linear buffer then
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);
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);
278 n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
281 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
283 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
288 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
289 sb->sb_rptr -= sb->sb_datalen;
295 * Write data from so_rcv to so's socket,
296 * updating all sbuf field as necessary
303 struct sbuf *sb = &so->so_rcv;
307 DEBUG_CALL("sowrite");
308 DEBUG_ARG("so = %lx", (long)so);
317 * No need to check if there's something to write,
318 * sowrite wouldn't have been called otherwise
323 iov[0].iov_base = sb->sb_rptr;
324 iov[1].iov_base = NULL;
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;
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;
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;
343 /* Check if there's urgent data to send, and if so, send it */
346 nn = writev(so->s, (const struct iovec *)iov, n);
348 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
350 nn = send(so->s, iov[0].iov_base, iov[0].iov_len,0);
352 /* This should never happen, but people tell me it does *shrug* */
353 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
357 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
358 so->so_state, errno));
360 tcp_sockclosed(sototcpcb(so));
365 if (n == 2 && nn == iov[0].iov_len) {
367 ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
371 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", 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
384 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
391 * recvfrom() a UDP socket
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 */
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));
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 */
426 } else { /* A "normal" UDP packet */
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
438 /* if (so->so_fport != htons(53)) { */
439 ioctlsocket(so->s, FIONREAD, &n);
442 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
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)));
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));
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...)
469 if (so->so_fport == htons(53))
470 so->so_expire = curtime + SO_EXPIREFAST;
472 so->so_expire = curtime + SO_EXPIRE;
475 /* if (m->m_len == len) {
476 * m_inc(m, MINCSIZE);
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);
487 } /* if ping packet */
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) {
508 switch(ntohl(so->so_faddr.s_addr) & 0xff) {
510 addr.sin_addr = dns_addr;
514 addr.sin_addr = loopback_addr;
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));
530 * Kill the socket if there's no reply in 4 minutes,
531 * but only if it's an expirable socket
534 so->so_expire = curtime + SO_EXPIRE;
535 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
540 * XXX This should really be tcp_listen
543 solisten(port, laddr, lport, flags)
549 struct sockaddr_in addr;
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 */
565 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
566 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
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) ||
590 int tmperrno = errno; /* Don't clobber the real reason we failed */
594 /* Restore the real errno */
596 WSASetLastError(tmperrno);
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;
609 so->so_faddr = addr.sin_addr;
617 * Data is available in so_rcv
618 * Just write() the data to the socket
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
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
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 */
659 register struct socket *so;
661 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
662 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
666 sofcantrcvmore(struct socket *so)
668 if ((so->so_state & SS_NOFDREF) == 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? */
678 so->so_state |= SS_FCANTRCVMORE;
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);
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 */
697 so->so_state |= SS_FCANTSENDMORE;
701 soisfdisconnected(so)
704 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
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
720 if (so->so_rcv.sb_cc)
721 so->so_state |= SS_FWDRAIN;