261
234
* wake up any sleepers
265
register struct tcpcb *tp;
237
tcp_close(struct tcpcb *tp)
267
239
register struct tcpiphdr *t;
268
240
struct socket *so = tp->t_socket;
241
Slirp *slirp = so->slirp;
269
242
register struct mbuf *m;
271
244
DEBUG_CALL("tcp_close");
272
245
DEBUG_ARG("tp = %lx", (long )tp);
274
247
/* free the reassembly queue, if any */
275
t = (struct tcpiphdr *) tp->seg_next;
276
while (t != (struct tcpiphdr *)tp) {
277
t = (struct tcpiphdr *)t->ti_next;
278
m = (struct mbuf *) REASS_MBUF((struct tcpiphdr *)t->ti_prev);
279
remque_32((struct tcpiphdr *) t->ti_prev);
248
t = tcpfrag_list_first(tp);
249
while (!tcpfrag_list_end(t, tp)) {
250
t = tcpiphdr_next(t);
251
m = tcpiphdr_prev(t)->ti_mbuf;
252
remque(tcpiphdr2qlink(tcpiphdr_prev(t)));
283
/* if (tp->t_template)
284
* (void) m_free(dtom(tp->t_template));
286
/* free(tp, M_PCB); */
289
soisfdisconnected(so);
290
257
/* clobber input socket cache if we're closing the cached connection */
291
if (so == tcp_last_so)
258
if (so == slirp->tcp_last_so)
259
slirp->tcp_last_so = &slirp->tcb;
293
260
closesocket(so->s);
294
261
sbfree(&so->so_rcv);
295
262
sbfree(&so->so_snd);
297
STAT(tcpstat.tcps_closed++);
298
264
return ((struct tcpcb *)0);
309
* When a source quench is received, close congestion window
310
* to one segment. We will gradually open it again as we proceed.
317
struct tcpcb *tp = intotcpcb(inp);
320
tp->snd_cwnd = tp->t_maxseg;
326
268
* TCP protocol interface to socket abstraction.
688
* First we accumulate all the initial option negotiation,
689
* then fork_exec() rlogin according to the options
696
struct sbuf *so_snd = &so->so_snd;
697
struct sbuf *so_rcv = &so->so_rcv;
699
/* First check if they have a priveladged port, or too much data has arrived */
700
if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
701
(m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
702
memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
703
so_snd->sb_wptr += 18;
705
tcp_sockclosed(sototcpcb(so));
710
/* Append the current data */
711
memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
712
so_rcv->sb_wptr += m->m_len;
713
so_rcv->sb_rptr += m->m_len;
717
* Check if we have all the initial options,
718
* and build argument list to rlogin while we're here
721
ptr = so_rcv->sb_data;
724
while (ptr < so_rcv->sb_wptr) {
728
sprintf(args, "rlogin -l %s %s",
729
ptr, inet_ntoa(so->so_faddr));
731
i2 = so_rcv->sb_wptr - ptr;
732
for (i = 0; i < i2; i++) {
736
sprintf(term, "%s", ptr);
738
sprintf(term, "TERM=%s", ptr);
751
/* We have it, set our term variable and fork_exec() */
753
setenv("TERM", term, 1);
757
fork_exec(so, args, 2);
761
/* And finally, send the client a 0 character */
762
so_snd->sb_wptr[0] = 0;
772
* First we accumulate all the initial option negotiation,
773
* then rsh_exec() rsh according to the options
780
struct sbuf *so_snd = &so->so_snd;
781
struct sbuf *so_rcv = &so->so_rcv;
783
/* First check if they have a priveladged port, or too much data has arrived */
784
if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
785
(m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
786
memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
787
so_snd->sb_wptr += 18;
789
tcp_sockclosed(sototcpcb(so));
794
/* Append the current data */
795
memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
796
so_rcv->sb_wptr += m->m_len;
797
so_rcv->sb_rptr += m->m_len;
801
* Check if we have all the initial options,
802
* and build argument list to rlogin while we're here
805
ptr = so_rcv->sb_data;
808
if (so->extra==NULL) {
812
if (port <= 0) return 0;
813
if (port > 1023 || port < 512) {
814
memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
815
so_snd->sb_wptr += 18;
817
tcp_sockclosed(sototcpcb(so));
820
if ((ns=socreate()) == NULL)
822
if (tcp_attach(ns)<0) {
827
ns->so_laddr=so->so_laddr;
828
ns->so_lport=htons(port);
830
(void) tcp_mss(sototcpcb(ns), 0);
832
ns->so_faddr=so->so_faddr;
833
ns->so_fport=htons(IPPORT_RESERVED-1); /* Use a fake port. */
835
if (ns->so_faddr.s_addr == 0 ||
836
ns->so_faddr.s_addr == loopback_addr.s_addr)
837
ns->so_faddr = alias_addr;
839
ns->so_iptos = tcp_tos(ns);
844
/* Compute window scaling to request. */
845
/* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
846
* (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
847
* tp->request_r_scale++;
850
/*soisfconnecting(ns);*/
852
STAT(tcpstat.tcps_connattempt++);
854
tp->t_state = TCPS_SYN_SENT;
855
tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
857
tcp_iss += TCP_ISSINCR/2;
862
while (ptr < so_rcv->sb_wptr) {
876
rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
880
/* And finally, send the client a 0 character */
881
so_snd->sb_wptr[0] = 0;
891
struct sbuf *so_snd = &so->so_snd;
892
struct sbuf *so_rcv = &so->so_rcv;
895
* If there is binary data here, we save it in so->so_m
900
rxdata=mtod(m, char *);
901
for (rxlen=m->m_len; rxlen; rxlen--) {
902
if (*rxdata++ & 0x80) {
907
} /* if(so->so_m==NULL) */
912
sbappendsb(so_rcv, m);
914
/* To avoid going over the edge of the buffer, we reset it */
915
if (so_snd->sb_cc == 0)
916
so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
920
* If the first packet we get here is 1 byte long, then it
921
* was done in telnet character mode, therefore we must echo
922
* the characters as they come. Otherwise, we echo nothing,
923
* because in linemode, the line is already echoed
924
* XXX two or more control connections won't work
927
if (m->m_len == 1) do_echo = 1;
931
sbappendsb(so_snd, m);
933
tcp_output(sototcpcb(so)); /* XXX */
938
while (num < so->so_rcv.sb_cc) {
939
if (*(so->so_rcv.sb_rptr + num) == '\n' ||
940
*(so->so_rcv.sb_rptr + num) == '\r') {
943
*(so_rcv->sb_rptr + num) = 0;
944
if (ctl_password && !ctl_password_ok) {
945
/* Need a password */
946
if (sscanf(so_rcv->sb_rptr, "pass %256s", buff) == 1) {
947
if (strcmp(buff, ctl_password) == 0) {
949
n = sprintf(so_snd->sb_wptr,
954
n = sprintf(so_snd->sb_wptr,
955
"Error: Password required, log on with \"pass PASSWORD\"\r\n");
959
n = do_config(so_rcv->sb_rptr, so, PRN_SPRINTF);
961
/* Register the printed data */
964
so_snd->sb_wptr += n;
966
n = sprintf(so_snd->sb_wptr, "Slirp> ");
968
so_snd->sb_wptr += n;
970
/* Drop so_rcv data */
972
so_rcv->sb_wptr = so_rcv->sb_rptr = so_rcv->sb_data;
973
tcp_output(sototcpcb(so)); /* Send the reply */
980
606
case EMU_FTP: /* ftp */
981
*(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
607
*(m->m_data+m->m_len) = 0; /* NUL terminate for strstr */
982
608
if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
984
610
* Need to emulate the PORT command
1076
708
/* The %256s is for the broken mIRC */
1077
709
if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
1078
if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
710
if ((so = tcp_listen(slirp, INADDR_ANY, 0,
711
htonl(laddr), htons(lport),
712
SS_FACCEPTONCE)) == NULL) {
1081
715
m->m_len = bptr - m->m_data; /* Adjust length */
1082
m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
1083
(unsigned long)ntohl(so->so_faddr.s_addr),
1084
ntohs(so->so_fport), 1);
716
m->m_len += snprintf(bptr, m->m_hdr.mh_size,
717
"DCC CHAT chat %lu %u%c\n",
718
(unsigned long)ntohl(so->so_faddr.s_addr),
719
ntohs(so->so_fport), 1);
1085
720
} else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1086
if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
721
if ((so = tcp_listen(slirp, INADDR_ANY, 0,
722
htonl(laddr), htons(lport),
723
SS_FACCEPTONCE)) == NULL) {
1089
726
m->m_len = bptr - m->m_data; /* Adjust length */
1090
m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
1091
buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1092
ntohs(so->so_fport), n1, 1);
727
m->m_len += snprintf(bptr, m->m_hdr.mh_size,
728
"DCC SEND %s %lu %u %u%c\n", buff,
729
(unsigned long)ntohl(so->so_faddr.s_addr),
730
ntohs(so->so_fport), n1, 1);
1093
731
} else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1094
if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
732
if ((so = tcp_listen(slirp, INADDR_ANY, 0,
733
htonl(laddr), htons(lport),
734
SS_FACCEPTONCE)) == NULL) {
1097
737
m->m_len = bptr - m->m_data; /* Adjust length */
1098
m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
1099
buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1100
ntohs(so->so_fport), n1, 1);
738
m->m_len += snprintf(bptr, m->m_hdr.mh_size,
739
"DCC MOVE %s %lu %u %u%c\n", buff,
740
(unsigned long)ntohl(so->so_faddr.s_addr),
741
ntohs(so->so_fport), n1, 1);
1239
881
* Return 0 if this connections is to be closed, 1 otherwise,
1240
882
* return 2 if this is a command-line connection
884
int tcp_ctl(struct socket *so)
1246
struct sbuf *sb = &so->so_snd;
1248
struct ex_list *ex_ptr;
1250
// struct socket *tmpso;
1252
DEBUG_CALL("tcp_ctl");
1253
DEBUG_ARG("so = %lx", (long )so);
1257
* Check if they're authorised
1259
if (ctl_addr.s_addr && (ctl_addr.s_addr == -1 || (so->so_laddr.s_addr != ctl_addr.s_addr))) {
1260
sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n");
1261
sb->sb_wptr += sb->sb_cc;
1265
command = (ntohl(so->so_faddr.s_addr) & 0xff);
1268
default: /* Check for exec's */
1271
* Check if it's pty_exec
1273
for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1274
if (ex_ptr->ex_fport == so->so_fport &&
1275
command == ex_ptr->ex_addr) {
1276
do_pty = ex_ptr->ex_pty;
1284
/* tcp_fconnect(so); */
1288
sb->sb_cc = sprintf(sb->sb_wptr,
1289
"Error: No application configured.\r\n");
1290
sb->sb_wptr += sb->sb_cc;
1294
DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
1295
return(fork_exec(so, ex_ptr->ex_exec, do_pty));
1299
for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
1300
if (tmpso->so_emu == EMU_CTL &&
1302
(tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK))
1304
/* Ooops, control connection already active */
1305
sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n");
1306
sb->sb_wptr += sb->sb_cc;
1310
so->so_emu = EMU_CTL;
1311
ctl_password_ok = 0;
1312
sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> ");
1313
sb->sb_wptr += sb->sb_cc;
886
Slirp *slirp = so->slirp;
887
struct sbuf *sb = &so->so_snd;
888
struct ex_list *ex_ptr;
891
DEBUG_CALL("tcp_ctl");
892
DEBUG_ARG("so = %lx", (long )so);
894
if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
895
/* Check if it's pty_exec */
896
for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
897
if (ex_ptr->ex_fport == so->so_fport &&
898
so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
899
if (ex_ptr->ex_pty == 3) {
901
so->extra = (void *)ex_ptr->ex_exec;
904
do_pty = ex_ptr->ex_pty;
905
DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
906
return fork_exec(so, ex_ptr->ex_exec, do_pty);
911
snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data),
912
"Error: No application configured.\r\n");
913
sb->sb_wptr += sb->sb_cc;