Seems to work, need to migrate to the UDP api however

This commit is contained in:
Quentin 2019-02-12 22:07:47 +01:00
parent 0e6be80fcb
commit d0386fb948
3 changed files with 32 additions and 3 deletions

View file

@ -41,6 +41,21 @@ failed_socks5:
}
}
void init_udp_server(struct donar_client_ctx* ctx, char* port) {
int sock1, sock2;
sock1 = create_udp_server(port);
if (sock1 < 0) goto socket_failed;
sock2 = dup(sock1);
if (sock2 < 0) goto socket_failed;
evt_core_add_fd (&(ctx->evts), "udp-read", sock1);
evt_core_add_fd (&(ctx->evts), "udp-write", sock2);
return;
socket_failed:
fprintf(stderr, "Socket initialization for the UDP server failed\n");
exit(EXIT_FAILURE);
}
void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, char* port) {
evt_core_init (&(ctx->evts));
evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co));
@ -54,5 +69,10 @@ void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* on
printf("--- Onion services loaded\n");
init_tcp_clients(ctx);
printf("--- TCP Clients Connected\n");
// TODO: Debug that, listen UDP and launch loop
init_udp_server (ctx, port);
printf("--- UDP Server is listening\n");
evt_core_loop(&(ctx->evts));
tor_os_free (&(ctx->tos));
}

View file

@ -51,14 +51,14 @@ int create_udp_client(char* host, char* service) {
return create_ip_client (host, service, SOCK_DGRAM);
}
int create_tcp_server(char* service) {
int create_ip_server(char* service, int type) {
int err, sock, enable;
struct addrinfo conf;
struct addrinfo *result, *cursor;
memset(&conf, 0, sizeof(struct addrinfo));
conf.ai_family = AF_INET; // AF_UNSPEC to listen on IPv6 or IPv4
conf.ai_socktype = SOCK_STREAM;
conf.ai_socktype = type;
conf.ai_flags = 0; // AI_PASSIVE to listen on 0.0.0.0
conf.ai_protocol = 0;
@ -92,6 +92,14 @@ int create_tcp_server(char* service) {
return sock;
}
int create_tcp_server(char* service) {
return create_ip_server (service, SOCK_STREAM);
}
int create_udp_server(char* service) {
return create_ip_server (service, SOCK_DGRAM);
}
/*
* The idea is to get the file descriptor flags
* then we add to the bitmask O_NONBLOCK and set the

View file

@ -11,6 +11,7 @@
int create_tcp_client(char* host, char* service);
int create_udp_client(char* host, char* service);
int create_tcp_server(char* service);
int create_udp_server(char* service);
int make_socket_non_blocking(int fd);
void add_fd_to_epoll(int epollfd, int fd, uint32_t flags);
int read_entity(int fd, void* entity, int size);