Mercurial
diff seobeo/s_linux_network.c @ 1:adcfad6e86fb
Updated naming and separated out some logic within seobeo.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Sep 2025 09:11:20 -0700 |
| parents | |
| children | 2758f5527d2b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/seobeo/s_linux_network.c Wed Sep 24 09:11:20 2025 -0700 @@ -0,0 +1,100 @@ +#include "seobeo/seobeo.h" + +int Seobeo_CreateSocket(int32 stream, char* port, int32 backlog) +{ + int32 sock_fd; + struct addrinfo hints, *server_infos, *free_server_info; + int32 yes = 1; // Need this for setsockopt + + memset(&hints, 0, sizeof(hints)); + if (stream) + { + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + } + else + { + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = IPPROTO_UDP; + hints.ai_flags = AI_PASSIVE; + } + + + if (getaddrinfo(NULL, port, &hints, &server_infos) != 0) + { + perror("getaddrinfo"); + return -1; + } + + for + ( + free_server_info = server_infos; + free_server_info != NULL; + free_server_info = free_server_info->ai_next + ) + { + if + ( + (sock_fd = socket( + free_server_info->ai_family, free_server_info->ai_socktype, + free_server_info->ai_protocol + )) == -1 + ) + { + perror("socket"); + continue; + } + + if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) + { + perror("setsockopt"); + continue; + } + + if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) + { + close(sock_fd); + perror("setsockopt"); + continue; + } + + // UDP should be non blocking + if(!stream) + { + if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) != 0) + { + close(sock_fd); + perror("v_network: Couldn't make socket non-blocking\n"); + return -1; + } + } + + // binded to a open server infos; + break; + } + + // No longer need these values + freeaddrinfo(server_infos); + + if (free_server_info == NULL) + { + perror("No free server"); + return -1; + } + + // TCP listen + if(stream) + { + if (listen(sock_fd, backlog) != 0) + { + perror("listen"); + return -1; + } + } + + return sock_fd; +} +