diff options
Diffstat (limited to 'src/customdhcpcd/logwriter.c')
-rw-r--r-- | src/customdhcpcd/logwriter.c | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/src/customdhcpcd/logwriter.c b/src/customdhcpcd/logwriter.c new file mode 100644 index 0000000..ee8bb1d --- /dev/null +++ b/src/customdhcpcd/logwriter.c @@ -0,0 +1,260 @@ +#include <arpa/inet.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <ctype.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> + +#include "common.h" +#include "dhcp.h" +#include "dhcpcd.h" +#include "errno.h" +#include "info.h" +#include "logger.h" +#include "logwriter.h" + +#include "../common/fbgui.h" // for constants +/*sockets for the logger and the qt-reader */ +int sockfd, ns; +int retval = -1; +char socketName[QTSOCKETADDRESSLENGTH]; +char interfaceName[IF_NAMESIZE]; + +void setSocketName(const char * sn) { + snprintf(socketName, sizeof(socketName), "%s", sn); +} + +void setInterfaceName(const char * in) { + snprintf(interfaceName, sizeof(interfaceName), "%s", in); +} + +int initQtLoggerSocket() { + /** + * new code. seems to be right. + */ + + struct sockaddr_un serv_addr; + fprintf(stdout, "start init \n"); + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sockfd < 0) { + fprintf(stdout, "ERROR opening socket \n"); + retval = sockfd; + return sockfd; + } + serv_addr.sun_family = AF_UNIX; + strcpy(serv_addr.sun_path, socketName); + + retval = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); + if (retval < 0) + fprintf(stdout, "ERROR connecting \n"); + fprintf(stdout, "init Qt Logger Socket done \n"); + return retval; +} + +void closeQtLoggerSocket() { + close(sockfd); +} + +void sendToQt(log_msg * msg) { + int n = -1; + int t; + int ret; + const char *tpl = "%s;%d;%d;%s\n"; + char outbuf[DHCP_MESSAGE_SIZE]; + char ack[ACK_SIZE]; + /* + size_t outbuf_size = sizeof(char) * 4 + // ";" *3 + newline + sizeof(int) * 2 + // status, substatus + sizeof(msg->device) + // devicename + sizeof(msg->msg); // msg + outbuf = malloc(outbuf_size); + memset(outbuf, 0, outbuf_size); + snprintf(outbuf, sizeof(char) * 3 + sizeof(int) * 2 + sizeof(msg->device) + + sizeof(msg->msg), tpl, msg->device, msg->status, msg->substatus, + msg->msg); + */ + memset(outbuf, '\0', DHCP_MESSAGE_SIZE); + ret = snprintf(outbuf, DHCP_MESSAGE_SIZE, tpl, msg->device, msg->status, + msg->substatus, msg->msg); + if (ret < 1) { + log ger(LOG_INFO, "[fbgui] ERROR filling message buffer"); + //syslog(LOG_INFO, "[fbgui] ERROR filling message buffer"); + return; + } + if (outbuf != NULL) { + n = send(sockfd, outbuf, DHCP_MESSAGE_SIZE, 0); + } + syslog(LOG_INFO, "[fbgui] INFO writing to socket: [%d:%d] %s (%s)", + msg->status, msg->substatus, msg->msg, msg->device); + + if (n <= 0) { + logger(LOG_ERR, "[fbgui] ERROR writing to socket: [%d:%d] %s (%s)", + msg->status, msg->substatus, msg->msg, msg->device); + //syslog(LOG_ERR, "[fbgui] ERROR writing to socket: [%d:%d] %s (%s)", + // msg->status, msg->substatus, msg->msg, msg->device); + } + /* + memset(ack, 0, ACK_SIZE); + if ((t = recv(sockfd, ack, ACK_SIZE, 0)) > 0) { + syslog(LOG_ERR, "[fbgui] recv ack echo> %s", ack); + printf("received: %s\n", ack); + } else { + if (t < 0) + syslog(LOG_ERR, "[fbgui] ERROR receiving from socket"); + else + syslog(LOG_ERR, "[fbgui] ERROR Server closed"); + } + */ +} + + + +void logToQt(int status, int substatus, const char * msg) { + if (retval >= 0) { + log_msg lm; + lm.status = status; + lm.substatus = substatus; + snprintf(lm.msg, sizeof(lm.msg), "%s", msg); + snprintf(lm.device, sizeof(lm.device), "%s", interfaceName); + sendToQt(&lm); + } +} + + + +void logSendToQt(int type) { + switch (type) { + case DHCP_DISCOVER: + logToQt(LOG_INFO, DHCP_DISCOVER, "send discover"); + break; + case DHCP_OFFER: + logToQt(LOG_INFO, DHCP_OFFER, "send offer"); + break; + case DHCP_REQUEST: + logToQt(LOG_INFO, DHCP_REQUEST, "send request"); + break; + case DHCP_DECLINE: + logToQt(LOG_INFO, DHCP_DECLINE, "send decline"); + break; + case DHCP_ACK: + logToQt(LOG_INFO, DHCP_ACK, "send ack"); + break; + case DHCP_NAK: + logToQt(LOG_INFO, DHCP_NAK, "send nak"); + break; + case DHCP_RELEASE: + logToQt(LOG_INFO, DHCP_RELEASE, "send release"); + break; + case DHCP_INFORM: + logToQt(LOG_INFO, DHCP_INFORM, "send inform"); + break; + default: + break; + } +} + + + +static void print_addresses (FILE *f, const struct address_head *addresses) +{ + const address_t *addr; + + STAILQ_FOREACH (addr, addresses, entries) { + fprintf (f, "%s", inet_ntoa (addr->address)); + if (STAILQ_NEXT (addr, entries)) + fprintf (f, " "); + } +} + + + +void logGatewayToFile(const interface_t *iface, const dhcp_t *dhcp) { + /*void logGatewayToFile(const interface_t iface, const dhcp_t *dhcp, + const options_t options)*/ + //char path[QTSOCKETADDRESSLENGTH]; + + /* + strcpy(path, DEFAULT_GATEWAY_INFO_LOCATION); + strcat(path, iface.name); + strcpy(iface.infofile, path); + options.test = false; + syslog(LOG_INFO, "[fbgui] try to open file: %s", iface.infofile); + write_info(&iface, dhcp, &options, true); + */ + FILE *f; + route_t *route; + char path[QTSOCKETADDRESSLENGTH]; + + strcpy(path, DEFAULT_INTERFACE_CONF_LOCATION); + strcat(path, iface->name); + + syslog(LOG_INFO, "[fbgui] try to open file: %s", path); + + logger(LOG_DEBUG, "writing %s", iface->infofile); + if ((f = fopen(path, "w")) == NULL) { + logger(LOG_ERR, "fopen `%s': %s", path, strerror(errno)); + //TODO: exit/return .. + return; + } + + if (dhcp->address.s_addr) { + struct in_addr n; + n.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; + fprintf(f, "IPADDR='%s'\n", inet_ntoa(dhcp->address)); + fprintf(f, "NETMASK='%s'\n", inet_ntoa(dhcp->netmask)); + fprintf(f, "NETWORK='%s'\n", inet_ntoa(n)); + fprintf(f, "BROADCAST='%s'\n", inet_ntoa(dhcp->broadcast)); + } + + if (dhcp->routes) { + bool doneone = false; + fprintf(f, "ROUTES='"); + STAILQ_FOREACH (route, dhcp->routes, entries) { + if (route->destination.s_addr != 0) { + if (doneone) + fprintf(f, " "); + fprintf(f, "%s", inet_ntoa(route->destination)); + fprintf(f, ",%s", inet_ntoa(route->netmask)); + fprintf(f, ",%s", inet_ntoa(route->gateway)); + doneone = true; + } + } + fprintf(f, "'\n"); + + doneone = false; + fprintf(f, "GATEWAYS='"); + STAILQ_FOREACH (route, dhcp->routes, entries) { + if (route->destination.s_addr == 0) { + if (doneone) + fprintf(f, " "); + fprintf(f, "%s", inet_ntoa(route->gateway)); + doneone = true; + } + } + fprintf(f, "'\n"); + } + + fprintf(f, "HOSTNAME='%s", dhcp->hostname); + fprintf(f, "'\n"); + fprintf(f, "DNSSEARCH='%s", dhcp->dnssearch); + fprintf(f, "'\n"); + + if (dhcp->dnsservers) { + fprintf(f, "DNSSERVERS='"); + print_addresses(f, dhcp->dnsservers); + fprintf(f, "'\n"); + } + fprintf(f, "INTERFACE='%s", iface->name); + fprintf(f, "'\n"); + if (iface->clientid_len > 0) { + fprintf(f, "CLIENTID='%s'\n", hwaddr_ntoa(iface->clientid, + iface->clientid_len)); + } + fprintf(f, "DHCPCHADDR='%s'\n", hwaddr_ntoa(iface->hwaddr, iface->hwlen)); + fclose(f); +} |