Skip to content

Commit 0c9c240

Browse files
protocolstardustsingaraiona
authored andcommitted
feat(IPC) - Parse credentials
1 parent 29fa245 commit 0c9c240

3 files changed

Lines changed: 54 additions & 10 deletions

File tree

core/ipc.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ i64_t ipc_open(poll_p poll, sock_addr_t *addr, i64_t timeout) {
229229
selector_p selector;
230230
struct poll_registry_t registry = ZERO_INIT_STRUCT;
231231
ipc_ctx_p ctx;
232-
u8_t buf[2] = {RAYFORCE_VERSION, 0x00};
232+
u8_t handshake[258]; // max creds (255) + null + version
233+
i64_t handshake_len;
233234

234235
LOG_DEBUG("Opening connection to %s:%lld", addr->ip, addr->port);
235236

@@ -239,10 +240,25 @@ i64_t ipc_open(poll_p poll, sock_addr_t *addr, i64_t timeout) {
239240
if (fd == -1)
240241
return -1;
241242

242-
if (sock_send(fd, buf, 2) == -1)
243+
// Build handshake: [version][creds\0] or [version\0] if no creds
244+
// Server reads until \0, then responds with version
245+
if (addr->creds_len > 0) {
246+
handshake[0] = RAYFORCE_VERSION;
247+
memcpy(&handshake[1], addr->creds, addr->creds_len);
248+
handshake[1 + addr->creds_len] = '\0';
249+
handshake_len = 2 + addr->creds_len;
250+
LOG_DEBUG("Sending handshake with credentials (len=%lld)", addr->creds_len);
251+
} else {
252+
handshake[0] = RAYFORCE_VERSION;
253+
handshake[1] = '\0';
254+
handshake_len = 2;
255+
LOG_DEBUG("Sending handshake without credentials");
256+
}
257+
258+
if (sock_send(fd, handshake, handshake_len) == -1)
243259
return -1;
244260

245-
if (sock_recv(fd, buf, 1) == -1)
261+
if (sock_recv(fd, handshake, 1) == -1)
246262
return -1;
247263

248264
LOG_TRACE("Setting socket to non-blocking mode");

core/sock.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@
4141
#include "ops.h"
4242

4343
i64_t sock_addr_from_str(str_p str, i64_t len, sock_addr_t *addr) {
44-
i64_t r;
45-
str_p tok;
44+
i64_t r, port_len;
45+
str_p tok, port_end;
4646

4747
// Check for NULL pointers
4848
if (str == NULL || addr == NULL)
4949
return -1;
5050

51-
// Get host part
51+
// Initialize creds to empty
52+
addr->creds[0] = '\0';
53+
addr->creds_len = 0;
54+
55+
// Get host part (first colon)
5256
tok = (str_p)memchr(str, ':', len);
5357
if (tok == NULL)
5458
return -1;
@@ -60,13 +64,35 @@ i64_t sock_addr_from_str(str_p str, i64_t len, sock_addr_t *addr) {
6064
addr->ip[tok - str] = '\0';
6165
tok++;
6266

63-
// Get port part
67+
// Calculate remaining length after host:
6468
len -= tok - str;
65-
r = i64_from_str(tok, len, &addr->port);
6669

67-
if (r != len)
70+
// Find next colon (if any) to separate port from credentials
71+
port_end = (str_p)memchr(tok, ':', len);
72+
if (port_end != NULL) {
73+
// Format: host:port:creds
74+
port_len = port_end - tok;
75+
} else {
76+
// Format: host:port (no creds)
77+
port_len = len;
78+
}
79+
80+
// Parse port
81+
r = i64_from_str(tok, port_len, &addr->port);
82+
if (r != port_len)
6883
return -1;
6984

85+
// Extract credentials if present
86+
if (port_end != NULL) {
87+
port_end++; // skip the colon
88+
i64_t creds_len = len - port_len - 1;
89+
if (creds_len > 0 && creds_len < ISIZEOF(addr->creds)) {
90+
memcpy(addr->creds, port_end, creds_len);
91+
addr->creds[creds_len] = '\0';
92+
addr->creds_len = creds_len;
93+
}
94+
}
95+
7096
return 0;
7197
}
7298

core/sock.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#include "rayforce.h"
2828

2929
typedef struct sock_addr_t {
30-
c8_t ip[256]; // For IPv4 addresses or hostnames
30+
c8_t ip[256]; // For IPv4 addresses or hostnames
3131
i64_t port;
32+
c8_t creds[256]; // Credentials (user:pass or token)
33+
i64_t creds_len;
3234
} sock_addr_t;
3335

3436
i64_t sock_addr_from_str(str_p str, i64_t len, sock_addr_t *addr);

0 commit comments

Comments
 (0)