From 635d6f901892cb7df028eb41f3d727e569a3af7e Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 14 Jul 2011 16:59:40 +0200 Subject: customdhcpcd is now compiling and can communicate with the LogReceiver started to implement the QProcess handling (parallel calls of dhcpcd out of fbgui/LogReceiver) --- workspace/LogReceiver/LogReceiver | Bin 35272 -> 35272 bytes workspace/LogReceiver/logreceiver.cpp | 57 +++++- workspace/LogReceiver/logreceiver.h | 9 +- workspace/customdhcpcd/src/dhcpcd.c | 329 +++++++++++++++++++++++++++++++-- workspace/customdhcpcd/src/dhcpcd.h | 9 + workspace/customdhcpcd/src/logger.h | 7 +- workspace/customdhcpcd/src/logwriter.c | 111 ++++++----- workspace/customdhcpcd/src/logwriter.h | 10 +- workspace/customdhcpcd/src/status.h | 3 +- 9 files changed, 454 insertions(+), 81 deletions(-) diff --git a/workspace/LogReceiver/LogReceiver b/workspace/LogReceiver/LogReceiver index 4c9ccec..4fa7468 100755 Binary files a/workspace/LogReceiver/LogReceiver and b/workspace/LogReceiver/LogReceiver differ diff --git a/workspace/LogReceiver/logreceiver.cpp b/workspace/LogReceiver/logreceiver.cpp index 29ea8d8..5c695e5 100644 --- a/workspace/LogReceiver/logreceiver.cpp +++ b/workspace/LogReceiver/logreceiver.cpp @@ -1,6 +1,8 @@ #include + #include #include -#include + #include + #include #include @@ -21,7 +23,7 @@ LogReceiver::LogReceiver(QWidget *parent) : quitButton->setAutoDefault(false); server = new QLocalServer(this); - if (!server->listen("/var/tmp/qt_c_socket_test")) { + if (!server->listen("/var/tmp/qt_c_socket_default")) { QMessageBox::critical(this, tr("LogReceiver"), tr( "Unable to start the server: %1.") .arg(server->errorString())); close(); @@ -70,9 +72,10 @@ void LogReceiver::handleNewInput() { QByteArray data = client->readAll(); QString logMsg(data); - QString s_state = logMsg.section(";", 0, 0); - QString s_subState = logMsg.section(";", 1, 1); - QString msg = logMsg.section(";", 2, 2); + QString interface = logMsg.section(";",0,0); + QString s_state = logMsg.section(";", 1, 1); + QString s_subState = logMsg.section(";", 2, 2); + QString msg = logMsg.section(";", 3, 3); qDebug() << logMsg; @@ -122,3 +125,47 @@ void LogReceiver::handleNewInput() { statusLabel->setText(logMsg); } +QList LogReceiver::getListOfNetworkInterfaces() { + QList nIList = QNetworkInterface::allInterfaces(); + QList result; + foreach(QNetworkInterface nI, nIList) { + if (((!(nI.flags() & QNetworkInterface::CanBroadcast)|| + nI.flags() & QNetworkInterface::IsLoopBack) || + nI.flags() & QNetworkInterface::IsPointToPoint)) + { + continue; + } + qDebug() << nI.humanReadableName(); + result.append(nI); + } + return result; +} + +void LogReceiver::runDHCPCD(QList &interfaces) { + foreach(QNetworkInterface ni, interfaces) { + QProcess * p = new QProcess(this); + clientProcesses.insert(p->pid(),p); + p->start(pathToDhcpcdExe,dhcpcdArguments); + connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); + connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), + this, SLOT(handleProcessFinished(int, QProcess::ExitStatus))); + } +} + +void LogReceiver::handleProcessFinished(int exitCode, + QProcess::ExitStatus exitStatus) { + + QObject* sender = const_cast (QObject::sender()); + QProcess* process = static_cast (sender); + + QProcess * client = clientProcesses.value(process->pid()); +} + +void LogReceiver::handleProcessStarted() { + + QObject* sender = const_cast (QObject::sender()); + QProcess* process = static_cast (sender); + + QProcess * client = clientProcesses.value(process->pid()); +} + diff --git a/workspace/LogReceiver/logreceiver.h b/workspace/LogReceiver/logreceiver.h index f73a56a..0c9df48 100644 --- a/workspace/LogReceiver/logreceiver.h +++ b/workspace/LogReceiver/logreceiver.h @@ -20,15 +20,22 @@ public: private slots: void handleNewConnection(); void handleNewInput(); + void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); + void handleProcessStarted(); private: Ui::LogReceiverClass ui; QLabel *statusLabel; QPushButton *quitButton; QLocalServer *server; - QStringList fortunes; quint16 blockSize; QMap clients; + QMap clientProcesses; + QString pathToDhcpcdExe; + QStringList dhcpcdArguments; + + void runDHCPCD(QList &interfaces); + void getListOfNetworkInterfaces(); }; #endif // LOGRECEIVER_H diff --git a/workspace/customdhcpcd/src/dhcpcd.c b/workspace/customdhcpcd/src/dhcpcd.c index fca0bd4..1fc9040 100644 --- a/workspace/customdhcpcd/src/dhcpcd.c +++ b/workspace/customdhcpcd/src/dhcpcd.c @@ -68,6 +68,7 @@ static const struct option longopts[] = { {"metric", required_argument, NULL, 'm'}, {"renew", no_argument, NULL, 'n'}, {"persistent", no_argument, NULL, 'p'}, + {"qtsocketaddress", required_argument, NULL, 'q'}, {"inform", optional_argument, NULL, 's'}, {"request", optional_argument, NULL, 'r'}, {"timeout", required_argument, NULL, 't'}, @@ -142,11 +143,11 @@ static void usage (void) printf ("usage: "PACKAGE" [-adknpEGHMNRSTY] [-c script] [-h hostname] [-i classID]\n" " [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n" " [-t timeout] [-u userclass] [-F none | ptr | both]\n" - " [-I clientID] \n"); + " [-I clientID] [-q qtsocketaddress] \n"); } -int nd_main(char *ifname) +int main (int argc, char **argv) { options_t *options; int userclasses = 0; @@ -182,6 +183,10 @@ int nd_main(char *ifname) options->doipv4ll = true; options->doduid = true; options->timeout = DEFAULT_TIMEOUT; + /* added by Niklas Goby, additional field, storing the socket address path for + * communicating with Qt "server" + * defined in dhcpcd.h */ + strcpy(options->qtsocketaddress, DEFAULT_QTSOCKETADDRESS); gethostname (options->hostname, sizeof (options->hostname)); if (strcmp (options->hostname, "(none)") == 0 || @@ -189,7 +194,287 @@ int nd_main(char *ifname) memset (options->hostname, 0, sizeof (options->hostname)); -/* + /* Don't set any optional arguments here so we retain POSIX + * compatibility with getopt */ + while ((opt = getopt_long(argc, argv, EXTRA_OPTS + "c:dh:i:kl:m:npr:s:t:u:xAEF:GHI:LMNRSTY", + longopts, &option_index)) != -1) + { + switch (opt) { + case 0: + if (longopts[option_index].flag) + break; + logger (LOG_ERR, + "option `%s' should set a flag", + longopts[option_index].name); + goto abort; + case 'c': + options->script = optarg; + break; + case 'd': + debug++; + switch (debug) { + case 1: + setloglevel (LOG_DEBUG); + break; + case 2: + options->daemonise = false; + break; + } + break; + #ifdef THERE_IS_NO_FORK + case 'f': + options->daemonised = true; + close_fds (); + break; + case 'g': + dhcpcd_skiproutes = xstrdup (optarg); + break; + #endif + case 'h': + if (! optarg) + *options->hostname = '\0'; + else if (strlen (optarg) > MAXHOSTNAMELEN) { + logger (LOG_ERR, + "`%s' too long for HostName string, max is %d", + optarg, MAXHOSTNAMELEN); + goto abort; + } else + strlcpy (options->hostname, optarg, + sizeof (options->hostname)); + break; + case 'i': + if (! optarg) { + *options->classid = '\0'; + } else if (strlen (optarg) > CLASS_ID_MAX_LEN) { + logger (LOG_ERR, + "`%s' too long for ClassID string, max is %d", + optarg, CLASS_ID_MAX_LEN); + goto abort; + } else + strlcpy (options->classid, optarg, + sizeof (options->classid)); + break; + case 'k': + sig = SIGHUP; + break; + case 'l': + if (*optarg == '-') { + logger (LOG_ERR, + "leasetime must be a positive value"); + goto abort; + } + errno = 0; + options->leasetime = (uint32_t) strtol (optarg, NULL, 0); + if (errno == EINVAL || errno == ERANGE) { + logger (LOG_ERR, "`%s' out of range", optarg); + goto abort; + } + break; + case 'm': + options->metric = atoint (optarg); + if (options->metric < 0) { + logger (LOG_ERR, + "metric must be a positive value"); + goto abort; + } + break; + case 'n': + sig = SIGALRM; + break; + case 'p': + options->persistent = true; + break; + case 's': + options->doinform = true; + options->doarp = false; + if (! optarg || strlen (optarg) == 0) { + options->request_address.s_addr = 0; + break; + } else { + char *slash = strchr (optarg, '/'); + if (slash) { + int cidr; + /* nullify the slash, so the -r option can read the + * address */ + *slash++ = '\0'; + if (sscanf (slash, "%d", &cidr) != 1 || + inet_cidrtoaddr (cidr, &options->request_netmask) != 0) { + logger (LOG_ERR, "`%s' is not a valid CIDR", slash); + goto abort; + } + } + } + /* FALLTHROUGH */ + case 'r': + if (! options->doinform) + options->dorequest = true; + if (strlen (optarg) > 0 && + ! inet_aton (optarg, &options->request_address)) + { + logger (LOG_ERR, "`%s' is not a valid IP address", optarg); + goto abort; + } + break; + case 't': + options->timeout = atoint (optarg); + if (options->timeout < 0) { + logger (LOG_ERR, "timeout must be a positive value"); + goto abort; + } + break; + case 'q': + if (strlen(optarg) > QTSOCKETADDRESSLENGTH) { + logger(LOG_ERR, "`%s' too long for an socket address path (max=%d)", + optarg, QTSOCKETADDRESSLENGTH); + goto abort; + } + strlcpy(options->qtsocketaddress, optarg, sizeof(options->qtsocketaddress)); + break; + case 'u': + { + int offset = 0; + for (i = 0; i < userclasses; i++) + offset += (int) options->userclass[offset] + 1; + if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) { + logger (LOG_ERR, "userclass overrun, max is %d", + USERCLASS_MAX_LEN); + goto abort; + } + userclasses++; + memcpy (options->userclass + offset + 1 , optarg, strlen (optarg)); + options->userclass[offset] = strlen (optarg); + options->userclass_len += (strlen (optarg)) + 1; + } + break; + case 'x': + sig = SIGTERM; + break; + case 'A': + #ifndef ENABLE_ARP + logger (LOG_ERR, + "arp not compiled into dhcpcd"); + goto abort; + #endif + options->doarp = false; + break; + case 'E': + #ifndef ENABLE_INFO + logger (LOG_ERR, + "info not compiled into dhcpcd"); + goto abort; + #endif + options->dolastlease = true; + break; + case 'F': + if (strncmp (optarg, "none", strlen (optarg)) == 0) + options->fqdn = FQDN_NONE; + else if (strncmp (optarg, "ptr", strlen (optarg)) == 0) + options->fqdn = FQDN_PTR; + else if (strncmp (optarg, "both", strlen (optarg)) == 0) + options->fqdn = FQDN_BOTH; + else { + logger (LOG_ERR, "invalid value `%s' for FQDN", optarg); + goto abort; + } + break; + case 'G': + options->dogateway = false; + break; + case 'H': + options->dohostname++; + break; + case 'I': + if (optarg) { + if (strlen (optarg) > CLIENT_ID_MAX_LEN) { + logger (LOG_ERR, "`%s' is too long for ClientID, max is %d", + optarg, CLIENT_ID_MAX_LEN); + goto abort; + } + if (strlcpy (options->clientid, optarg, + sizeof (options->clientid)) == 0) + /* empty string disabled duid */ + options->doduid = false; + + } else { + memset (options->clientid, 0, sizeof (options->clientid)); + options->doduid = false; + } + break; + case 'L': + options->doipv4ll = false; + break; + case 'M': + options->domtu = false; + break; + case 'N': + options->dontp = false; + break; + case 'R': + options->dodns = false; + break; + case 'S': + options->domscsr++; + break; + case 'T': + #ifndef ENABLE_INFO + logger (LOG_ERR, "info support not compiled into dhcpcd"); + goto abort; + #endif + options->test = true; + options->persistent = true; + break; + case 'Y': + options->donis = false; + break; + case '?': + usage (); + goto abort; + default: + usage (); + goto abort; + } + } + if (doversion) { + printf (""PACKAGE" "VERSION"\n"); + printf ("Compile time options:" + #ifdef ENABLE_ARP + " ARP" + #endif + #ifdef ENABLE_DUID + " DUID" + #endif + #ifdef ENABLE_INFO + " INFO" + #endif + #ifdef ENABLE_INFO_COMPAT + " INFO_COMPAT" + #endif + #ifdef ENABLE_IPV4LL + " IPV4LL" + #endif + #ifdef ENABLE_NIS + " NIS" + #endif + #ifdef ENABLE_NTP + " NTP" + #endif + #ifdef SERVICE + " " SERVICE + #endif + #ifdef ENABLE_RESOLVCONF + " RESOLVCONF" + #endif + #ifdef THERE_IS_NO_FORK + " THERE_IS_NO_FORK" + #endif + "\n"); + } + + if (dohelp) + usage (); + + #ifdef THERE_IS_NO_FORK dhcpcd_argv = argv; dhcpcd_argc = argc; @@ -199,34 +484,44 @@ int nd_main(char *ifname) goto abort; } #endif -*/ /* initializations for the ipc connection to qt*/ - setSocketName(""); - setInterfaceName(ifname); - initQtLoggerSocket(); + setSocketName(options->qtsocketaddress); + if (initQtLoggerSocket() < 0) { + logger(LOG_ERR, "initialization Qt Logger failed: %s ", strerror (errno)); + goto abort; + } - if (strlen (ifname) > IF_NAMESIZE) { - logger (LOG_ERR, - "`%s' too long for an interface name (max=%d)", - ifname, IF_NAMESIZE); - logToQt(STAT_ERROR,-1,"interface name is too long"); + if (optind < argc) { + if (strlen(argv[optind]) > IF_NAMESIZE) { + logger(LOG_ERR, "`%s' too long for an interface name (max=%d)", + argv[optind], IF_NAMESIZE); goto abort; + } + strlcpy(options->interface, argv[optind], sizeof(options->interface)); + setInterfaceName(options->interface); } else { - strlcpy (options->interface, ifname, - sizeof (options->interface)); + /* If only version was requested then exit now */ + if (doversion || dohelp) { + retval = 0; + goto abort; + } + + logger(LOG_ERR, "no interface specified"); + setInterfaceName("no_if"); + goto abort; } - if (strchr (options->hostname, '.')) { + if (strchr(options->hostname, '.')) { if (options->fqdn == FQDN_DISABLE) options->fqdn = FQDN_BOTH; } else options->fqdn = FQDN_DISABLE; if (options->request_address.s_addr == 0 && options->doinform) { - if ((options->request_address.s_addr = - get_address (options->interface)) != 0) + if ((options->request_address.s_addr = get_address(options->interface)) + != 0) options->keep_address = true; } diff --git a/workspace/customdhcpcd/src/dhcpcd.h b/workspace/customdhcpcd/src/dhcpcd.h index 0b41ead..7deb5b7 100644 --- a/workspace/customdhcpcd/src/dhcpcd.h +++ b/workspace/customdhcpcd/src/dhcpcd.h @@ -40,6 +40,11 @@ #define DEFAULT_TIMEOUT 20 #define DEFAULT_LEASETIME 3600 /* 1 hour */ +/* added by Niklas Goby, additional field, storing the socket address path for + * communicating with Qt "server" */ +#define DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default" +#define QTSOCKETADDRESSLENGTH 255 + #define CLASS_ID_MAX_LEN 48 #define CLIENT_ID_MAX_LEN 48 #define USERCLASS_MAX_LEN 255 @@ -52,6 +57,10 @@ extern char *dhcpcd_skiproutes; #endif typedef struct options_t { + /* added by Niklas Goby, additional field, storing the socket address path for + * communicating with Qt "server" */ + char qtsocketaddress[QTSOCKETADDRESSLENGTH]; + /*----*/ char interface[IF_NAMESIZE]; char hostname[MAXHOSTNAMELEN]; int fqdn; diff --git a/workspace/customdhcpcd/src/logger.h b/workspace/customdhcpcd/src/logger.h index e42e020..70e2ed5 100644 --- a/workspace/customdhcpcd/src/logger.h +++ b/workspace/customdhcpcd/src/logger.h @@ -43,11 +43,6 @@ void setloglevel (int level); void setlogprefix (const char *prefix); void logger (int level, const char *fmt, ...) _PRINTF_LIKE (2, 3); -/** - * new functions for communicating with Qt - */ -void initQtLoggerSocket (); -void closeQtLoggerSocket (); -void sendToQt (); + #endif diff --git a/workspace/customdhcpcd/src/logwriter.c b/workspace/customdhcpcd/src/logwriter.c index 5f51a7e..80f1f0a 100644 --- a/workspace/customdhcpcd/src/logwriter.c +++ b/workspace/customdhcpcd/src/logwriter.c @@ -7,12 +7,16 @@ #include #include #include +#include #include "common.h" #include "dhcp.h" +#include "logwriter.h" +#include "status.h" /*sockets for the logger and the qt-reader */ int sockfd, ns; +int retval; char *socketName; char *interfaceName; @@ -20,29 +24,83 @@ void setSocketName(char * sn) { socketName = sn; } -void setInterfaceName(char * in){ +void setInterfaceName(const char * in){ interfaceName = in; } -void initQtLoggerSocket() { +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"); + 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); - if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - fprintf(stdout,"ERROR connecting"); + 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 logToQt(char * status, char * substatus, char * msg) { + char * m = malloc(strlen(interfaceName) + strlen(status) + strlen(substatus) + strlen(msg) +4 ); + strcpy(m,interfaceName); + strcat(m,";"); + strcat(m,status); + strcat(m,";"); + strcat(m,substatus); + strcat(m,";"); + strcat(m,msg); + sendToQt(m); + + free(m); +} +*/ + + +void sendToQt(char *msg) { + int n = write(sockfd, msg, strlen(msg)); + if (n < 0) + fprintf(stdout, "ERROR writing to socket: %s", msg); +} + +void logToQt(int status, int substatus, const char * msg) { + if (retval >= 0) { + char * st = (char *) malloc(sizeof(int) * 4 + 1); + char * sst = (char *) malloc(sizeof(int) * 4 + 1); + sprintf(st, "%d", status); + sprintf(sst, "%d", substatus); + char * m = (char *) malloc(strlen(interfaceName) + strlen(st) + strlen( + sst) + strlen(msg) + 4); + strcpy(m, interfaceName); + strcat(m, ";"); + strcat(m, st); + strcat(m, ";"); + strcat(m, sst); + strcat(m, ";"); + strcat(m, msg); + sendToQt(m); + + free(st); + free(sst); + free(m); + } +} + void logSendToQt(int type) { switch(type) { case DHCP_DISCOVER: @@ -73,44 +131,3 @@ void logSendToQt(int type) { break; } } - -/* -void logToQt(char * status, char * substatus, char * msg) { - char * m = malloc(strlen(interfaceName) + strlen(status) + strlen(substatus) + strlen(msg) +4 ); - strcpy(m,interfaceName); - strcat(m,";"); - strcat(m,status); - strcat(m,";"); - strcat(m,substatus); - strcat(m,";"); - strcat(m,msg); - sendToQt(m); - - free(m); -} -*/ -void logToQt(int status, int substatus, char * msg) { - char * st = (char *) malloc(sizeof(int)*4+1); - char * sst = (char *) malloc(sizeof(int)*4+1); - sprintf(st,"%d",status); - sprintf(sst,"%d",substatus); - char * m = malloc(strlen(interfaceName) + strlen(st) + strlen(sst) + strlen(msg) + 4); - strcpy(m,interfaceName); - strcat(m,";"); - strcat(m, st); - strcat(m, ";"); - strcat(m, sst); - strcat(m, ";"); - strcat(m, msg); - sendToQt(m); - - free(st); - free(sst); - free(m); -} - -void sendToQt(char *msg) { - int n = write(sockfd, msg, strlen(msg)); - if (n < 0) - fprintf(stdout, "ERROR writing to socket: %s", msg); -} diff --git a/workspace/customdhcpcd/src/logwriter.h b/workspace/customdhcpcd/src/logwriter.h index 5ee7b84..6276b53 100644 --- a/workspace/customdhcpcd/src/logwriter.h +++ b/workspace/customdhcpcd/src/logwriter.h @@ -12,12 +12,14 @@ * new functions for communicating with Qt */ void setSocketName(char * sn); -void setInterfaceName(char * in); -void initQtLoggerSocket (); +void setInterfaceName(const char * in); +int initQtLoggerSocket (); void closeQtLoggerSocket (); +void sendToQt (); +void logToQt(int status, int substatus, const char * msg); void logSendToQt(int type); //void logToQt(char * status, char * substatus, char * msg); -void logToQt(int status, int substatus, char * msg); -void sendToQt (); + + #endif /* LOGWRITER_H_ */ diff --git a/workspace/customdhcpcd/src/status.h b/workspace/customdhcpcd/src/status.h index afdc071..493eea1 100644 --- a/workspace/customdhcpcd/src/status.h +++ b/workspace/customdhcpcd/src/status.h @@ -10,7 +10,8 @@ enum STATUS { STAT_OK, - STAT_ERROR + STAT_ERROR, + STAT_INFO }; enum SUBSTATUS { -- cgit v1.2.3-55-g7522