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
22 solookup(head
, laddr
, lport
, faddr
, fport
)
31 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
32 if (so
->so_lport
== lport
&&
33 so
->so_laddr
.s_addr
== laddr
.s_addr
&&
34 so
->so_faddr
.s_addr
== faddr
.s_addr
&&
35 so
->so_fport
== fport
)
40 return (struct socket
*)NULL
;
46 * Create a new socket, initialise the fields
47 * It is the responsibility of the caller to
48 * insque() it into the correct linked-list
55 so
= (struct socket
*)malloc(sizeof(struct socket
));
57 memset(so
, 0, sizeof(struct socket
));
58 so
->so_state
= SS_NOFDREF
;
65 * remque and free a socket, clobber cache
71 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
75 if (so
== tcp_last_so
)
77 else if (so
== udp_last_so
)
82 if(so
->so_next
&& so
->so_prev
)
83 remque(so
); /* crashes if so is not in a queue */
89 * Read from so's socket into sb_snd, updating all relevant sbuf fields
90 * NOTE: This will only be called if it is select()ed for reading, so
91 * a read() of 0 (or less) means it's disconnected
97 int n
, nn
, lss
, total
;
98 struct sbuf
*sb
= &so
->so_snd
;
99 int len
= sb
->sb_datalen
- sb
->sb_cc
;
101 int mss
= so
->so_tcpcb
->t_maxseg
;
103 DEBUG_CALL("soread");
104 DEBUG_ARG("so = %lx", (long )so
);
107 * No need to check if there's enough room to read.
108 * soread wouldn't have been called if there weren't
111 len
= sb
->sb_datalen
- sb
->sb_cc
;
113 iov
[0].iov_base
= sb
->sb_wptr
;
114 if (sb
->sb_wptr
< sb
->sb_rptr
) {
115 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
116 /* Should never succeed, but... */
117 if (iov
[0].iov_len
> len
)
118 iov
[0].iov_len
= len
;
119 if (iov
[0].iov_len
> mss
)
120 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
123 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
124 /* Should never succeed, but... */
125 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
126 len
-= iov
[0].iov_len
;
128 iov
[1].iov_base
= sb
->sb_data
;
129 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
130 if(iov
[1].iov_len
> len
)
131 iov
[1].iov_len
= len
;
132 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
135 if (iov
[1].iov_len
> lss
) {
136 iov
[1].iov_len
-= lss
;
139 lss
-= iov
[1].iov_len
;
140 iov
[0].iov_len
-= lss
;
146 if (iov
[0].iov_len
> mss
)
147 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
153 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
154 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
156 nn
= read(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
);
159 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
162 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
164 tcp_sockclosed(sototcpcb(so
));
171 * If there was no error, try and read the second time round
172 * We read again if n = 2 (ie, there's another part of the buffer)
173 * and we read as much as we could in the first read
174 * We don't test for <= 0 this time, because there legitimately
175 * might not be any more data (since the socket is non-blocking),
176 * a close will be detected on next iteration.
177 * A return of -1 wont (shouldn't) happen, since it didn't happen above
179 if (n
== 2 && nn
== iov
[0].iov_len
)
180 nn
+= read(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
);
182 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
188 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
189 sb
->sb_wptr
-= sb
->sb_datalen
;
196 * When the socket is created, we set it SO_OOBINLINE,
197 * so when OOB data arrives, we soread() it and everything
198 * in the send buffer is sent as urgent data
204 struct tcpcb
*tp
= sototcpcb(so
);
206 DEBUG_CALL("sorecvoob");
207 DEBUG_ARG("so = %lx", (long)so
);
210 * We take a guess at how much urgent data has arrived.
211 * In most situations, when urgent data arrives, the next
212 * read() should get all the urgent data. This guess will
213 * be wrong however if more data arrives just after the
214 * urgent data, or the read() doesn't return all the
218 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
226 * There's a lot duplicated code here, but...
232 struct sbuf
*sb
= &so
->so_rcv
;
233 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
237 DEBUG_CALL("sosendoob");
238 DEBUG_ARG("so = %lx", (long)so
);
239 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
241 if (so
->so_urgc
> 2048)
242 so
->so_urgc
= 2048; /* XXXX */
244 if (sb
->sb_rptr
< sb
->sb_wptr
) {
245 /* We can send it directly */
246 n
= send(so
->s
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
249 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
252 * Since there's no sendv or sendtov like writev,
253 * we must copy all data to a linear buffer then
256 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
257 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
258 memcpy(buff
, sb
->sb_rptr
, len
);
261 n
= sb
->sb_wptr
- sb
->sb_data
;
262 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
263 memcpy((buff
+ len
), sb
->sb_data
, n
);
267 n
= send(so
->s
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
270 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
272 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
277 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
278 sb
->sb_rptr
-= sb
->sb_datalen
;
284 * Write data from so_rcv to so's socket,
285 * updating all sbuf field as necessary
292 struct sbuf
*sb
= &so
->so_rcv
;
296 DEBUG_CALL("sowrite");
297 DEBUG_ARG("so = %lx", (long)so
);
306 * No need to check if there's something to write,
307 * sowrite wouldn't have been called otherwise
312 iov
[0].iov_base
= sb
->sb_rptr
;
313 if (sb
->sb_rptr
< sb
->sb_wptr
) {
314 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
315 /* Should never succeed, but... */
316 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
319 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
320 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
321 len
-= iov
[0].iov_len
;
323 iov
[1].iov_base
= sb
->sb_data
;
324 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
325 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
330 /* Check if there's urgent data to send, and if so, send it */
333 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
335 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
337 nn
= write(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
);
339 /* This should never happen, but people tell me it does *shrug* */
340 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
344 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
345 so
->so_state
, errno
));
347 tcp_sockclosed(sototcpcb(so
));
352 if (n
== 2 && nn
== iov
[0].iov_len
)
353 nn
+= write(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
);
354 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
360 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
361 sb
->sb_rptr
-= sb
->sb_datalen
;
364 * If in DRAIN mode, and there's no more data, set
367 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
374 * recvfrom() a UDP socket
380 struct sockaddr_in addr
;
381 int addrlen
= sizeof(struct sockaddr_in
);
383 DEBUG_CALL("sorecvfrom");
384 DEBUG_ARG("so = %lx", (long)so
);
386 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
390 len
= recvfrom(so
->s
, buff
, 256, 0,
391 (struct sockaddr
*)&addr
, &addrlen
);
392 /* XXX Check if reply is "correct"? */
394 if(len
== -1 || len
== 0) {
395 u_char code
=ICMP_UNREACH_PORT
;
397 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
398 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
400 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
401 errno
,strerror(errno
)));
402 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
404 icmp_reflect(so
->so_m
);
405 so
->so_m
= 0; /* Don't m_free() it again! */
407 /* No need for this socket anymore, udp_detach it */
409 } else { /* A "normal" UDP packet */
413 if (!(m
= m_get())) return;
414 m
->m_data
+= if_maxlinkhdr
;
417 * XXX Shouldn't FIONREAD packets destined for port 53,
418 * but I don't know the max packet size for DNS lookups
421 /* if (so->so_fport != htons(53)) { */
422 ioctl(so
->s
, FIONREAD
, &n
);
425 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
431 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
432 (struct sockaddr
*)&addr
, &addrlen
);
433 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
434 m
->m_len
, errno
,strerror(errno
)));
436 u_char code
=ICMP_UNREACH_PORT
;
438 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
439 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
441 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
442 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
446 * Hack: domain name lookup will be used the most for UDP,
447 * and since they'll only be used once there's no need
448 * for the 4 minute (or whatever) timeout... So we time them
449 * out much quicker (10 seconds for now...)
452 if (so
->so_fport
== htons(53))
453 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
455 so
->so_expire
= curtime
+ SO_EXPIRE
;
458 /* if (m->m_len == len) {
459 * m_inc(m, MINCSIZE);
465 * If this packet was destined for CTL_ADDR,
466 * make it look like that's where it came from, done by udp_output
468 udp_output(so
, m
, &addr
);
470 } /* if ping packet */
482 struct sockaddr_in addr
;
484 DEBUG_CALL("sosendto");
485 DEBUG_ARG("so = %lx", (long)so
);
486 DEBUG_ARG("m = %lx", (long)m
);
488 addr
.sin_family
= AF_INET
;
489 if ((so
->so_faddr
.s_addr
& htonl(0xffffff00)) == special_addr
.s_addr
) {
491 switch(ntohl(so
->so_faddr
.s_addr
) & 0xff) {
493 addr
.sin_addr
= dns_addr
;
497 addr
.sin_addr
= loopback_addr
;
501 addr
.sin_addr
= so
->so_faddr
;
502 addr
.sin_port
= so
->so_fport
;
504 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
)));
506 /* Don't care what port we get */
507 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
508 (struct sockaddr
*)&addr
, sizeof (struct sockaddr
));
513 * Kill the socket if there's no reply in 4 minutes,
514 * but only if it's an expirable socket
517 so
->so_expire
= curtime
+ SO_EXPIRE
;
518 so
->so_state
= SS_ISFCONNECTED
; /* So that it gets select()ed */
523 * XXX This should really be tcp_listen
526 solisten(port
, laddr
, lport
, flags
)
532 struct sockaddr_in addr
;
534 int s
, addrlen
= sizeof(addr
), opt
= 1;
536 DEBUG_CALL("solisten");
537 DEBUG_ARG("port = %d", port
);
538 DEBUG_ARG("laddr = %x", laddr
);
539 DEBUG_ARG("lport = %d", lport
);
540 DEBUG_ARG("flags = %x", flags
);
542 if ((so
= socreate()) == NULL
) {
543 /* free(so); Not sofree() ??? free(NULL) == NOP */
547 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
548 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
555 * SS_FACCEPTONCE sockets must time out.
557 if (flags
& SS_FACCEPTONCE
)
558 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
560 so
->so_state
= (SS_FACCEPTCONN
|flags
);
561 so
->so_lport
= lport
; /* Kept in network format */
562 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
564 addr
.sin_family
= AF_INET
;
565 addr
.sin_addr
.s_addr
= INADDR_ANY
;
566 addr
.sin_port
= port
;
568 if (((s
= socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
569 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
571 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
575 /* Restore the real errno */
579 setsockopt(s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int));
580 setsockopt(s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
582 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
583 so
->so_fport
= addr
.sin_port
;
584 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
585 so
->so_faddr
= our_addr
;
587 so
->so_faddr
= addr
.sin_addr
;
594 * Data is available in so_rcv
595 * Just write() the data to the socket
603 /* FD_CLR(so->s,&writefds); */
607 * Data has been freed in so_snd
608 * We have room for a read() if we want to
609 * For now, don't read, it'll be done in the main loop
619 * Various session state calls
620 * XXX Should be #define's
621 * The socket state stuff needs work, these often get call 2 or 3
622 * times each when only 1 was needed
626 register struct socket
*so
;
628 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
629 SS_FCANTSENDMORE
|SS_FWDRAIN
);
630 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
635 register struct socket
*so
;
637 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
638 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
645 if ((so
->so_state
& SS_NOFDREF
) == 0) {
647 if (global_writefds
!= NULL
)
648 FD_CLR(so
->s
, global_writefds
);
650 so
->so_state
&= ~(SS_ISFCONNECTING
);
651 if (so
->so_state
& SS_FCANTSENDMORE
)
652 so
->so_state
= SS_NOFDREF
; /* Don't select it */ /* XXX close() here as well? */
654 so
->so_state
|= SS_FCANTRCVMORE
;
661 if ((so
->so_state
& SS_NOFDREF
) == 0) {
662 shutdown(so
->s
,1); /* send FIN to fhost */
663 if (global_readfds
!= NULL
)
664 FD_CLR(so
->s
, global_readfds
);
665 if (global_xfds
!= NULL
)
666 FD_CLR(so
->s
, global_xfds
);
669 so
->so_state
&= ~(SS_ISFCONNECTING
);
670 if (so
->so_state
& SS_FCANTRCVMORE
)
671 so
->so_state
= SS_NOFDREF
; /* as above */
673 so
->so_state
|= SS_FCANTSENDMORE
;
677 soisfdisconnected(so
)
680 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
682 /* so->so_state = SS_ISFDISCONNECTED; */
684 * XXX Do nothing ... ?
689 * Set write drain mode
690 * Set CANTSENDMORE once all data has been write()n
696 if (so
->so_rcv
.sb_cc
)
697 so
->so_state
|= SS_FWDRAIN
;