Fix CLOSED event, fix linux test callback

This commit is contained in:
Daniele Lacamera 2024-11-03 09:47:29 +01:00
parent 15b4d7d6ea
commit f11153b3c6
2 changed files with 49 additions and 47 deletions

View file

@ -1051,11 +1051,13 @@ static void tcp_input(struct ipstack *S, struct ipstack_tcp_seg *tcp, uint32_t f
t->sock.tcp.state = TCP_CLOSE_WAIT; t->sock.tcp.state = TCP_CLOSE_WAIT;
t->sock.tcp.ack = ee32(tcp->seq) + 1; t->sock.tcp.ack = ee32(tcp->seq) + 1;
tcp_send_ack(t); tcp_send_ack(t);
t->events |= CB_EVENT_CLOSED;
} }
else if (t->sock.tcp.state == TCP_FIN_WAIT_1) { else if (t->sock.tcp.state == TCP_FIN_WAIT_1) {
t->sock.tcp.state = TCP_CLOSING; t->sock.tcp.state = TCP_CLOSING;
t->sock.tcp.ack = ee32(tcp->seq) + 1; t->sock.tcp.ack = ee32(tcp->seq) + 1;
tcp_send_ack(t); tcp_send_ack(t);
t->events |= CB_EVENT_CLOSED;
} }
} }
@ -1102,12 +1104,12 @@ static void tcp_input(struct ipstack *S, struct ipstack_tcp_seg *tcp, uint32_t f
/* FIN */ /* FIN */
if (t->sock.tcp.state == TCP_ESTABLISHED) { if (t->sock.tcp.state == TCP_ESTABLISHED) {
t->sock.tcp.state = TCP_CLOSE_WAIT; t->sock.tcp.state = TCP_CLOSE_WAIT;
t->events |= CB_EVENT_CLOSED;
t->events &= ~CB_EVENT_READABLE; t->events &= ~CB_EVENT_READABLE;
} else if (t->sock.tcp.state == TCP_FIN_WAIT_1) { } else if (t->sock.tcp.state == TCP_FIN_WAIT_1) {
t->sock.tcp.state = TCP_CLOSING; t->sock.tcp.state = TCP_CLOSING;
} }
t->sock.tcp.ack = ee32(tcp->seq) + 1; t->sock.tcp.ack = ee32(tcp->seq) + 1;
t->events |= CB_EVENT_CLOSED;
tcp_send_ack(t); tcp_send_ack(t);
} }
if (tcp->flags & 0x10) { if (tcp->flags & 0x10) {
@ -1971,6 +1973,7 @@ int ipstack_poll(struct ipstack *s, uint64_t now)
ts->callback(i | MARK_TCP_SOCKET, ts->events, ts->callback_arg); ts->callback(i | MARK_TCP_SOCKET, ts->events, ts->callback_arg);
ts->events = 0; ts->events = 0;
} }
} }
for (i = 0; i < MAX_UDPSOCKETS; i++) { for (i = 0; i < MAX_UDPSOCKETS; i++) {
struct tsocket *ts = &s->udpsockets[i]; struct tsocket *ts = &s->udpsockets[i];

View file

@ -26,31 +26,28 @@ static int closed = 0;
static void socket_cb(int fd, uint16_t event, void *arg) static void socket_cb(int fd, uint16_t event, void *arg)
{ {
int ret = 0; int ret = 0;
printf("Called socket_cb, events: %04x\n", event); printf("Called socket_cb, events: %04x fd %d\n", event, fd & (~0x1000));
if ((fd == listen_fd) && (event & CB_EVENT_READABLE)) { if ((fd == listen_fd) && (event & CB_EVENT_READABLE) && (client_fd == -1)) {
client_fd = ft_accept((struct ipstack *)arg, listen_fd, NULL, NULL); client_fd = ft_accept((struct ipstack *)arg, listen_fd, NULL, NULL);
if (client_fd > 0) { if (client_fd > 0) {
printf("accept: %04x\n", client_fd); printf("accept: %04x\n", client_fd);
} }
} else if ((fd == client_fd) && (event & CB_EVENT_READABLE )) { } else if ((fd == client_fd) && (event & CB_EVENT_READABLE )) {
ret = ft_recvfrom((struct ipstack *)arg, client_fd, buf, sizeof(buf), 0, NULL, NULL); ret = ft_recvfrom((struct ipstack *)arg, client_fd, buf, sizeof(buf), 0, NULL, NULL);
if (ret == -11) if (ret != -11) {
return; /* Call again */ if (ret < 0) {
if (ret < 0) { printf("Recv error: %d\n", ret);
printf("Recv error: %d\n", ret); ft_close((struct ipstack *)arg, client_fd);
ft_close((struct ipstack *)arg, client_fd); } else if (ret == 0) {
return; printf("Client side closed the connection.\n");
} else if (ret == 0) { ft_close((struct ipstack *)arg, client_fd);
printf("Client side closed the connection.\n"); printf("Server: Exiting.\n");
ft_close((struct ipstack *)arg, client_fd); exit_ok = 1;
client_fd = -1; } else if (ret > 0) {
printf("Server: Exiting.\n"); printf("recv: %d, echoing back\n", ret);
exit_ok = 1; tot_recv += ret;
return; }
} else if (ret > 0) {
printf("recv: %d, echoing back\n", ret);
tot_recv += ret;
} }
} }
if ((event & CB_EVENT_WRITABLE) || ((ret > 0) && !closed)) { if ((event & CB_EVENT_WRITABLE) || ((ret > 0) && !closed)) {
@ -60,24 +57,26 @@ static void socket_cb(int fd, uint16_t event, void *arg)
printf("Server: I closed the connection.\n"); printf("Server: I closed the connection.\n");
closed = 1; closed = 1;
} }
if (tot_sent >= tot_recv) if (tot_sent < tot_recv) {
return; snd_ret = ft_sendto((struct ipstack *)arg, client_fd, buf + tot_sent, tot_recv - tot_sent, 0, NULL, 0);
snd_ret = ft_sendto((struct ipstack *)arg, client_fd, buf + tot_sent, tot_recv - tot_sent, 0, NULL, 0); if (snd_ret != -11) {
if (snd_ret == -11) if (snd_ret < 0) {
return; /* Call again */ printf("Send error: %d\n", snd_ret);
if (snd_ret < 0) { ft_close((struct ipstack *)arg, client_fd);
printf("Send error: %d\n", snd_ret); } else {
ft_close((struct ipstack *)arg, client_fd); tot_sent += snd_ret;
return; printf("sent %d bytes\n", snd_ret);
} if (tot_recv == tot_sent) {
tot_sent = 0;
tot_sent += snd_ret; tot_recv = 0;
printf("sent %d bytes\n", snd_ret); }
if (tot_recv == tot_sent) { }
tot_sent = 0; }
tot_recv = 0;
} }
} }
if (event & CB_EVENT_CLOSED) {
printf("Closing %d, client fd: %d\n", fd, client_fd);
}
if ((fd == client_fd) && (event & CB_EVENT_CLOSED)) { if ((fd == client_fd) && (event & CB_EVENT_CLOSED)) {
printf("Client side closed the connection (EVENT_CLOSED)\n"); printf("Client side closed the connection (EVENT_CLOSED)\n");
ft_close((struct ipstack *)arg, client_fd); ft_close((struct ipstack *)arg, client_fd);