From 93549c53f6c485a00ae1c9856878e3f6af148cb0 Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Fri, 31 Jul 2015 09:30:20 -0700 Subject: [PATCH] hacked-together irc stuff --- Makefile | 2 +- kernel/include/mod/net.h | 4 +++ modules/irc.c | 58 ++++++++++++++++++++++++++++++++-------- modules/net.c | 16 +++++------ 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index b96797ed..25cf62b7 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ BOOT_MODULES += packetfs BOOT_MODULES += snd BOOT_MODULES += pcspkr BOOT_MODULES += ac97 -BOOT_MODULES += net rtl +BOOT_MODULES += net rtl irc # This is kinda silly. We're going to form an -initrd argument.. # which is basically -initrd "hdd/mod/%.ko,hdd/mod/%.ko..." diff --git a/kernel/include/mod/net.h b/kernel/include/mod/net.h index 3cd2cda0..d5cc0c8a 100644 --- a/kernel/include/mod/net.h +++ b/kernel/include/mod/net.h @@ -21,4 +21,8 @@ extern void init_netif_funcs(get_mac_func mac_func, get_packet_func get_func, se extern void net_handler(void * data, char * name); extern size_t write_dhcp_packet(uint8_t * buffer); +extern struct socket* net_open(uint32_t type); +extern int net_send(struct socket* socket, uint8_t* payload, size_t payload_size, int flags); +extern size_t net_recv(struct socket* socket, uint8_t* buffer, size_t len); +extern int net_connect(struct socket* socket, uint32_t dest_ip, uint16_t dest_port); #endif diff --git a/modules/irc.c b/modules/irc.c index c46e8c57..1d00c6d4 100644 --- a/modules/irc.c +++ b/modules/irc.c @@ -5,28 +5,46 @@ #include #include -static fs_node_t * irc_socket; - static spin_lock_t irc_tty_lock = { 0 }; static char irc_input[400] = {'\0'}; static char irc_prompt[100] = {'\0'}; static char irc_nick[32] = {'\0'}; static char irc_payload[512]; +static struct socket * irc_socket = NULL; -static char * fgets(char * buf, int size, fs_node_t * stream) { +static char read_a_byte(struct socket * stream) { + static char * foo = NULL; + static char * read_ptr = NULL; + static int have_bytes = 0; + if (!foo) foo = malloc(2048); + while (!have_bytes) { + have_bytes = net_recv(stream, (uint8_t *)foo, 2048); + read_ptr = foo; + } + + char ret = *read_ptr; + + have_bytes -= 1; + read_ptr++; + + return ret; +} + + +static char * fgets(char * buf, int size, struct socket * stream) { char * x = buf; int collected = 0; while (collected < size) { - int r = read_fs(stream, 0, 1, (unsigned char *)x); - collected += r; - if (r == -1) return NULL; - if (!r) break; + *x = read_a_byte(stream); + + collected++; + if (*x == '\n') break; - x += r; + x++; } x++; @@ -36,6 +54,7 @@ static char * fgets(char * buf, int size, fs_node_t * stream) { static void irc_send(char * payload) { /* Uh, stuff */ + net_send(irc_socket, (uint8_t *)payload, strlen(payload), 0); } static int tty_readline(fs_node_t * dev, char * linebuf, int max) { @@ -181,7 +200,9 @@ static void ircd(void * data, char * name) { char * buf = malloc(4096); while (1) { + fprintf(tty, "[irc] Receiving...\n"); char * result = fgets(buf, 4095, irc_socket); + fprintf(tty, "[irc] result = %s\n", result); if (!result) continue; size_t len = strlen(buf); if (!len) continue; @@ -191,20 +212,34 @@ static void ircd(void * data, char * name) { } DEFINE_SHELL_FUNCTION(irc_init, "irc connector") { + /* TODO set up IRC socket */ + irc_socket = net_open(SOCK_STREAM); + net_connect(irc_socket, ip_aton("10.255.50.206"), 1025); + + fprintf(tty, "[irc] Socket is at 0x%x\n", irc_socket); + + /* TODO set up IRC daemon */ + create_kernel_tasklet(ircd, "[ircd]", tty); + + return 0; +} + +DEFINE_SHELL_FUNCTION(irc_nick, "irc nick") { if (argc < 2) { fprintf(tty, "Specify a username\n"); return 1; } + fprintf(tty, "[irc] Sending name...\n"); memcpy(irc_nick, argv[1], strlen(argv[1])+1); - sprintf(irc_payload, "NICK %s\r\nUSER %s * 0 :%s\r\n", irc_nick, irc_nick, irc_nick); - + sprintf(irc_payload, "NICK %s\r\nUSER %s * 0 :%s\r\n" + "PASS %s:%s\r\n", irc_nick, irc_nick, irc_nick, irc_nick, "Mqlsfanpra"); irc_send(irc_payload); - return 0; } + DEFINE_SHELL_FUNCTION(irc_join, "irc channel tool") { if (argc < 2) { @@ -262,6 +297,7 @@ DEFINE_SHELL_FUNCTION(irc_join, "irc channel tool") { static int init(void) { BIND_SHELL_FUNCTION(irc_init); + BIND_SHELL_FUNCTION(irc_nick); BIND_SHELL_FUNCTION(irc_join); return 0; diff --git a/modules/net.c b/modules/net.c index f7e63400..3356503c 100644 --- a/modules/net.c +++ b/modules/net.c @@ -420,7 +420,7 @@ static int net_send_tcp(struct socket *socket, uint16_t flags, uint8_t * payload return net_send_ip(socket, IPV4_PROT_TCP, tcp, sizeof(struct tcp_header) + payload_size); } -static struct socket* net_open(uint32_t type) { +struct socket* net_open(uint32_t type) { // This is a socket() call struct socket *sock = malloc(sizeof(struct socket)); memset(sock, 0, sizeof(struct socket)); @@ -429,11 +429,11 @@ static struct socket* net_open(uint32_t type) { return sock; } -static int net_send(struct socket* socket, uint8_t* payload, size_t payload_size, int flags) { +int net_send(struct socket* socket, uint8_t* payload, size_t payload_size, int flags) { return net_send_tcp(socket, TCP_FLAGS_ACK | TCP_FLAGS_PSH, payload, payload_size); } -static size_t net_recv(struct socket* socket, uint8_t* buffer, size_t len) { +size_t net_recv(struct socket* socket, uint8_t* buffer, size_t len) { tcpdata_t *tcpdata = NULL; node_t *node = NULL; fprintf(_atty, "net_recv: ENTER\n"); @@ -462,7 +462,7 @@ static size_t net_recv(struct socket* socket, uint8_t* buffer, size_t len) { fprintf(_atty, "net_recv: Got packet with len: %d\n", tcpdata->payload_size); if (tcpdata->payload != 0) { - memcpy(buffer, tcpdata->payload, len); + memcpy(buffer, tcpdata->payload, len < tcpdata->payload_size ? len : tcpdata->payload_size); } fprintf(_atty, "net_recv: About to free the node value\n"); @@ -589,7 +589,7 @@ static struct ethernet_packet* net_receive(void) { return eth; } -static int net_connect(struct socket* socket) { +int net_connect(struct socket* socket, uint32_t dest_ip, uint16_t dest_port) { int ret; if (socket->sock_type == SOCK_DGRAM) { @@ -607,8 +607,8 @@ static int net_connect(struct socket* socket) { socket->packet_queue = list_create(); socket->packet_wait = list_create(); - socket->ip = ip_aton("192.168.134.129"); - socket->port_dest = 12345; + socket->ip = dest_ip; //ip_aton("10.255.50.206"); + socket->port_dest = dest_port; //12345; fprintf(_atty, "net_connect: using ephemeral port: %d\n", (void*)socket->port_recv); @@ -653,7 +653,7 @@ DEFINE_SHELL_FUNCTION(conn, "Do connection") { struct socket* socket = net_open(SOCK_STREAM); fprintf(_atty, "conn: Make connection\n"); - ret = net_connect(socket); + ret = net_connect(socket, ip_aton("10.255.50.206"), 12345); fprintf(_atty, "conn: connection ret: %d\n", ret);