add: relay/piface support
This commit is contained in:
parent
fa6ceb2bf4
commit
db64e4f820
34 changed files with 1259 additions and 313 deletions
51
helpers/bind_server.c
Normal file
51
helpers/bind_server.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <helpers.h>
|
||||
|
||||
int
|
||||
helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog)
|
||||
{
|
||||
char port_str[6];
|
||||
sprintf(port_str, "%d", port);
|
||||
|
||||
struct addrinfo hints, *res;
|
||||
int fd;
|
||||
int status;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if ((status = getaddrinfo(addr, port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_ERROR("getaddrinfo: %s", gai_strerror(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
|
||||
if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
|
||||
{
|
||||
LOG_ERROR("error binding socket: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((status = listen(fd, max_client_backlog)) == -1)
|
||||
{
|
||||
LOG_ERROR("error setting up listener: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
return fd;
|
||||
}
|
|
@ -5,33 +5,35 @@
|
|||
#include <sys/socket.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <helper.h>
|
||||
#include <helpers.h>
|
||||
|
||||
int
|
||||
helper_connect_server(char* host, char* port)
|
||||
helper_connect_tcp_server(char* host, uint16_t port)
|
||||
{
|
||||
int s, status;
|
||||
struct addrinfo hints, *res;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care)
|
||||
hints.ai_socktype = SOCK_STREAM; //set socket flag
|
||||
char port_str[6];
|
||||
sprintf(port_str, "%d", port);
|
||||
|
||||
if ((status = getaddrinfo(host, port, &hints, &res)) != 0) { //getaddrinfo() will evaluate the given address, using the hints-flags and port, and return an IP address and other server infos
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int s, status;
|
||||
struct addrinfo hints, *res;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care)
|
||||
hints.ai_socktype = SOCK_STREAM; //set socket flag
|
||||
|
||||
//res got filled out by getaddrinfo() for us
|
||||
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
|
||||
if ((status = getaddrinfo(host, port_str, &hints, &res)) != 0) { //getaddrinfo() will evaluate the given address, using the hints-flags and port, and return an IP address and other server infos
|
||||
LOG_ERROR("getaddrinfo: %s\n", gai_strerror(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0) {
|
||||
fprintf(stderr, "Keine Verbindung mit dem Netzwerk möglich.\n");
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//res got filled out by getaddrinfo() for us
|
||||
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
|
||||
|
||||
freeaddrinfo(res);
|
||||
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0) {
|
||||
LOG_ERROR("connect() failed");
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return s;
|
||||
freeaddrinfo(res);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
|
24
helpers/get_port.c
Normal file
24
helpers/get_port.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <helpers.h>
|
||||
#include <logger.h>
|
||||
|
||||
uint16_t
|
||||
helper_get_port(int sock)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
|
||||
{
|
||||
LOG_ERROR("could not get socket name for port: %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ntohs(sin.sin_port);
|
||||
}
|
||||
}
|
57
helpers/open_discovery_socket.c
Normal file
57
helpers/open_discovery_socket.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <helpers.h>
|
||||
|
||||
int
|
||||
helper_open_discovery_socket(uint16_t discovery_port)
|
||||
{
|
||||
struct addrinfo hints, *res;
|
||||
int fd, status;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; // use ipv4
|
||||
hints.ai_socktype = SOCK_DGRAM; //set socket flag
|
||||
hints.ai_flags = AI_PASSIVE; // get my IP
|
||||
|
||||
char discovery_port_str[6];
|
||||
sprintf(discovery_port_str, "%u", discovery_port);
|
||||
|
||||
//get connection info for our computer
|
||||
if ((status = getaddrinfo(NULL, discovery_port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_FATAL("getaddrinfo: %s", gai_strerror(status));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
//creating socket
|
||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
int yes = 1;
|
||||
|
||||
// lose the pesky "Address already in use" error message
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
|
||||
{
|
||||
LOG_FATAL("setsockopt: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1)
|
||||
{
|
||||
LOG_FATAL("bind: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
LOG_INFO("opened discovery socket on port %u", discovery_port);
|
||||
|
||||
return fd;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue