snac2/xs_socket.h

217 lines
4.8 KiB
C
Raw Permalink Normal View History

2024-01-04 08:22:03 +00:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2022-09-19 18:41:11 +00:00
#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);
2023-09-23 20:02:52 +00:00
int xs_socket_server(const char *addr, const char *serv);
2024-06-20 15:38:02 +00:00
int xs_socket_accept(int rs);
2023-12-27 11:54:38 +00:00
int _xs_socket_peername(int s, char *buf, int buf_size);
2023-09-04 09:13:40 +00:00
int xs_socket_connect(const char *addr, const char *serv);
2022-09-19 18:41:11 +00:00
2023-12-27 11:54:38 +00:00
#ifdef _XS_H
xs_str *xs_socket_peername(int s);
#endif
2022-09-19 18:41:11 +00:00
#ifdef XS_IMPLEMENTATION
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
2023-03-08 03:28:20 +00:00
#include <arpa/inet.h>
2023-12-27 11:54:38 +00:00
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
2022-09-19 18:41:11 +00:00
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;
}
2023-09-23 20:02:52 +00:00
int xs_socket_server(const char *addr, const char *serv)
2023-09-04 09:13:40 +00:00
/* opens a server socket by service name (or port as string) */
2022-09-19 18:41:11 +00:00
{
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;
}
2023-09-04 09:13:40 +00:00
struct servent *se;
if ((se = getservbyname(serv, "tcp")) != NULL)
host.sin_port = se->s_port;
else
host.sin_port = htons(atoi(serv));
2022-09-19 18:41:11 +00:00
host.sin_family = AF_INET;
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;
}
2024-06-20 15:38:02 +00:00
int xs_socket_accept(int rs)
2022-09-19 18:41:11 +00:00
/* accepts an incoming connection */
{
struct sockaddr_storage addr;
socklen_t l = sizeof(addr);
2022-09-19 18:41:11 +00:00
2024-06-20 15:38:02 +00:00
return accept(rs, (struct sockaddr *)&addr, &l);
2022-09-19 18:41:11 +00:00
}
2023-03-08 03:28:20 +00:00
2023-12-27 11:54:38 +00:00
int _xs_socket_peername(int s, char *buf, int buf_size)
/* fill the buffer with the socket peername */
2023-03-08 03:28:20 +00:00
{
struct sockaddr_storage addr;
socklen_t slen = sizeof(addr);
2023-12-27 11:54:38 +00:00
const char *p = NULL;
2023-03-08 03:28:20 +00:00
if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) {
if (addr.ss_family == AF_INET) {
struct sockaddr_in *sa = (struct sockaddr_in *)&addr;
2023-12-27 11:54:38 +00:00
p = inet_ntop(AF_INET, &sa->sin_addr, buf, buf_size);
2023-03-08 03:28:20 +00:00
}
else
if (addr.ss_family == AF_INET6) {
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr;
2023-12-27 11:54:38 +00:00
p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, buf_size);
2023-03-08 03:28:20 +00:00
}
}
2023-12-27 11:54:38 +00:00
return p != NULL;
2023-03-08 03:28:20 +00:00
}
2023-09-04 09:13:40 +00:00
int xs_socket_connect(const char *addr, const char *serv)
/* creates a client connection socket */
{
int d = -1;
#ifndef WITHOUT_GETADDRINFO
struct addrinfo *res;
struct addrinfo hints;
memset(&hints, '\0', sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG;
if (getaddrinfo(addr, serv, &hints, &res) == 0) {
struct addrinfo *r;
for (r = res; r != NULL; r = r->ai_next) {
d = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
if (d != -1) {
if (connect(d, r->ai_addr, r->ai_addrlen) == 0)
break;
close(d);
d = -1;
}
}
freeaddrinfo(res);
}
#else /* WITHOUT_GETADDRINFO */
/* traditional socket interface */
struct hostent *he;
if ((he = gethostbyname(addr)) != NULL) {
struct sockaddr_in host;
memset(&host, '\0', sizeof(host));
memcpy(&host.sin_addr, he->h_addr_list[0], he->h_length);
host.sin_family = he->h_addrtype;
struct servent *se;
if ((se = getservbyname(serv, "tcp")) != NULL)
host.sin_port = se->s_port;
else
host.sin_port = htons(atoi(serv));
if ((d = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
if (connect(d, (struct sockaddr *)&host, sizeof(host)) == -1) {
close(d);
2023-09-04 09:13:40 +00:00
d = -1;
}
2023-09-04 09:13:40 +00:00
}
}
#endif /* WITHOUT_GETADDRINFO */
return d;
}
2023-12-27 11:54:38 +00:00
#ifdef _XS_H
xs_str *xs_socket_peername(int s)
/* returns the remote address as a string */
{
char buf[2028];
xs_str *p = NULL;
if (_xs_socket_peername(s, buf, sizeof(buf)))
p = xs_str_new(buf);
return p;
}
#endif /* _XS_H */
2022-09-19 18:41:11 +00:00
#endif /* XS_IMPLEMENTATION */
2023-09-04 09:13:40 +00:00
#endif /* _XS_SOCKET_H */