2022-09-19 18:41:11 +00:00
|
|
|
/* copyright (c) 2022 grunfink - MIT license */
|
|
|
|
|
|
|
|
#ifndef _XS_SOCKET_H
|
|
|
|
|
|
|
|
#define _XS_SOCKET_H
|
|
|
|
|
2022-10-07 13:06:17 +00:00
|
|
|
int xs_socket_timeout(int s, double rto, double sto);
|
2022-10-08 05:00:05 +00:00
|
|
|
int xs_socket_server(const char *addr, int port);
|
2022-09-19 18:41:11 +00:00
|
|
|
FILE *xs_socket_accept(int rs);
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef XS_IMPLEMENTATION
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
|
2022-10-07 13:06:17 +00:00
|
|
|
int xs_socket_timeout(int s, double rto, double sto)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* sets the socket timeout in seconds */
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (rto > 0.0) {
|
|
|
|
tv.tv_sec = (int)rto;
|
2022-10-07 13:06:17 +00:00
|
|
|
tv.tv_usec = (int)((rto - (double)(int)rto) * 1000000.0);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sto > 0.0) {
|
|
|
|
tv.tv_sec = (int)sto;
|
2022-10-07 13:06:17 +00:00
|
|
|
tv.tv_usec = (int)((sto - (double)(int)sto) * 1000000.0);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
ret = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 05:00:05 +00:00
|
|
|
int xs_socket_server(const char *addr, int port)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* opens a server socket */
|
|
|
|
{
|
|
|
|
int rs = -1;
|
|
|
|
struct sockaddr_in host;
|
|
|
|
|
|
|
|
memset(&host, '\0', sizeof(host));
|
|
|
|
|
|
|
|
if (addr != NULL) {
|
|
|
|
struct hostent *he;
|
|
|
|
|
|
|
|
if ((he = gethostbyname(addr)) != NULL)
|
|
|
|
memcpy(&host.sin_addr, he->h_addr_list[0], he->h_length);
|
|
|
|
else
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
host.sin_family = AF_INET;
|
|
|
|
host.sin_port = htons(port);
|
|
|
|
|
|
|
|
if ((rs = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
|
|
|
|
/* reuse addr */
|
|
|
|
int i = 1;
|
|
|
|
setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i));
|
|
|
|
|
|
|
|
if (bind(rs, (struct sockaddr *)&host, sizeof(host)) == -1) {
|
|
|
|
close(rs);
|
|
|
|
rs = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
listen(rs, SOMAXCONN);
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE *xs_socket_accept(int rs)
|
|
|
|
/* accepts an incoming connection */
|
|
|
|
{
|
|
|
|
int cs = -1;
|
2022-10-05 10:08:17 +00:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t l = sizeof(addr);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-10-05 10:08:17 +00:00
|
|
|
cs = accept(rs, (struct sockaddr *)&addr, &l);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return cs == -1 ? NULL : fdopen(cs, "r+");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* XS_IMPLEMENTATION */
|
|
|
|
|
|
|
|
#endif /* _XS_SOCKET_H */
|