Skip to content

Commit

Permalink
Added shutdown for TCP and more properly handle FIN packets
Browse files Browse the repository at this point in the history
  • Loading branch information
transistorfet committed Mar 1, 2021
1 parent ba5b61d commit 827bcbe
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 104 deletions.
6 changes: 3 additions & 3 deletions software/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ monitor.bin: $(MONITOR_OBJS)
hexdump -v -e '/1 "0x%02X, "' $@ > output.txt


boot.load: boot.bin
tools/make-load-file $^ $@ 180000

boot.bin: $(BOOT_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) --entry=main -Ttext=0x0000 -o $@.elf $^
$(CC) $(CFLAGS) $(LDFLAGS) --entry=main -Ttext=0x0000 -Wl,--oformat=binary -o $@ $^
Expand All @@ -157,7 +154,10 @@ kernel.bin: $(KERNEL_OBJS)
cp $@ $(BUILD)


boot.load: LOAD_ARGS = 180000
kernel.load: LOAD_ARGS = 100000 ata0


%.load: %.bin
tools/make-load-file $^ $@ $(LOAD_ARGS)

Expand Down
8 changes: 8 additions & 0 deletions software/include/netinet/in.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#include <stdint.h>
#include <sys/socket.h>

#include <asm/macros.h>

#define ntohl(x) from_be32((x))
#define ntohs(x) from_be16((x))
#define htonl(x) to_be32((x))
#define htons(x) to_be16((x))


#define IPPROTO_IP 0
#define IPPROTO_ICMP 1
#define IPPROTO_IGMP 2
Expand Down
8 changes: 3 additions & 5 deletions software/src/commands/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#include <sys/select.h>
#include <netinet/in.h>

//#include <arpa/inet.h>
#include <asm/macros.h>
#define htobe16 to_be16

#define HTTP_PORT 8099
#define MAX_CONNECTIONS 20
#define MAX_INPUT 256
Expand Down Expand Up @@ -66,7 +62,7 @@ int create_listener()

memset(&addr, '\0', sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htobe16(HTTP_PORT);
addr.sin_port = htons(HTTP_PORT);
addr.sin_addr.s_addr = INADDR_ANY;

error = bind(listenfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
Expand Down Expand Up @@ -102,6 +98,7 @@ void close_connection(struct connection *conn)
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (clients[i] == conn) {
printf("closing client %d\n", clients[i]->fd);
shutdown(clients[i]->fd, SHUT_RDWR);
close(clients[i]->fd);
free(clients[i]);
clients[i] = NULL;
Expand Down Expand Up @@ -230,6 +227,7 @@ int run_server()

read_loop(listenfd);

shutdown(listenfd, SHUT_RDWR);
close(listenfd);

return 0;
Expand Down
29 changes: 21 additions & 8 deletions software/src/commands/ns.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <asm/macros.h>

#include "prototype.h"

#define MAX_INPUT 256

void handle_signal(int signum);
int client_loop(int sockfd, int f_udp, char *address, int port, int f_verbose);
int listen_loop(int sockfd, int f_udp, int port, int f_verbose);

int MAIN(command_ns)(int argc, char **argv)
{
int opt;
int sockfd;
struct sigaction act;

const char *usage = "Usage: ns [-ulv] [<address> [<port>]]";
char *address = "192.168.1.102";
Expand Down Expand Up @@ -61,6 +63,11 @@ int MAIN(command_ns)(int argc, char **argv)
if (optind < argc)
port = strtol(argv[optind++], NULL, 10);

// On SIGINT, cause the syscall to cancel
act.sa_handler = handle_signal;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);

sockfd = socket(PF_INET, f_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
if (sockfd < 0) {
Expand All @@ -73,11 +80,17 @@ int MAIN(command_ns)(int argc, char **argv)
else
client_loop(sockfd, f_udp, address, port, f_verbose);

shutdown(sockfd, SHUT_RDWR);
close(sockfd);

return 0;
}

void handle_signal(int signum)
{
return;
}

int client_loop(int sockfd, int f_udp, char *address, int port, int f_verbose)
{
int error;
Expand All @@ -87,7 +100,7 @@ int client_loop(int sockfd, int f_udp, char *address, int port, int f_verbose)

memset(&addr, '\0', sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = to_be16(port);
addr.sin_port = htons(port);
inet_aton(address, &addr.sin_addr);

if (!f_udp) {
Expand All @@ -104,7 +117,8 @@ int client_loop(int sockfd, int f_udp, char *address, int port, int f_verbose)
while (1) {
nbytes = read(STDIN_FILENO, buffer, MAX_INPUT);
if (nbytes < 0) {
printf("Error reading: %d\n", nbytes);
if (nbytes != EINTR)
printf("Error reading: %d\n", nbytes);
break;
}

Expand Down Expand Up @@ -132,7 +146,7 @@ int listen_loop(int sockfd, int f_udp, int port, int f_verbose)

memset(&addr, '\0', sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = to_be16(port);
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;

error = bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
Expand Down Expand Up @@ -170,7 +184,8 @@ int listen_loop(int sockfd, int f_udp, int port, int f_verbose)
nbytes = recv(sockfd, buffer, MAX_INPUT, 0);

if (nbytes <= 0) {
printf("Error receiving: %d\n", nbytes);
if (nbytes != 0 && nbytes != EINTR)
printf("Error receiving: %d\n", nbytes);
return -1;
}
buffer[nbytes] = '\0';
Expand All @@ -181,8 +196,6 @@ int listen_loop(int sockfd, int f_udp, int port, int f_verbose)
fputs(buffer, stdout);
}

close(sockfd);

return 0;
}

1 change: 1 addition & 0 deletions software/src/kernel/net/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct endpoint_ops {
int (*listen)(struct endpoint *ep, int queue);
int (*accept)(struct endpoint *ep, struct sockaddr *sockaddr, socklen_t *len, struct endpoint **result);
int (*connect)(struct endpoint *ep, const struct sockaddr *sockaddr, socklen_t len);
int (*shutdown)(struct endpoint *ep, int how);
int (*send)(struct endpoint *ep, const char *buf, int nbytes);
int (*recv)(struct endpoint *ep, char *buf, int max);
int (*send_to)(struct endpoint *ep, const char *buf, int nbytes, const struct sockaddr *sockaddr, socklen_t len);
Expand Down
Loading

0 comments on commit 827bcbe

Please sign in to comment.