protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

commit 298fa883707d639856eee7ca83c6bfb5dd9dab8b
parent 1b2b24fa43d98378b15c09d947ddb470e7d3af04
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  6 Aug 2020 20:24:57 -0700

client/server packets working

Diffstat:
Mclient.c | 16+++++++++-------
Mnet.c | 16+++++++++++++++-
Mnet.h | 2+-
Mserve.c | 24+++++++++++++++---------
4 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/client.c b/client.c @@ -10,6 +10,7 @@ #include "client.h" #include "cursor.h" +#include "net.h" int inet_aton(const char *cp, struct in_addr *inp); @@ -21,6 +22,7 @@ int protoverse_connect(const char *server_ip_str, int port) struct in_addr server_in_addr; struct sockaddr_in server_addr; struct cursor cursor; + struct packet packet; ssize_t sent; const char msg[] = "hello, world"; @@ -41,15 +43,15 @@ int protoverse_connect(const char *server_ip_str, int port) server_addr.sin_addr = server_in_addr; printf("sending '%s' to %s\n", msg, server_ip_str); - sent = sendto(sockfd, buf, sizeof(msg), MSG_CONFIRM, - (struct sockaddr*)&server_addr, - sizeof(server_addr)); - printf("does this work?\n"); + packet.type = PKT_CHAT; + packet.data.chat.message = "hello, world"; + packet.data.chat.sender = 1; - if (sent != sizeof(msg)) { - printf("expected to send %ld, sent %ld instead\n", - sizeof(msg), sent); + sent = send_packet(sockfd, (struct sockaddr*)&server_addr, + sizeof(server_addr), &packet); + + if (!sent) { exit(1); } diff --git a/net.c b/net.c @@ -54,7 +54,21 @@ int send_packet(int sockfd, struct sockaddr *to_addr, int to_addr_len, struct pa return 0; } - return 1; + return ok; +} + +int recv_packet(int sockfd, struct cursor *buf, struct packet *packet) +{ + static unsigned char tmp[0xFFFF]; + struct cursor tmp_cursor; + struct sockaddr addr; + socklen_t addrlen; + + recvfrom(sockfd, tmp, sizeof(tmp), 0, &addr, &addrlen); + + make_cursor(tmp, tmp + sizeof(tmp), &tmp_cursor); + + return pull_packet(&tmp_cursor, buf, packet); } static int push_envelope(struct cursor *cursor, enum packet_type type, int len) diff --git a/net.h b/net.h @@ -31,7 +31,7 @@ struct packet { }; int send_packet(int fd, struct sockaddr *to_addr, int to_addr_len, struct packet *packet); -int recv_packet(int fd, struct packet *packet); +int recv_packet(int fd, struct cursor *buf, struct packet *packet); int push_packet(unsigned char *buf, int bufsize, struct packet *packet); int pull_packet(struct cursor *c, struct cursor *buf, struct packet *packet); diff --git a/serve.c b/serve.c @@ -8,19 +8,22 @@ #include <errno.h> #include "serve.h" +#include "net.h" int inet_aton(const char *cp, struct in_addr *inp); int protoverse_serve(const char *bind_addr_str, int port) { - int fd; + static unsigned char buf_[1024]; + struct in_addr my_addr; struct sockaddr_in bind_addr; - struct sockaddr addr; - socklen_t addrlen; - ssize_t recv_len; - static char buf[1024]; - int err; + struct packet packet; + struct cursor buf; + + int err, ok, fd; + + make_cursor(buf_, buf_ + sizeof(buf_), &buf); if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { printf("socket creation failed: %s\n", strerror(errno)); @@ -45,10 +48,13 @@ int protoverse_serve(const char *bind_addr_str, int port) } while (1) { - recv_len = recvfrom(fd, buf, sizeof(buf), 0, &addr, - &addrlen); + ok = recv_packet(fd, &buf, &packet); + if (!ok) { + printf("malformed packet\n"); + continue; + } - printf("got '%.*s'\n", (int)recv_len, buf); + print_packet(&packet); } }