summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2024-06-19 18:50:55 +0200
committerLizzy Fleckenstein <lizzy@vlhl.dev>2024-06-19 18:51:12 +0200
commit9720c7efddf2d7595c9a0b021bc5fc6327c22bcf (patch)
tree0c64d493c2a053638607f34793797a1c15893b75
parent6490c179e7f789a9efb876fa0bdfc195dc374dc5 (diff)
client: send hi to server
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
-rw-r--r--include/net.h18
-rw-r--r--src/client.c11
-rw-r--r--src/net.c7
-rw-r--r--src/server.c55
4 files changed, 51 insertions, 40 deletions
diff --git a/include/net.h b/include/net.h
index fcb12a4..ce2eb02 100644
--- a/include/net.h
+++ b/include/net.h
@@ -5,13 +5,16 @@
#ifndef NET_H
#define NET_H
+#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
+#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>
-
#include "str.h"
+#include "ser.h"
+#include "content.h"
#define SET_NONBLOCK(X) fcntl((X), F_SETFL, fcntl((X), F_GETFL, 0) | O_NONBLOCK)
@@ -23,6 +26,7 @@ typedef uint32_t pkt_header;
typedef struct {
int socket;
bool disco;
+ str *name;
struct {
bool header;
size_t len;
@@ -37,12 +41,20 @@ typedef struct {
} out;
} peer;
-void invalid_pkt(str from, str pkt);
+#define SEND_PKT(CONN, TYPE, ...) \
+ { strbuf pkt = NILSBUF; \
+ ser_pkt_type(&pkt, TYPE); \
+ __VA_ARGS__ \
+ if (!peer_send(&(CONN), pkt.data, pkt.len)) \
+ fprintf(stderr, "failed to send " #TYPE " to %*s\n", PSTR(*(CONN).name)); \
+ free(pkt.data); }
+
+void invalid_pkt(peer *p, str pkt);
int socket_create(const char *host, const char *port, bool server);
int socket_accept(int accept_fd);
-void peer_init(peer *p, int socket);
+void peer_init(peer *p, int socket, str *name);
void peer_free(peer *p);
struct pollfd peer_prepare(peer *p);
str peer_recv(peer *p, struct pollfd pfd);
diff --git a/src/client.c b/src/client.c
index 3bf3649..d15ba8e 100644
--- a/src/client.c
+++ b/src/client.c
@@ -10,6 +10,7 @@
#include "net.h"
#include "ticker.h"
#include "sig.h"
+#include "content.h"
typedef struct {
peer conn;
@@ -80,10 +81,16 @@ int main()
int socket = socket_create("127.0.0.1", "4560", false);
if (socket < 0)
client_exit(&c, EXIT_FAILURE);
- peer_init(&c.conn, socket);
+ str server_name = S("server");
+ peer_init(&c.conn, socket, &server_name);
gfx_alt_buffer(true);
+ SEND_PKT(c.conn, SPKT_HI,
+ ser_str(&pkt, c.name);
+ ser_str(&pkt, c.pass);
+ )
+
ticker t;
ticker_init(&t, NANOS/60);
@@ -122,7 +129,7 @@ int main()
if (c.conn.disco)
client_exit(&c, EXIT_SUCCESS);
if (pkt.len > 0 && !handle_pkt(&c, pkt))
- invalid_pkt(S("server"), pkt);
+ invalid_pkt(&c.conn, pkt);
uint64_t dtime;
if (ticker_tick(&t, &dtime))
diff --git a/src/net.c b/src/net.c
index 18970c1..b5edc07 100644
--- a/src/net.c
+++ b/src/net.c
@@ -16,11 +16,11 @@
#include "net.h"
-void invalid_pkt(str from, str pkt)
+void invalid_pkt(peer *from, str pkt)
{
// TODO: maybe inform peer about failure? not sure
// FIXME: hexdumping a gazillon bytes to stderr might be an issue
- fprintf(stderr, "invalid pkt from %*s: ", PSTR(from));
+ fprintf(stderr, "invalid pkt from %*s: ", PSTR(*from->name));
for (size_t i = 0; i < pkt.len; i++)
fprintf(stderr, "%02x%c", (uint8_t) pkt.data[i], i+1 == pkt.len ? '\n' : ' ');
}
@@ -76,10 +76,11 @@ int socket_accept(int accept_fd)
return socket;
}
-void peer_init(peer *p, int socket)
+void peer_init(peer *p, int socket, str *name)
{
p->socket = socket;
p->disco = false;
+ p->name = name;
p->in.header = true;
p->in.len = 0;
diff --git a/src/server.c b/src/server.c
index 5355620..0addcf2 100644
--- a/src/server.c
+++ b/src/server.c
@@ -192,37 +192,26 @@ void ser_node(strbuf *w, node *n)
ser_color(w, n->col);
}
-bool player_auth(player *p)
-{
- return p->auth;
-}
-
-#define SEND_PKT(TYPE, PEERS, NPEERS, FILTER, ...) \
- { strbuf pkt = NILSBUF; \
- ser_pkt_type(&pkt, TYPE); \
- __VA_ARGS__ \
- for (size_t i = 0; i < NPEERS; i++) { \
- if (FILTER(&(PEERS)[i]) && !peer_send(&(PEERS)[i].conn, pkt.data, pkt.len)) \
- fprintf(stderr, "failed to send " #TYPE " to %*s\n", PSTR((PEERS)[i].name));} \
- free(pkt.data); }
-
void send_players(game *g)
{
- SEND_PKT(CPKT_PLAYERS, g->players.data, g->players.len, player_auth,
- ser_u16(&pkt, g->players.len);
- for (size_t i = 0; i < g->players.len; i++) {
- player *p = &g->players.data[i];
- if (!p->auth) continue;
- ser_str(&pkt, p->name);
- ser_u64(&pkt, p->id);
- }
- )
+ for (size_t i = 0; i < g->players.len; i++) {
+ player *p = &g->players.data[i];
+ if (p->auth) SEND_PKT(p->conn, CPKT_PLAYERS,
+ ser_u16(&pkt, g->players.len);
+ for (size_t i = 0; i < g->players.len; i++) {
+ player *p = &g->players.data[i];
+ if (!p->auth) continue;
+ ser_str(&pkt, p->name);
+ ser_u64(&pkt, p->id);
+ }
+ )
+ }
}
// send_nudes when
void send_nodes(player *p, game *g, int8_t z, int32_t x, int32_t y, uint32_t w, uint32_t h)
{
- SEND_PKT(CPKT_NODES, p, 1,,
+ SEND_PKT(p->conn, CPKT_NODES,
ser_i8(&pkt, z);
ser_i32(&pkt, x);
ser_i32(&pkt, y);
@@ -275,25 +264,27 @@ bool handle_hi(str pkt, player *p, game *g)
return false;
if (str_cmp(g->passphrase, pass) != 0) {
- fprintf(stderr, "wrong passphrase from %*s\n", PSTR(name)); // TODO: log ip ?
- SEND_PKT(CPKT_FAIL, p, 1,, ser_fail_reason(&pkt, FAIL_WRONG_PASS);)
+ printf("wrong passphrase from %*s\n", PSTR(name)); // TODO: log ip ?
+ SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_WRONG_PASS);)
return true; // valid pkt, but invalid passphrase
}
for (size_t i = 0; i < g->players.len; i++) {
player *p2 = &g->players.data[i];
if (p2->auth && str_cmp(p2->name, name) == 0) {
- SEND_PKT(CPKT_FAIL, p, 1,, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);)
+ SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);)
return true;
}
}
+ printf("player authenticated as %*s\n", PSTR(name));
+
p->auth = true;
p->name = str_clone(name);
p->x = p->y = 0;
p->z = 0;
- SEND_PKT(CPKT_HI, p, 1,, ser_str(&pkt, g->motd);)
+ SEND_PKT(p->conn, CPKT_HI, ser_str(&pkt, g->motd);)
send_players(g);
uint32_t range = 10;
@@ -354,7 +345,7 @@ int main()
for (size_t i = 0; i < g.players.len;) {
player *p = &g.players.data[i];
if (p->conn.disco) {
- printf("player disco\n");
+ printf("player %*s disconnected\n", PSTR(p->name));
player_remove(p, &g);
continue;
}
@@ -382,7 +373,7 @@ int main()
if (p->conn.disco || pkt.len == 0)
continue;
if (!handle_pkt(pkt, p, &g))
- invalid_pkt(p->name, pkt);
+ invalid_pkt(&p->conn, pkt);
}
if (fds[g.players.len].revents) {
@@ -394,8 +385,8 @@ int main()
player *p = ARR_APPEND(g.players);
p->auth = false;
p->id = g.entity_id++;
- p->name = S("unauthenticated");
- peer_init(&p->conn, socket);
+ p->name = S("(unauthenticated)");
+ peer_init(&p->conn, socket, &p->name);
}
}
}