2023-05-21 18:47:35 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2023-05-26 23:52:57 -04:00
|
|
|
#include <lua5.3/lualib.h>
|
|
|
|
#include <lua5.3/lauxlib.h>
|
2023-05-21 18:47:35 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-05-27 17:28:19 -04:00
|
|
|
#include "oct_log.h"
|
2023-05-21 18:47:35 -04:00
|
|
|
#include "oct_networking.h"
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int needs_recv;
|
|
|
|
char recv_buffer[BUFFER_SIZE];
|
2023-05-27 17:28:19 -04:00
|
|
|
char recv_addr[NI_MAXHOST];
|
|
|
|
char recv_port[NI_MAXSERV];
|
2023-05-26 23:52:57 -04:00
|
|
|
|
|
|
|
int needs_send;
|
2023-05-21 18:47:35 -04:00
|
|
|
char send_buffer[BUFFER_SIZE];
|
2023-05-27 17:28:19 -04:00
|
|
|
char send_addr[NI_MAXHOST];
|
|
|
|
char send_port[NI_MAXSERV];
|
2023-05-21 18:47:35 -04:00
|
|
|
int sfd;
|
2023-05-26 21:20:13 -04:00
|
|
|
} oct_network_node;
|
2023-05-21 18:47:35 -04:00
|
|
|
|
2023-06-08 22:07:58 -04:00
|
|
|
// Address book currently implemented as a doubly-linked list with O(n) inserts, accesses
|
|
|
|
// Ideal implementation is hash table with O(1) inserts, accesses
|
|
|
|
// However, we'll see if this works for now
|
2023-06-04 12:20:57 -04:00
|
|
|
struct {
|
|
|
|
unsigned int size;
|
|
|
|
struct oct_network_ab_entry* first;
|
|
|
|
struct oct_network_ab_entry* last;
|
|
|
|
} oct_network_ab;
|
|
|
|
|
|
|
|
// Returns a live sfd, need to close eventually
|
|
|
|
static int oct_network_init_socket(char* addr, char* port) {
|
|
|
|
int s;
|
|
|
|
int sfd;
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo* result;
|
|
|
|
struct addrinfo* rp;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
s = getaddrinfo(oct_network_node.send_addr, oct_network_node.send_port, &hints, &result);
|
|
|
|
if (s != 0) {
|
|
|
|
OCT_LOG_WARNING("getaddrinfo failed on %s:%s\n", addr, port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
Try each address until we successfully connect(2).
|
|
|
|
If socket(2) (or connect(2)) fails, we (close the socket
|
|
|
|
and) try the next address. */
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
|
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
|
|
rp->ai_protocol);
|
|
|
|
if (sfd == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
|
|
|
break; /* Success */
|
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
}
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
if (rp == NULL) {
|
|
|
|
OCT_LOG_WARNING("Could not connect to %s:%s", addr, port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-05-26 23:52:57 -04:00
|
|
|
int oct_network_node_init(char* port, lua_State* L) {
|
|
|
|
oct_network_node.needs_recv = 1;
|
2023-05-26 21:20:13 -04:00
|
|
|
oct_network_node.needs_send = 0;
|
2023-05-21 18:47:35 -04:00
|
|
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC; // ipv4 or ipv6
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // datagram
|
|
|
|
hints.ai_flags = AI_PASSIVE; // any IP address
|
|
|
|
hints.ai_protocol = 0; // any protocol
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
|
|
|
struct addrinfo* result;
|
|
|
|
int s = getaddrinfo(NULL, port, &hints, &result);
|
|
|
|
if (s != 0) {
|
|
|
|
//fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
Try each address until we successfully bind(2).
|
|
|
|
If socket(2) (or bind(2)) fails, we (close the socket
|
|
|
|
and) try the next address. */
|
|
|
|
|
|
|
|
struct addrinfo *rp;
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
2023-05-26 21:20:13 -04:00
|
|
|
oct_network_node.sfd = socket(rp->ai_family, rp->ai_socktype,
|
2023-05-21 18:47:35 -04:00
|
|
|
rp->ai_protocol);
|
2023-05-26 21:20:13 -04:00
|
|
|
if (oct_network_node.sfd == -1)
|
2023-05-21 18:47:35 -04:00
|
|
|
continue;
|
|
|
|
|
2023-05-26 21:20:13 -04:00
|
|
|
if (bind(oct_network_node.sfd, rp->ai_addr, rp->ai_addrlen) == 0)
|
2023-05-21 18:47:35 -04:00
|
|
|
break; /* Success */
|
|
|
|
|
2023-05-26 21:20:13 -04:00
|
|
|
close(oct_network_node.sfd);
|
2023-05-21 18:47:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
if (!rp) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-26 23:52:57 -04:00
|
|
|
|
2023-06-02 08:25:00 -04:00
|
|
|
OCT_LOG_INFO("Listening on 127.0.0.1 port %s", port);
|
|
|
|
|
2023-05-26 23:52:57 -04:00
|
|
|
lua_pushcfunction(L, oct_network_recv_msg_lua);
|
|
|
|
lua_setglobal(L, "oct_recv");
|
|
|
|
lua_pushcfunction(L, oct_network_send_msg_lua);
|
|
|
|
lua_setglobal(L, "oct_send");
|
2023-05-21 18:47:35 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2023-05-26 21:20:13 -04:00
|
|
|
|
2023-05-26 23:52:57 -04:00
|
|
|
|
|
|
|
// This runs every loop, filling the receive buffer if needed
|
2023-05-26 21:20:13 -04:00
|
|
|
int oct_network_recv_msg() {
|
2023-05-26 23:52:57 -04:00
|
|
|
if (oct_network_node.needs_recv) {
|
|
|
|
struct sockaddr_storage peer_addr;
|
2023-05-27 17:28:19 -04:00
|
|
|
socklen_t peer_addrlen = sizeof(peer_addr);
|
2023-05-26 23:52:57 -04:00
|
|
|
|
|
|
|
// recvfrom returns -1 if nothing was received
|
|
|
|
// Need MSG_DONTWAIT flag for nonblocking
|
2023-05-27 17:28:19 -04:00
|
|
|
if (recvfrom(oct_network_node.sfd, oct_network_node.recv_buffer, BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen) > 0) {
|
|
|
|
int s = getnameinfo((struct sockaddr *) &peer_addr,
|
|
|
|
peer_addrlen, oct_network_node.recv_addr, NI_MAXHOST, oct_network_node.recv_port, NI_MAXSERV, NI_NUMERICSERV);
|
|
|
|
if (s != 0) {
|
|
|
|
OCT_LOG_WARNING("Received message from unknown host: %s", oct_network_node.recv_buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-05-26 23:52:57 -04:00
|
|
|
oct_network_node.needs_recv = 0;
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 21:20:13 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-26 23:52:57 -04:00
|
|
|
|
2023-05-26 21:20:13 -04:00
|
|
|
int oct_network_send_msg() {
|
2023-05-27 17:28:19 -04:00
|
|
|
if (oct_network_node.needs_send) {
|
2023-06-04 12:20:57 -04:00
|
|
|
int sfd = oct_network_init_socket(oct_network_node.send_addr, oct_network_node.send_port);
|
2023-05-27 17:28:19 -04:00
|
|
|
ssize_t length = strlen(oct_network_node.send_buffer);
|
|
|
|
if (write(sfd, oct_network_node.send_buffer, length) != length) {
|
|
|
|
OCT_LOG_WARNING("Partial write when sending message");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
oct_network_node.needs_send = 0;
|
2023-06-02 08:25:00 -04:00
|
|
|
close(sfd);
|
2023-05-27 17:28:19 -04:00
|
|
|
}
|
|
|
|
return 1;
|
2023-05-26 21:20:13 -04:00
|
|
|
}
|
2023-05-26 23:52:57 -04:00
|
|
|
|
|
|
|
int oct_network_recv_msg_lua(lua_State* L) {
|
|
|
|
if (!oct_network_node.needs_recv) {
|
|
|
|
lua_pushstring(L, oct_network_node.recv_buffer);
|
2023-05-27 17:28:19 -04:00
|
|
|
lua_pushstring(L, oct_network_node.recv_addr);
|
|
|
|
lua_pushstring(L, oct_network_node.recv_port);
|
2023-05-26 23:52:57 -04:00
|
|
|
oct_network_node.needs_recv = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushstring(L, "");
|
2023-05-27 17:28:19 -04:00
|
|
|
lua_pushstring(L, "");
|
|
|
|
lua_pushstring(L, "");
|
2023-05-26 23:52:57 -04:00
|
|
|
}
|
2023-05-27 17:28:19 -04:00
|
|
|
return 3;
|
2023-05-26 23:52:57 -04:00
|
|
|
}
|
|
|
|
int oct_network_send_msg_lua(lua_State* L) {
|
2023-05-27 17:28:19 -04:00
|
|
|
if (!oct_network_node.needs_send) {
|
|
|
|
strncpy(oct_network_node.send_buffer, luaL_checkstring(L, -3), BUFFER_SIZE);
|
|
|
|
strncpy(oct_network_node.send_addr, luaL_checkstring(L, -2), NI_MAXHOST);
|
|
|
|
strncpy(oct_network_node.send_port, luaL_checkstring(L, -1), NI_MAXSERV);
|
|
|
|
oct_network_node.needs_send = 1;
|
|
|
|
}
|
2023-06-04 12:20:57 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void oct_network_ab_init() {
|
|
|
|
oct_network_ab.size = 0;
|
|
|
|
oct_network_ab.first = NULL;
|
|
|
|
oct_network_ab.last = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int oct_network_ab_insert(char* addr, char* port) {
|
|
|
|
// First create the entry
|
|
|
|
struct oct_network_ab_entry* e = (struct oct_network_ab_entry*)malloc(sizeof(struct oct_network_ab_entry));
|
2023-06-08 22:07:58 -04:00
|
|
|
if (!e) {
|
|
|
|
OCT_LOG_ERROR("Could not allocate space for address book entry for %s:%s", addr, port);
|
|
|
|
}
|
|
|
|
|
2023-06-04 12:20:57 -04:00
|
|
|
// Then, create the socket
|
2023-06-08 22:07:58 -04:00
|
|
|
if ((e->sfd = oct_network_init_socket(addr, port)) < 0) {
|
|
|
|
free(e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
e->next = NULL;
|
|
|
|
e->prev = oct_network_ab.last;
|
|
|
|
strncpy(e->addr, addr, NI_MAXHOST);
|
|
|
|
strncpy(e->port, port, NI_MAXSERV);
|
|
|
|
if (oct_network_ab.size == 0) {
|
|
|
|
oct_network_ab.first = e;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
oct_network_ab.last->next = e;
|
|
|
|
}
|
|
|
|
oct_network_ab.last = e;
|
|
|
|
oct_network_ab.size++;
|
2023-06-04 12:20:57 -04:00
|
|
|
|
2023-06-08 22:07:58 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2023-06-04 12:20:57 -04:00
|
|
|
|
2023-06-08 22:07:58 -04:00
|
|
|
int oct_network_ab_remove(struct oct_network_ab_entry* e) {
|
|
|
|
// First, remove node from linked list and fix
|
|
|
|
if (e == oct_network_ab.first) {
|
|
|
|
oct_network_ab.first = e->next;
|
|
|
|
e->next->prev = NULL;
|
|
|
|
}
|
|
|
|
else if (e == oct_network_ab.last) {
|
|
|
|
oct_network_ab.last = e->prev;
|
|
|
|
e->prev->next = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
e->next->prev = e->prev;
|
|
|
|
e->prev->next = e->next;
|
|
|
|
}
|
|
|
|
// Finally, get rid of node
|
|
|
|
close(e->sfd);
|
|
|
|
free(e);
|
|
|
|
oct_network_ab.size--;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void oct_network_ab_deinit() {
|
|
|
|
while (oct_network_ab.first) {
|
|
|
|
oct_network_ab_remove(oct_network_ab.first);
|
|
|
|
}
|
2023-05-26 23:52:57 -04:00
|
|
|
}
|