Add run time options to RPi gateway

This commit is contained in:
Marcelo Aquino
2016-09-08 15:56:37 -03:00
parent b4f4e28545
commit 09103e9c04
13 changed files with 178 additions and 77 deletions
+12 -11
View File
@@ -28,6 +28,7 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "log.h"
#include "EthernetClient.h"
#include "EthernetServer.h"
@@ -56,26 +57,26 @@ void EthernetServer::begin(IPAddress address)
sprintf(portstr, "%d", port);
if ((rv = getaddrinfo(address.toString().c_str(), portstr, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
mys_log(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(rv));
return;
}
// loop through all the results and bind to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("socket");
mys_log(LOG_ERR, "socket: %s\n", strerror(errno));
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
mys_log(LOG_ERR, "setsockopt: %s\n", strerror(errno));
freeaddrinfo(servinfo);
return;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("bind");
mys_log(LOG_ERR, "bind: %s\n", strerror(errno));
continue;
}
@@ -83,13 +84,13 @@ void EthernetServer::begin(IPAddress address)
}
if (p == NULL) {
fprintf(stderr, "failed to bind\n");
mys_log(LOG_ERR, "failed to bind\n");
freeaddrinfo(servinfo);
return;
}
if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) {
perror("listen");
mys_log(LOG_ERR, "listen: %s\n", strerror(errno));
freeaddrinfo(servinfo);
return;
}
@@ -101,7 +102,7 @@ void EthernetServer::begin(IPAddress address)
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
void *addr = &(ipv4->sin_addr);
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
ETHERNETSERVER_DEBUG("Listening for connections on %s:%s\n", ipstr, portstr);
mys_log(LOG_DEBUG, "Listening for connections on %s:%s\n", ipstr, portstr);
}
bool EthernetServer::hasClient()
@@ -141,7 +142,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
client.stop();
clients[i] = clients.back();
clients.pop_back();
ETHERNETSERVER_DEBUG("Client disconnected.");
mys_log(LOG_DEBUG, "Client disconnected.");
}
}
@@ -191,7 +192,7 @@ void EthernetServer::_accept()
}
}
if (no_free_slots) {
ETHERNETSERVER_DEBUG("Max number of ethernet clients reached.");
mys_log(LOG_DEBUG, "Max number of ethernet clients reached.");
return;
}
}
@@ -200,7 +201,7 @@ void EthernetServer::_accept()
new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
if (new_fd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
perror("accept");
mys_log(LOG_ERR, "accept: %s\n", strerror(errno));
}
return;
}
@@ -210,5 +211,5 @@ void EthernetServer::_accept()
void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr);
inet_ntop(client_addr.ss_family, addr, ipstr, sizeof ipstr);
ETHERNETSERVER_DEBUG("New connection from %s\n", ipstr);
mys_log(LOG_DEBUG, "New connection from %s\n", ipstr);
}