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.ack = ee32(tcp->seq) + 1;
tcp_send_ack(t);
t->events |= CB_EVENT_CLOSED;
}
else if (t->sock.tcp.state == TCP_FIN_WAIT_1) {
t->sock.tcp.state = TCP_CLOSING;
t->sock.tcp.ack = ee32(tcp->seq) + 1;
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 */
if (t->sock.tcp.state == TCP_ESTABLISHED) {
t->sock.tcp.state = TCP_CLOSE_WAIT;
t->events |= CB_EVENT_CLOSED;
t->events &= ~CB_EVENT_READABLE;
} else if (t->sock.tcp.state == TCP_FIN_WAIT_1) {
t->sock.tcp.state = TCP_CLOSING;
}
t->sock.tcp.ack = ee32(tcp->seq) + 1;
t->events |= CB_EVENT_CLOSED;
tcp_send_ack(t);
}
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->events = 0;
}
}
for (i = 0; i < MAX_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)
{
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);
if (client_fd > 0) {
printf("accept: %04x\n", client_fd);
}
} else if ((fd == client_fd) && (event & CB_EVENT_READABLE )) {
ret = ft_recvfrom((struct ipstack *)arg, client_fd, buf, sizeof(buf), 0, NULL, NULL);
if (ret == -11)
return; /* Call again */
if (ret < 0) {
printf("Recv error: %d\n", ret);
ft_close((struct ipstack *)arg, client_fd);
return;
} else if (ret == 0) {
printf("Client side closed the connection.\n");
ft_close((struct ipstack *)arg, client_fd);
client_fd = -1;
printf("Server: Exiting.\n");
exit_ok = 1;
return;
} else if (ret > 0) {
printf("recv: %d, echoing back\n", ret);
tot_recv += ret;
if (ret != -11) {
if (ret < 0) {
printf("Recv error: %d\n", ret);
ft_close((struct ipstack *)arg, client_fd);
} else if (ret == 0) {
printf("Client side closed the connection.\n");
ft_close((struct ipstack *)arg, client_fd);
printf("Server: Exiting.\n");
exit_ok = 1;
} else if (ret > 0) {
printf("recv: %d, echoing back\n", ret);
tot_recv += ret;
}
}
}
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");
closed = 1;
}
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);
if (snd_ret == -11)
return; /* Call again */
if (snd_ret < 0) {
printf("Send error: %d\n", snd_ret);
ft_close((struct ipstack *)arg, client_fd);
return;
}
tot_sent += snd_ret;
printf("sent %d bytes\n", snd_ret);
if (tot_recv == tot_sent) {
tot_sent = 0;
tot_recv = 0;
if (tot_sent < tot_recv) {
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 < 0) {
printf("Send error: %d\n", snd_ret);
ft_close((struct ipstack *)arg, client_fd);
} else {
tot_sent += snd_ret;
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)) {
printf("Client side closed the connection (EVENT_CLOSED)\n");
ft_close((struct ipstack *)arg, client_fd);