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
13 #include <sys/filio.h>
24 solookup(head
, laddr
, lport
, faddr
, fport
)
33 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
34 if (so
->so_lport
== lport
&&
35 so
->so_laddr
.s_addr
== laddr
.s_addr
&&
36 so
->so_faddr
.s_addr
== faddr
.s_addr
&&
37 so
->so_fport
== fport
)
42 return (struct socket
*)NULL
;
48 * Create a new socket, initialise the fields
49 * It is the responsibility of the caller to
50 * insque() it into the correct linked-list
57 so
= (struct socket
*)malloc(sizeof(struct socket
));
59 memset(so
, 0, sizeof(struct socket
));
60 so
->so_state
= SS_NOFDREF
;
67 * remque and free a socket, clobber cache
73 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
77 if (so
== tcp_last_so
)
79 else if (so
== udp_last_so
)
84 if(so
->so_next
&& so
->so_prev
)
85 remque(so
); /* crashes if so is not in a queue */
91 * Read from so's socket into sb_snd, updating all relevant sbuf fields
92 * NOTE: This will only be called if it is select()ed for reading, so
93 * a read() of 0 (or less) means it's disconnected
99 int n
, nn
, lss
, total
;
100 struct sbuf
*sb
= &so
->so_snd
;
101 int len
= sb
->sb_datalen
- sb
->sb_cc
;
103 int mss
= so
->so_tcpcb
->t_maxseg
;
105 DEBUG_CALL("soread");
106 DEBUG_ARG("so = %lx", (long )so
);
109 * No need to check if there's enough room to read.
110 * soread wouldn't have been called if there weren't
113 len
= sb
->sb_datalen
- sb
->sb_cc
;
115 iov
[0].iov_base
= sb
->sb_wptr
;
116 if (sb
->sb_wptr
< sb
->sb_rptr
) {
117 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
118 /* Should never succeed, but... */
119 if (iov
[0].iov_len
> len
)
120 iov
[0].iov_len
= len
;
121 if (iov
[0].iov_len
> mss
)
122 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
125 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
126 /* Should never succeed, but... */
127 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
128 len
-= iov
[0].iov_len
;
130 iov
[1].iov_base
= sb
->sb_data
;
131 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
132 if(iov
[1].iov_len
> len
)
133 iov
[1].iov_len
= len
;
134 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
137 if (iov
[1].iov_len
> lss
) {
138 iov
[1].iov_len
-= lss
;
141 lss
-= iov
[1].iov_len
;
142 iov
[0].iov_len
-= lss
;
148 if (iov
[0].iov_len
> mss
)
149 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
155 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
156 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
158 nn
= recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
161 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
164 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
166 tcp_sockclosed(sototcpcb(so
));
173 * If there was no error, try and read the second time round
174 * We read again if n = 2 (ie, there's another part of the buffer)
175 * and we read as much as we could in the first read
176 * We don't test for <= 0 this time, because there legitimately
177 * might not be any more data (since the socket is non-blocking),
178 * a close will be detected on next iteration.
179 * A return of -1 wont (shouldn't) happen, since it didn't happen above
181 if (n
== 2 && nn
== iov
[0].iov_len
) {
183 ret
= recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
188 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
194 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
195 sb
->sb_wptr
-= sb
->sb_datalen
;
202 * When the socket is created, we set it SO_OOBINLINE,
203 * so when OOB data arrives, we soread() it and everything
204 * in the send buffer is sent as urgent data
210 struct tcpcb
*tp
= sototcpcb(so
);
212 DEBUG_CALL("sorecvoob");
213 DEBUG_ARG("so = %lx", (long)so
);
216 * We take a guess at how much urgent data has arrived.
217 * In most situations, when urgent data arrives, the next
218 * read() should get all the urgent data. This guess will
219 * be wrong however if more data arrives just after the
220 * urgent data, or the read() doesn't return all the
224 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
232 * There's a lot duplicated code here, but...
238 struct sbuf
*sb
= &so
->so_rcv
;
239 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
243 DEBUG_CALL("sosendoob");
244 DEBUG_ARG("so = %lx", (long)so
);
245 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
247 if (so
->so_urgc
> 2048)
248 so
->so_urgc
= 2048; /* XXXX */
250 if (sb
->sb_rptr
< sb
->sb_wptr
) {
251 /* We can send it directly */
252 n
= send(so
->s
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
255 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
258 * Since there's no sendv or sendtov like writev,
259 * we must copy all data to a linear buffer then
262 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
263 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
264 memcpy(buff
, sb
->sb_rptr
, len
);
267 n
= sb
->sb_wptr
- sb
->sb_data
;
268 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
269 memcpy((buff
+ len
), sb
->sb_data
, n
);
273 n
= send(so
->s
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
276 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
278 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
283 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
284 sb
->sb_rptr
-= sb
->sb_datalen
;
290 * Write data from so_rcv to so's socket,
291 * updating all sbuf field as necessary
298 struct sbuf
*sb
= &so
->so_rcv
;
302 DEBUG_CALL("sowrite");
303 DEBUG_ARG("so = %lx", (long)so
);
312 * No need to check if there's something to write,
313 * sowrite wouldn't have been called otherwise
318 iov
[0].iov_base
= sb
->sb_rptr
;
319 if (sb
->sb_rptr
< sb
->sb_wptr
) {
320 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
321 /* Should never succeed, but... */
322 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
325 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
326 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
327 len
-= iov
[0].iov_len
;
329 iov
[1].iov_base
= sb
->sb_data
;
330 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
331 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
336 /* Check if there's urgent data to send, and if so, send it */
339 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
341 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
343 nn
= send(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
345 /* This should never happen, but people tell me it does *shrug* */
346 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
350 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
351 so
->so_state
, errno
));
353 tcp_sockclosed(sototcpcb(so
));
358 if (n
== 2 && nn
== iov
[0].iov_len
) {
360 ret
= send(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
364 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
370 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
371 sb
->sb_rptr
-= sb
->sb_datalen
;
374 * If in DRAIN mode, and there's no more data, set
377 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
384 * recvfrom() a UDP socket
390 struct sockaddr_in addr
;
391 int addrlen
= sizeof(struct sockaddr_in
);
393 DEBUG_CALL("sorecvfrom");
394 DEBUG_ARG("so = %lx", (long)so
);
396 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
400 len
= recvfrom(so
->s
, buff
, 256, 0,
401 (struct sockaddr
*)&addr
, &addrlen
);
402 /* XXX Check if reply is "correct"? */
404 if(len
== -1 || len
== 0) {
405 u_char code
=ICMP_UNREACH_PORT
;
407 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
408 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
410 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
411 errno
,strerror(errno
)));
412 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
414 icmp_reflect(so
->so_m
);
415 so
->so_m
= 0; /* Don't m_free() it again! */
417 /* No need for this socket anymore, udp_detach it */
419 } else { /* A "normal" UDP packet */
423 if (!(m
= m_get())) return;
424 m
->m_data
+= if_maxlinkhdr
;
427 * XXX Shouldn't FIONREAD packets destined for port 53,
428 * but I don't know the max packet size for DNS lookups
431 /* if (so->so_fport != htons(53)) { */
432 ioctlsocket(so
->s
, FIONREAD
, &n
);
435 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
441 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
442 (struct sockaddr
*)&addr
, &addrlen
);
443 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
444 m
->m_len
, errno
,strerror(errno
)));
446 u_char code
=ICMP_UNREACH_PORT
;
448 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
449 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
451 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
452 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
456 * Hack: domain name lookup will be used the most for UDP,
457 * and since they'll only be used once there's no need
458 * for the 4 minute (or whatever) timeout... So we time them
459 * out much quicker (10 seconds for now...)
462 if (so
->so_fport
== htons(53))
463 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
465 so
->so_expire
= curtime
+ SO_EXPIRE
;
468 /* if (m->m_len == len) {
469 * m_inc(m, MINCSIZE);
475 * If this packet was destined for CTL_ADDR,
476 * make it look like that's where it came from, done by udp_output
478 udp_output(so
, m
, &addr
);
480 } /* if ping packet */
492 struct sockaddr_in addr
;
494 DEBUG_CALL("sosendto");
495 DEBUG_ARG("so = %lx", (long)so
);
496 DEBUG_ARG("m = %lx", (long)m
);
498 addr
.sin_family
= AF_INET
;
499 if ((so
->so_faddr
.s_addr
& htonl(0xffffff00)) == special_addr
.s_addr
) {
501 switch(ntohl(so
->so_faddr
.s_addr
) & 0xff) {
503 addr
.sin_addr
= dns_addr
;
507 addr
.sin_addr
= loopback_addr
;
511 addr
.sin_addr
= so
->so_faddr
;
512 addr
.sin_port
= so
->so_fport
;
514 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
)));
516 /* Don't care what port we get */
517 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
518 (struct sockaddr
*)&addr
, sizeof (struct sockaddr
));
523 * Kill the socket if there's no reply in 4 minutes,
524 * but only if it's an expirable socket
527 so
->so_expire
= curtime
+ SO_EXPIRE
;
528 so
->so_state
= SS_ISFCONNECTED
; /* So that it gets select()ed */
533 * XXX This should really be tcp_listen
536 solisten(port
, laddr
, lport
, flags
)
542 struct sockaddr_in addr
;
544 int s
, addrlen
= sizeof(addr
), opt
= 1;
546 DEBUG_CALL("solisten");
547 DEBUG_ARG("port = %d", port
);
548 DEBUG_ARG("laddr = %x", laddr
);
549 DEBUG_ARG("lport = %d", lport
);
550 DEBUG_ARG("flags = %x", flags
);
552 if ((so
= socreate()) == NULL
) {
553 /* free(so); Not sofree() ??? free(NULL) == NOP */
557 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
558 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
565 * SS_FACCEPTONCE sockets must time out.
567 if (flags
& SS_FACCEPTONCE
)
568 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
570 so
->so_state
= (SS_FACCEPTCONN
|flags
);
571 so
->so_lport
= lport
; /* Kept in network format */
572 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
574 addr
.sin_family
= AF_INET
;
575 addr
.sin_addr
.s_addr
= INADDR_ANY
;
576 addr
.sin_port
= port
;
578 if (((s
= socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
579 (setsockopt(s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int)) < 0) ||
580 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
582 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
586 /* Restore the real errno */
588 WSASetLastError(tmperrno
);
594 setsockopt(s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
596 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
597 so
->so_fport
= addr
.sin_port
;
598 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
599 so
->so_faddr
= alias_addr
;
601 so
->so_faddr
= addr
.sin_addr
;
608 * Data is available in so_rcv
609 * Just write() the data to the socket
617 /* FD_CLR(so->s,&writefds); */
621 * Data has been freed in so_snd
622 * We have room for a read() if we want to
623 * For now, don't read, it'll be done in the main loop
633 * Various session state calls
634 * XXX Should be #define's
635 * The socket state stuff needs work, these often get call 2 or 3
636 * times each when only 1 was needed
640 register struct socket
*so
;
642 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
643 SS_FCANTSENDMORE
|SS_FWDRAIN
);
644 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
649 register struct socket
*so
;
651 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
652 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
659 if ((so
->so_state
& SS_NOFDREF
) == 0) {
661 if(global_writefds
) {
662 FD_CLR(so
->s
,global_writefds
);
665 so
->so_state
&= ~(SS_ISFCONNECTING
);
666 if (so
->so_state
& SS_FCANTSENDMORE
)
667 so
->so_state
= SS_NOFDREF
; /* Don't select it */ /* XXX close() here as well? */
669 so
->so_state
|= SS_FCANTRCVMORE
;
676 if ((so
->so_state
& SS_NOFDREF
) == 0) {
677 shutdown(so
->s
,1); /* send FIN to fhost */
678 if (global_readfds
) {
679 FD_CLR(so
->s
,global_readfds
);
682 FD_CLR(so
->s
,global_xfds
);
685 so
->so_state
&= ~(SS_ISFCONNECTING
);
686 if (so
->so_state
& SS_FCANTRCVMORE
)
687 so
->so_state
= SS_NOFDREF
; /* as above */
689 so
->so_state
|= SS_FCANTSENDMORE
;
693 soisfdisconnected(so
)
696 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
698 /* so->so_state = SS_ISFDISCONNECTED; */
700 * XXX Do nothing ... ?
705 * Set write drain mode
706 * Set CANTSENDMORE once all data has been write()n
712 if (so
->so_rcv
.sb_cc
)
713 so
->so_state
|= SS_FWDRAIN
;