From c9433d7303c6cec8031d35a6fbb3ff97dbd22078 Mon Sep 17 00:00:00 2001 From: Niklas Date: Wed, 29 Jun 2011 16:57:27 +0200 Subject: new funtions for the networkDiscovery class. extensions to the logger.c some tryouts with the QLocalSocket also try QSocketNotifyer --- workspace/networkDiscovery/dhcpcd/dhcp.c | 1 + workspace/networkDiscovery/dhcpcd/dhcp.h | 1 - workspace/networkDiscovery/dhcpcd/dhcpcd.8.in | 365 ------------------------ workspace/networkDiscovery/dhcpcd/dhcpcd.c | 221 ++++++++++++++ workspace/networkDiscovery/dhcpcd/dhcpcd.h | 2 + workspace/networkDiscovery/dhcpcd/logger.c | 52 ++++ workspace/networkDiscovery/dhcpcd/logger.h | 9 + workspace/networkDiscovery/dhcpcd/mk/cc.mk | 23 -- workspace/networkDiscovery/dhcpcd/mk/depend.mk | 11 - workspace/networkDiscovery/dhcpcd/mk/dist.mk | 11 - workspace/networkDiscovery/dhcpcd/mk/man.mk | 14 - workspace/networkDiscovery/dhcpcd/mk/os.mk | 77 ----- workspace/networkDiscovery/dhcpcd/mk/prog.mk | 34 --- workspace/networkDiscovery/networkDiscovery.pro | 8 +- workspace/networkDiscovery/networkdiscovery.cpp | 304 ++++---------------- workspace/networkDiscovery/networkdiscovery.h | 10 +- 16 files changed, 355 insertions(+), 788 deletions(-) delete mode 100644 workspace/networkDiscovery/dhcpcd/dhcpcd.8.in delete mode 100644 workspace/networkDiscovery/dhcpcd/mk/cc.mk delete mode 100644 workspace/networkDiscovery/dhcpcd/mk/depend.mk delete mode 100644 workspace/networkDiscovery/dhcpcd/mk/dist.mk delete mode 100644 workspace/networkDiscovery/dhcpcd/mk/man.mk delete mode 100644 workspace/networkDiscovery/dhcpcd/mk/os.mk delete mode 100644 workspace/networkDiscovery/dhcpcd/mk/prog.mk diff --git a/workspace/networkDiscovery/dhcpcd/dhcp.c b/workspace/networkDiscovery/dhcpcd/dhcp.c index 8ed66da..f625e8f 100644 --- a/workspace/networkDiscovery/dhcpcd/dhcp.c +++ b/workspace/networkDiscovery/dhcpcd/dhcp.c @@ -476,6 +476,7 @@ void free_dhcp (dhcp_t *dhcp) } } + static bool dhcp_add_address (struct address_head **addresses, const unsigned char *data, int length) diff --git a/workspace/networkDiscovery/dhcpcd/dhcp.h b/workspace/networkDiscovery/dhcpcd/dhcp.h index cc66d13..ef97b75 100644 --- a/workspace/networkDiscovery/dhcpcd/dhcp.h +++ b/workspace/networkDiscovery/dhcpcd/dhcp.h @@ -212,5 +212,4 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, uint32_t xid, char type, const options_t *options); void free_dhcp (dhcp_t *dhcp); int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message); - #endif diff --git a/workspace/networkDiscovery/dhcpcd/dhcpcd.8.in b/workspace/networkDiscovery/dhcpcd/dhcpcd.8.in deleted file mode 100644 index ddeff9b..0000000 --- a/workspace/networkDiscovery/dhcpcd/dhcpcd.8.in +++ /dev/null @@ -1,365 +0,0 @@ -.\" Copyright 2006-2008 Roy Marples -.\" All rights reserved -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.Dd Feb 20, 2008 -.Dt DHCPCD 8 SMM -.Sh NAME -.Nm dhcpcd -.Nd an RFC 2131 compliant DHCP client -.Sh SYNOPSIS -.Nm -.Op Fl dknpAEGHMLNRSTY -.Op Fl c , -script Ar script -.Op Fl h , -hostname Ar hostname -.Op Fl i , -classid Ar classid -.Op Fl l , -leasetime Ar seconds -.Op Fl m , -metric Ar metric -.Op Fl r , -request Ar address -.Op Fl t , -timeout Ar seconds -.Op Fl u , -userclass Ar class -.Op Fl F , -fqdn Ar FQDN -.Op Fl I , -clientid Ar clientid -.Ar interface -.Nm -.Fl k , -release -.Ar interface -.Nm -.Fl x , -exit -.Ar interface -.Sh DESCRIPTION -.Nm -is an implementation of the DHCP client specified in -.Rs -.%T "RFC 2131" -.Re -.Nm -gets the host information -.Po -IP address, routes, etc -.Pc -from a DHCP server and configures the network -.Ar interface -of the -machine on which it is running. -.Nm -will then write DNS information to -.Xr resolvconf 8 , -if available, otherwise directly to -.Pa /etc/resolv.conf . -.Nm -will also configure -.Pa /etc/yp.conf -and -.Pa /etc/ntpd.conf -with NIS and NTP information if the DHCP server provided them. -If those file contents changed, then -.Nm -will also attempt to restart the respective services to notify them of the -change. -If the hostname is currenly blank, (null) or localhost then -.Nm -will set the hostname to the one supplied by the DHCP server, or look it up -in DNS if none supplied. -.Nm -then daemonises and waits for the lease renewal time to lapse. -Then it attempts to renew its lease and reconfigure if the new lease changes. -.Ss Local Link configuration -If -.Nm -failed to obtain a lease, it will probe for a valid IPv4LL address -.Po -aka Zeroconf, aka APIPA -.Pc . -Once obtained it will probe every 10 seconds for a DHCP server to get a -proper address. -.Pp -Even when -.Nm -obtains a proper lease, it will still add a Local Link route -.Po -165.254.0.0/16 -.Pc -so that the host can communicate with clients using these addresses. -.Pp -When using IPv4LL, -.Nm -will always succeed and return a 0 exit code. To disable this behaviour, you -can use the -.Fl L , -noipv4ll -option. -.Ss Hooking into DHCP events -.Nm -will run @PREFIX@/etc/dhcpcd.sh, or the script specified by the -.Fl c , -script -option. It will set $1 to a shell compatible file that holds various -configuration settings obtained from the DHCP server and $2 to either -up, down or new depending on the state of -.Nm . -.Nm -ignores the exist code of the script. -.Ss Fine tuning -You can fine tune the behaviour of -.Nm -with the following options :- -.Bl -tag -width indent -.It Fl d , -debug -Echo debug and informational messages to the console. -Subsequent debug options stop -.Nm -from daemonising. -.It Fl h , -hostname Ar hostname -By default, -.Nm -will send the current hostname to the DHCP server so it can register in DNS. -You can use this option to specify the -.Ar hostname -sent, or an empty string to -stop any -.Ar hostname -from being sent. -.It Fl i , -classid Ar classid -Override the DHCP vendor -.Ar classid -field we send. The default is -dhcpcd-. -.It Fl k , -release -This causes an existing -.Nm -process running on the -.Ar interface -to release it's lease, deconfigure the -.Ar interface -and then exit. -.It Fl l , -leasetime Ar seconds -Request a specific lease time in -.Ar seconds . -By default -.Nm -does not request any lease time and leaves the it in the hands of the -DHCP server. -.It Fl m , -metric Ar metric -Added routes will use the -.Ar metric -on systems where this is supported -.Po -presently only Linux -.Pc . -Route metrics allow the addition of routes to the same destination across -different interfaces, the lower the metric the more it is preferred. -.It Fl n , -renew -Notifies an existing -.Nm -process running on the -.Ar interface -to renew it's lease. If -.Nm -is not running, then it starts up as normal. -.It Fl p , -persistent -.Nm -normally deconfigures the -.Ar interface -and configuration when it exits. -Sometimes, this isn't desirable if for example you have root mounted over NFS. -You can use this option to stop this from happening. -.It Fl r , -request Op Ar address -.Nm -normally sends a DHCP Broadcast to find servers to offer an address. -.Nm -will then request the address used. You can use this option to skip the -broadcast step and just request an -.Ar address . -The downside is if you request -an -.Ar address -the DHCP server does not know about or the DHCP server is not -authorative, it will remain silent. In this situation, we go back to the init -state and broadcast again. -If no -.Ar address -is given then we use the first address currently assigned to the -.Ar interface . -.It Fl s , -inform Op Ar address Op / Ar cidr -Behaves exactly like -.Fl r , -request -as above, but sends a DHCP inform instead of a request. This requires the -interface to be configured first. This does not get a lease as such, just -notifies the DHCP server of the -.Ar address -we are using. -.It Fl t , -timeout Ar seconds -Timeout after -.Ar seconds , -instead of the default 20. -A setting of 0 -.Ar seconds -causes -.Nm -to wait forever to get a lease. -.It Fl u , -userclass Ar class -Tags the DHCP message with the userclass -.Ar class . -DHCP servers use this give memebers of the class DHCP options other than the -default, without having to know things like hardware address or hostname. -.If Fl F , -fqdn Ar fqdn -Requests that the DHCP server updates DNS using FQDN instead of just a -hostname. Valid values for -.Ar fqdn -are none, ptr and both. -.Nm -dhcpcd itself never does any DNS updates. -.It Fl H , --sethostname -Forces -.Nm -to set the hostname as supplied by the DHCP server. Because some OS's and users -prefer to have just the hostname, or the full FQDN more -.Fl H , --sethostname -options change the behaviour. Below is the list of possible combinations:- -.Bl -tag -width indent -.It Fl H -set the hostname to the full FQDN. -.It Fl HH -strip the domain if it matches the dns domain. -.It Fl HHH -strip the domain regardless. -.It Fl HHHH -same as -.Fl H -but force hostname lookup via DNS. -.It Fl HHHHH -same as above, but strip the domain if it matches the dns domain. -.It Fl HHHHHH -same as above, but strip the domain regardless. -.El -.It Fl I , -clientid Ar clientid -Send -.Ar clientid -as a client identifier string. If -.Ar clientid -matches a hardware address format, such as 01:00:01:02:03:04:05 then we encode -it as that, otherwise as a string. You need to specify the hardware type in -the first byte. Ethernet is 01, and the hardware address in the example is -00:01:02:03:04:05. If the -.Ar clientid -is a blank string, then we disable DUID support and use a -.Ar clientid -as shown above. -.It Fl S, -mscsr -Microsoft have their own code for Classless Static Routes -.Po -RFC 3442 -.Pc . -You can use this option to request this as well as the normal CSR. Another -instace of this option only requests the Microsoft CSR to prevent DHCP message -over-running its maximum size. DHCP server administrators should update their -CSR code from the Microsoft specific one to the RFC compliant one as the -content is fully compatible. -.El -.Ss Restriciting behaviour -.Nm -will try to do as much as it can by default. However, there are sometimes -situations where you don't want the things to be configured exactly how the -the DHCP server wants. Here are some option that deal with turning these bits -off. -.Bl -tag -width indent -.It Fl A , -noarp -Don't request or claim the address by ARP. -.It Fl G , -nogateway -Don't set any default routes. -.It Fl L , -noipv4ll -Don't use IPv4LL at all. -.It Fl M , -nomtu -Don't set the MTU of the -.Ar interface . -.It Fl N , -nontp -Don't touch -.Pa /etc/ntpd.conf -or restart the ntp service. -.It Fl R , -nodns -Don't send DNS information to resolvconf or touch -.Pa /etc/resolv.conf . -.It Fl T , -test -On receipt of discover messages, simply print the contents of the DHCP -message to the console. -.Nm -will not configure the -.Ar interface , -touch any files or restart any services. -.It Fl Y , -nonis -Don't touch -.Pa /etc/yp.conf -or restart the ypbind service. -.El -.Sh NOTES -Because -.Nm -supports InfiniBand, we put a Node-specific Client Identifier in the -ClientID field. This is required by RFC 4390. It's also required for DHCP IPv6 -which -.Nm -should support one day. However, some DHCP servers have no idea what this is -and reject the message as they do not understand type 255. This is not -conformant with RFC 2132 and the server should be fixed. Also, some DHCP -server configurations require an ethernet hardware address of 6 hexacdecimal -numbers in the ClientID which is the default behaviour of most other DHCP -clients. If your DHCP server is as desribed above, you should fix the server, -or if that is not an option you can compile DUID support out of -.Nm -or use the -.Fl I , -clientid Ar clientid -option and set -.Ar clientid -to ''. -.Pp -ISC dhcpd, dnsmasq, udhcpd and Microsoft DHCP server 2003 default configurations -work just fine with the default -.Nm -configuration. -.Pp -.Nm -requires a Berkley Packet Filter, or BPF device on BSD based systems and a -Linux Socket Filter, or LPF device on Linux based systems. -.Sh FILES -.Bl -ohang -.It Pa @PREFIX@/etc/dhcpcd.sh -Bourne shell script that is run when we configure or deconfigure an interface. -.It Pa @INFODIR@/dhcpcd.duid -Text file that holds the DUID used to identify the host. -.It Pa @INFODIR@/dhcpcd- Ns Ar interface Ns .info -Bourne shell file that holds the DHCP values used in configuring the interface. -This path is passed as the first argument to -.Pa @PREFIX@/etc/dhcpcd.sh . -.El -.Sh SEE ALSO -.Xr ntp 1 , -.Xr resolv.conf 5 , -.Xr resolvconf 8 , -.Xr yp.conf 5 , -.Xr ypbind 8 -.Sh STANDARDS -RFC 2131, RFC 2132, RFC 2855, RFC 3004, RFC 3361, RFC 3397, RFC 3442, RFC 3927, -RFC 4361, RFC 4390, RFC 4702. -.Sh AUTHORS -.An "Roy Marples" Aq roy@marples.name -.Sh BUGS -Please report them to http://bugs.marples.name diff --git a/workspace/networkDiscovery/dhcpcd/dhcpcd.c b/workspace/networkDiscovery/dhcpcd/dhcpcd.c index 9af930a..925717a 100644 --- a/workspace/networkDiscovery/dhcpcd/dhcpcd.c +++ b/workspace/networkDiscovery/dhcpcd/dhcpcd.c @@ -130,6 +130,7 @@ static pid_t read_pid (const char *pidfile) fscanf (fp, "%d", &pid); fclose (fp); + return (pid); } @@ -143,3 +144,223 @@ static void usage (void) /** * to be deleted */ + +int nd_main(char *ifname) +{ + options_t *options; + int userclasses = 0; + int opt; + int option_index = 0; + char *prefix; + pid_t pid; + int debug = 0; + int i; + int pidfd = -1; + int sig = 0; + int retval = EXIT_FAILURE; + + /* Close any un-needed fd's */ + for (i = getdtablesize() - 1; i >= 3; --i) + close (i); + + openlog (PACKAGE, LOG_PID, LOG_LOCAL0); + + options = xzalloc (sizeof (*options)); + options->script = (char *) DEFAULT_SCRIPT; + snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", + PACKAGE, VERSION); + + options->doarp = true; + options->dodns = true; + options->domtu = true; + options->donis = true; + options->dontp = true; + options->dogateway = true; + options->daemonise = true; + options->doinform = false; + options->doipv4ll = true; + options->doduid = true; + options->timeout = DEFAULT_TIMEOUT; + + gethostname (options->hostname, sizeof (options->hostname)); + if (strcmp (options->hostname, "(none)") == 0 || + strcmp (options->hostname, "localhost") == 0) + memset (options->hostname, 0, sizeof (options->hostname)); + + +/* +#ifdef THERE_IS_NO_FORK + dhcpcd_argv = argv; + dhcpcd_argc = argc; + if (! realpath (argv[0], dhcpcd)) { + logger (LOG_ERR, "unable to resolve the path `%s': %s", + argv[0], strerror (errno)); + goto abort; + } +#endif +*/ + + if (strlen (ifname) > IF_NAMESIZE) { + logger (LOG_ERR, + "`%s' too long for an interface name (max=%d)", + ifname, IF_NAMESIZE); + goto abort; + } else { + strlcpy (options->interface, ifname, + sizeof (options->interface)); + } + + 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) + options->keep_address = true; + } + + if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) { + logger (LOG_ERR, + "you are not allowed to request a link local address"); + goto abort; + } + + if (geteuid ()) + logger (LOG_WARNING, PACKAGE " will not work correctly unless" + " run as root"); + + prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3)); + snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface); + setlogprefix (prefix); + snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE, + options->interface); + free (prefix); + + chdir ("/"); + umask (022); + + if (mkdir (INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP + | S_IROTH | S_IXOTH) && errno != EEXIST) + { + logger (LOG_ERR, + "mkdir(\"%s\",0): %s\n", INFODIR, strerror (errno)); + goto abort; + } + + if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP + | S_IROTH | S_IXOTH) && errno != EEXIST) + { + logger (LOG_ERR, + "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno)); + goto abort; + } + + if (options->test) { + if (options->dorequest || options->doinform) { + logger (LOG_ERR, + "cannot test with --inform or --request"); + goto abort; + } + + if (options->dolastlease) { + logger (LOG_ERR, "cannot test with --lastlease"); + goto abort; + } + + if (sig != 0) { + logger (LOG_ERR, + "cannot test with --release or --renew"); + goto abort; + } + } + + if (sig != 0) { + int killed = -1; + pid = read_pid (options->pidfile); + if (pid != 0) + logger (LOG_INFO, "sending signal %d to pid %d", + sig, pid); + + if (! pid || (killed = kill (pid, sig))) + logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, + ""PACKAGE" not running"); + + if (pid != 0 && (sig != SIGALRM || killed != 0)) + unlink (options->pidfile); + + if (killed == 0) { + retval = EXIT_SUCCESS; + goto abort; + } + + if (sig != SIGALRM) + goto abort; + } + + if (! options->test && ! options->daemonised) { + if ((pid = read_pid (options->pidfile)) > 0 && + kill (pid, 0) == 0) + { + logger (LOG_ERR, ""PACKAGE + " already running on pid %d (%s)", + pid, options->pidfile); + goto abort; + } + + pidfd = open (options->pidfile, + O_WRONLY | O_CREAT | O_NONBLOCK, 0664); + if (pidfd == -1) { + logger (LOG_ERR, "open `%s': %s", + options->pidfile, strerror (errno)); + goto abort; + } + + /* Lock the file so that only one instance of dhcpcd runs + * on an interface */ + if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) { + logger (LOG_ERR, "flock `%s': %s", + options->pidfile, strerror (errno)); + goto abort; + } + + /* dhcpcd.sh should not interhit this fd */ + if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 || + fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1) + logger (LOG_ERR, "fcntl: %s", strerror (errno)); + + writepid (pidfd, getpid ()); + logger (LOG_INFO, PACKAGE " " VERSION " starting"); + } + + /* Seed random */ + srandomdev (); + + /* Massage our filters per platform */ + setup_packet_filters (); + + if (dhcp_run (options, &pidfd) == 0) + retval = EXIT_SUCCESS; + +abort: + /* If we didn't daemonise then we need to punt the pidfile now */ + if (pidfd > -1) { + close (pidfd); + unlink (options->pidfile); + } + + free (options); + +#ifdef THERE_IS_NO_FORK + /* There may have been an error before the dhcp_run function + * clears this, so just do it here to be safe */ + free (dhcpcd_skiproutes); +#endif + + logger (LOG_INFO, "exiting"); + + exit (retval); + /* NOTREACHED */ +} diff --git a/workspace/networkDiscovery/dhcpcd/dhcpcd.h b/workspace/networkDiscovery/dhcpcd/dhcpcd.h index f57093f..0b41ead 100644 --- a/workspace/networkDiscovery/dhcpcd/dhcpcd.h +++ b/workspace/networkDiscovery/dhcpcd/dhcpcd.h @@ -91,4 +91,6 @@ typedef struct options_t { char pidfile[PATH_MAX]; } options_t; +int nd_main (char *ifname); + #endif diff --git a/workspace/networkDiscovery/dhcpcd/logger.c b/workspace/networkDiscovery/dhcpcd/logger.c index 99f845f..a68fe65 100644 --- a/workspace/networkDiscovery/dhcpcd/logger.c +++ b/workspace/networkDiscovery/dhcpcd/logger.c @@ -27,6 +27,11 @@ #define SYSLOG_NAMES +#define COM_CH "./com_ch" + +#include +#include +#include #include #include #include @@ -34,9 +39,13 @@ #include #include + #include "common.h" #include "logger.h" +/*sockets for the logger and the qt-reader */ +int s, ns; + static int loglevel = LOG_WARNING; static char logprefix[12] = {0}; @@ -117,3 +126,46 @@ void logger (int level, const char *fmt, ...) va_end (p); } +void initQtLoggerSocket () +{ + /*variables for the communication over sockets with Qt*/ + int fromlen; + struct sockaddr_un soaddr, faddr; + int slen; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + perror("server: socket"); + exit(1); + } + + soaddr.sun_family = AF_UNIX; + strcpy(soaddr.sun_path, COM_CH); + unlink(COM_CH); + slen = strlen(soaddr.sun_path) + sizeof(soaddr.sun_family); + if (bind(s, (struct sockaddr *) &soaddr, slen) < 0) { + perror("server: bind"); + exit(1); + } + + if (listen(s, 5) < 0) { + perror("server: listen"); + exit(1); + } + + if ((ns = accept(s, &faddr, &fromlen)) < 0) { + perror("server: accept"); + exit(1); + } +} + +void closeQtLoggerSocket (){ + close(s); +} + +void sendToQt (char *msg) { + if(send(ns,msg, strlen(msg),0) < 0) + { + fprintf(stdout,"logger: failed to send message: %s", msg); + } +} + diff --git a/workspace/networkDiscovery/dhcpcd/logger.h b/workspace/networkDiscovery/dhcpcd/logger.h index 989f68d..e42e020 100644 --- a/workspace/networkDiscovery/dhcpcd/logger.h +++ b/workspace/networkDiscovery/dhcpcd/logger.h @@ -36,9 +36,18 @@ #include + + int logtolevel (const char *priority); 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/networkDiscovery/dhcpcd/mk/cc.mk b/workspace/networkDiscovery/dhcpcd/mk/cc.mk deleted file mode 100644 index d52597b..0000000 --- a/workspace/networkDiscovery/dhcpcd/mk/cc.mk +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2008 Roy Marples - -# Setup some good default CFLAGS -CFLAGS?= -O2 - -# Default to using the C99 standard -CSTD?= c99 -_CSTD_SH= if test -n "${CSTD}"; then echo "-std=${CSTD}"; else echo ""; fi -_CSTD!= ${_CSTD_SH} -CFLAGS+= ${_CSTD}$(shell ${_CSTD_SH}) - -# Try and use some good cc flags -_CC_FLAGS= -pedantic -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \ - -Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \ - -Wbad-function-cast -Wnested-externs -Wcomment -Winline \ - -Wchar-subscripts -Wcast-align -Wno-format-nonliteral \ - -Wdeclaration-after-statement -Wsequence-point -Wextra -_CC_FLAGS_SH= for f in ${_CC_FLAGS}; do \ - if ${CC} $$f -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \ - then printf "%s" "$$f "; fi \ - done -_CC_FLAGS!= ${_CC_FLAGS_SH} -CFLAGS+= ${_CC_FLAGS}$(shell ${CC_FLAGS_SH}) diff --git a/workspace/networkDiscovery/dhcpcd/mk/depend.mk b/workspace/networkDiscovery/dhcpcd/mk/depend.mk deleted file mode 100644 index a4d717a..0000000 --- a/workspace/networkDiscovery/dhcpcd/mk/depend.mk +++ /dev/null @@ -1,11 +0,0 @@ -# This only works for make implementations that always include a .depend if -# it exists. Only GNU make does not do this. - -# Copyright 2008 Roy Marples - -CLEANFILES+= .depend - -.depend: ${SRCS} - ${CC} ${CFLAGS} -MM ${SRCS} > .depend - -depend: .depend diff --git a/workspace/networkDiscovery/dhcpcd/mk/dist.mk b/workspace/networkDiscovery/dhcpcd/mk/dist.mk deleted file mode 100644 index 1d3669d..0000000 --- a/workspace/networkDiscovery/dhcpcd/mk/dist.mk +++ /dev/null @@ -1,11 +0,0 @@ -# rules to make a distribution tarball from a git repo -# Copyright 2008 Roy Marples - -GITREF?= HEAD -DISTPREFIX?= ${PROG}-${VERSION} -DISTFILE?= ${DISTPREFIX}.tar.bz2 - -CLEANFILES+= ${DISTFILE} - -dist: - git archive --prefix=${DISTPREFIX}/ ${GITREF} | bzip2 > ${DISTFILE} diff --git a/workspace/networkDiscovery/dhcpcd/mk/man.mk b/workspace/networkDiscovery/dhcpcd/mk/man.mk deleted file mode 100644 index 5d9bf26..0000000 --- a/workspace/networkDiscovery/dhcpcd/mk/man.mk +++ /dev/null @@ -1,14 +0,0 @@ -# rules to install manpages -# Copyright 2008 Roy Marples - -MANPREFIX?= /usr/share -MANDIR?= ${MANPREFIX}/man/man -MANMODE?= 0444 -MINSTALL?= ${INSTALL} -m ${MANMODE} - -man: ${MAN} - -# We cheat as all our pages go into section 8 -maninstall: man - ${INSTALL} -d ${DESTDIR}${MANDIR}8 - for man in ${MAN}; do ${MINSTALL} $$man ${DESTDIR}${MANDIR}8; done diff --git a/workspace/networkDiscovery/dhcpcd/mk/os.mk b/workspace/networkDiscovery/dhcpcd/mk/os.mk deleted file mode 100644 index af173fc..0000000 --- a/workspace/networkDiscovery/dhcpcd/mk/os.mk +++ /dev/null @@ -1,77 +0,0 @@ -# Setup OS specific variables -# Copyright 2008 Roy Marples - -# Work out if we need -lresolv or not -_LIBRESOLV_SH= printf '\#include \n\#include \nint main (void) { return (res_init ()); }\n' > .res_init.c; \ - if ${CC} .res_init.c -o .res_init >/dev/null 2>&1; then \ - echo ""; \ - elif ${CC} .res_init.c -lresolv -o .res_init >/dev/null 2>&1; then \ - echo "-lresolv"; \ - else \ - echo "Cannot work out how to get res_init to link" >&2; \ - rm -f .res_init.c .res_init; \ - exit 1; \ - fi; \ - rm -f .res_init.c .res_init -_LIBRESOLV!= ${_LIBRESOLV_SH} -LIBRESOLV= ${_LIBRESOLV}$(shell ${_LIBRESOLV_SH}) - -# Work out if we need -lrt or not -_LIBRT_SH= printf '\#include \n\#include \n\nint main (void) { struct timespec ts;\n\#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)\nreturn (clock_gettime (CLOCK_MONOTONIC, &ts));\n\#else\nreturn -1;\n\#endif\n}\n' > .clock_gettime.c; \ - if ${CC} .clock_gettime.c -o .clock_gettime >/dev/null 2>&1; then \ - echo ""; \ - elif ${CC} .clock_gettime.c -lrt -o .clock_gettime >/dev/null 2>&1; then \ - echo "-lrt"; \ - else \ - echo ""; \ - fi; \ - rm -f .clock_gettime.c .clock_gettime -_LIBRT!= ${_LIBRT_SH} -LIBRT= ${_LIBRT}$(shell ${_LIBRT_SH}) - -# Work out if our fork() works or not -_HAVE_FORK_SH= if test "${HAVE_FORK}" = "yes"; then \ - echo ""; \ - elif test -n "${HAVE_FORK}"; then \ - echo "-DTHERE_IS_NO_FORK"; \ - else \ - printf '\#include \n\#include \nint main (void) { pid_t pid = fork(); if (pid == -1) exit (-1); exit (0); }\n' > .fork.c; \ - ${CC} .fork.c -o .fork >/dev/null 2>&1; \ - if ./.fork; then \ - echo ""; \ - else \ - echo "-DTHERE_IS_NO_FORK"; \ - fi; \ - rm -f .fork.c .fork; \ - fi; -_HAVE_FORK!= ${_HAVE_FORK_SH} -FORK= ${_HAVE_FORK}$(shell ${_HAVE_FORK_SH}) - -# info dir defaults to /var/lib/dhcpcd on Linux and /var/db elsewhere -_INFODIR_SH= if test -n "${INFODIR}"; then \ - echo "${INFODIR}"; \ - else \ - case `uname -s` in \ - Linux) echo "/var/lib/dhcpcd";; \ - *) echo "/var/db";; \ - esac \ - fi -_INFODIR!= ${_INFODIR_SH} -INFOD?= ${_INFODIR}$(shell ${_INFODIR_SH}) - -# Work out how to restart services -_RC_SH= if test -n "${HAVE_INIT}"; then \ - test "${HAVE_INIT}" = "no" || echo "-DENABLE_${HAVE_INIT}"; \ - elif test -x /sbin/runscript; then echo "-DENABLE_OPENRC"; \ - elif test -x /sbin/service; then echo "-DENABLE_SERVICE"; \ - elif test -x /etc/rc.d/rc.S -a -x /etc/rc.d/rc.M; then echo "-DENABLE_SLACKRC"; \ - elif test -d /etc/rc.d; then echo "-DENABLE_BSDRC"; \ - elif test -d /etc/init.d; then echo "-DENABLE_SYSV"; \ - fi -_RC!= ${_RC_SH} -RC= ${_RC}$(shell ${_RC_SH}) - -# glibc requires _BSD_SOURCE and _XOPEN_SOURCE -_DEF_SH= case `uname -s` in Linux) echo "-D_BSD_SOURCE -D_XOPEN_SOURCE=600";; *) echo;; esac -_DEF!= ${_DEF_SH} -CFLAGS+= ${_DEF}$(shell ${_DEF_SH}) diff --git a/workspace/networkDiscovery/dhcpcd/mk/prog.mk b/workspace/networkDiscovery/dhcpcd/mk/prog.mk deleted file mode 100644 index 6f2560c..0000000 --- a/workspace/networkDiscovery/dhcpcd/mk/prog.mk +++ /dev/null @@ -1,34 +0,0 @@ -# rules to build a program -# based on FreeBSD's bsd.prog.mk - -# Copyright 2008 Roy Marples - -BINDIR?= ${PREFIX}/usr/bin -BINMODE?= 0755 -OBJS+= ${SRCS:.c=.o} - -INSTALL?= install - -all: ${PROG} ${MAN} - -${PROG}: ${SCRIPTS} ${OBJS} - ${CC} ${LDFLAGS} -o $@ ${OBJS} ${LDADD} - -_proginstall: ${PROG} - ${INSTALL} -d ${DESTDIR}${BINDIR} - ${INSTALL} -m ${BINMODE} ${PROG} ${DESTDIR}${BINDIR} - -include ${MK}/depend.mk -include ${MK}/man.mk -include ${MK}/dist.mk - -install: _proginstall maninstall - -clean: - rm -f ${OBJS} ${PROG} ${CLEANFILES} - -LINTFLAGS?= -hx -LINTFLAGS+= -X 159,247,352 - -lint: ${SRCS:.c=.c} - ${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} $^ ${.ALLSRC} diff --git a/workspace/networkDiscovery/networkDiscovery.pro b/workspace/networkDiscovery/networkDiscovery.pro index d6ef25c..7095e70 100644 --- a/workspace/networkDiscovery/networkDiscovery.pro +++ b/workspace/networkDiscovery/networkDiscovery.pro @@ -3,8 +3,9 @@ TARGET = networkDiscovery QT += core \ gui \ network -LIBS += -L/usr/lib -INCLUDEPATH += /usr/include/ + +# LIBS += -L/usr/lib +# INCLUDEPATH += /usr/include/ HEADERS += dhcpcd/arp.h \ dhcpcd/client.h \ dhcpcd/common.h \ @@ -36,5 +37,6 @@ SOURCES += dhcpcd/arp.c \ dhcpcd/socket.c \ main.cpp \ networkdiscovery.cpp -FORMS += networkdiscovery.ui +FORMS += networkdiscovery.ui \ + networkdiscovery.ui RESOURCES += diff --git a/workspace/networkDiscovery/networkdiscovery.cpp b/workspace/networkDiscovery/networkdiscovery.cpp index f2cb2bb..cf4f354 100644 --- a/workspace/networkDiscovery/networkdiscovery.cpp +++ b/workspace/networkDiscovery/networkdiscovery.cpp @@ -1,6 +1,3 @@ -#include "networkdiscovery.h" -#include - #include #include #include @@ -16,14 +13,30 @@ #include #include -#include "config.h" -#include "client.h" +#ifdef __cplusplus +extern "C" { +#endif +#include "dhcpcd/config.h" +#include "dhcpcd/client.h" #include "dhcpcd/dhcpcd.h" -#include "dhcp.h" -#include "interface.h" -#include "logger.h" -#include "socket.h" -#include "version.h" +#include "dhcpcd/dhcp.h" +#include "dhcpcd/interface.h" +#include "dhcpcd/logger.h" +#include "dhcpcd/socket.h" +#include "dhcpcd/version.h" +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus + +#include +#include "networkdiscovery.h" +#include +#include + + +#endif NetworkDiscovery::NetworkDiscovery(QWidget *parent) : QWidget(parent) @@ -35,6 +48,7 @@ NetworkDiscovery::NetworkDiscovery(QWidget *parent) * test if i get some dhcp values for the dummy0 interface */ doDHCP(interfaces); + doDhcp(interfaces.first()); } NetworkDiscovery::~NetworkDiscovery() @@ -45,7 +59,6 @@ NetworkDiscovery::~NetworkDiscovery() QList NetworkDiscovery::getListOfNetworkInterfaces() { QList nIList = QNetworkInterface::allInterfaces(); QList result; - foreach(QNetworkInterface nI, nIList) { if (((!(nI.flags() & QNetworkInterface::CanBroadcast)|| nI.flags() & QNetworkInterface::IsLoopBack) || @@ -61,251 +74,46 @@ QList NetworkDiscovery::getListOfNetworkInterfaces() { int NetworkDiscovery::doDHCP(QList interfaces) { - + return 0; } -static int atoint (const char *s) -{ - char *t; - long n; - errno = 0; - n = strtol (s, &t, 0); - if ((errno != 0 && n == 0) || s == t || - (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))) - { - logger (LOG_ERR, "`%s' out of range", s); - return (-1); - } - return ((int) n); -} - -static pid_t read_pid (const char *pidfile) +int NetworkDiscovery::doDhcp(QNetworkInterface interface) { - FILE *fp; - pid_t pid = 0; - - if ((fp = fopen (pidfile, "r")) == NULL) { - errno = ENOENT; - return 0; - } - - fscanf (fp, "%d", &pid); - fclose (fp); - - return (pid); + nd_main("eth0"); } -int NetworkDiscovery::doDhcp(QNetworkInterface interface) +void NetworkDiscovery::listenToLogger() { + skt = new QLocalSocket(this); - options_t *options; - int userclasses = 0; - int opt; - int option_index = 0; - char *prefix; - pid_t pid; - int debug = 0; - int i; - int pidfd = -1; - int sig = 0; - int retval = EXIT_FAILURE; - - /* Close any un-needed fd's */ - for (i = getdtablesize() - 1; i >= 3; --i) - close(i); - - openlog(PACKAGE, LOG_PID, LOG_LOCAL0); - - options = xzalloc(sizeof(*options)); - options->script = (char *) DEFAULT_SCRIPT; - snprintf(options->classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION); - - options->doarp = true; - options->dodns = true; - options->domtu = true; - options->donis = true; - options->dontp = true; - options->dogateway = true; - options->daemonise = true; - options->doinform = false; - options->doipv4ll = true; - options->doduid = true; - options->timeout = DEFAULT_TIMEOUT; - - gethostname(options->hostname, sizeof(options->hostname)); - if (strcmp(options->hostname, "(none)") == 0 || - strcmp(options->hostname, "localhost") == 0) - memset(options->hostname, 0, sizeof(options->hostname)); - - -/* - * kann vermutlich gelöscht werden. - -#ifdef THERE_IS_NO_FORK - dhcpcd_argv = argv; - dhcpcd_argc = argc; - if (! realpath (argv[0], dhcpcd)) { - logger (LOG_ERR, "unable to resolve the path `%s': %s", - argv[0], strerror (errno)); - goto abort; - } -#endif -*/ - - //TODO:: do something with the .toStdString()!! may cause errors cause char is expected - - if (interface.humanReadableName().length() > IF_NAMESIZE) { - - logger(LOG_ERR, "`%s' too long for an interface name (max=%d)", - interface.humanReadableName().toStdString(), IF_NAMESIZE); - goto abort; - } else { - strlcpy(options->interface, interface.humanReadableName().toStdString(), sizeof(options->interface)); - } - - 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) - options->keep_address = true; - } - - if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) { - logger(LOG_ERR, "you are not allowed to request a link local address"); - goto abort; - } - - if (geteuid()) - logger(LOG_WARNING, PACKAGE " will not work correctly unless" - " run as root"); - - prefix = xmalloc(sizeof(char) * (IF_NAMESIZE + 3)); - snprintf(prefix, IF_NAMESIZE, "%s: ", options->interface); - setlogprefix(prefix); - snprintf(options->pidfile, sizeof(options->pidfile), PIDFILE, - options->interface); - free(prefix); - - chdir("/"); - umask(022); - - if (mkdir(INFODIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH) && errno != EEXIST) { - logger(LOG_ERR, "mkdir(\"%s\",0): %s\n", INFODIR, strerror(errno)); - goto abort; - } - - if (mkdir(ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH - | S_IXOTH) && errno != EEXIST) { - logger(LOG_ERR, "mkdir(\"%s\",0): %s\n", ETCDIR, strerror(errno)); - goto abort; - } - - if (options->test) { - if (options->dorequest || options->doinform) { - logger(LOG_ERR, "cannot test with --inform or --request"); - goto abort; - } - - if (options->dolastlease) { - logger(LOG_ERR, "cannot test with --lastlease"); - goto abort; - } - - if (sig != 0) { - logger(LOG_ERR, "cannot test with --release or --renew"); - goto abort; - } - } - - if (sig != 0) { - int killed = -1; - pid = read_pid(options->pidfile); - if (pid != 0) - logger(LOG_INFO, "sending signal %d to pid %d", sig, pid); - - if (!pid || (killed = kill(pid, sig))) - logger(sig == SIGALRM ? LOG_INFO : LOG_ERR, ""PACKAGE" not running"); - - if (pid != 0 && (sig != SIGALRM || killed != 0)) - unlink(options->pidfile); - - if (killed == 0) { - retval = EXIT_SUCCESS; - goto abort; - } - - if (sig != SIGALRM) - goto abort; - } - - if (!options->test && !options->daemonised) { - if ((pid = read_pid(options->pidfile)) > 0 && kill(pid, 0) == 0) { - logger(LOG_ERR, ""PACKAGE - " already running on pid %d (%s)", pid, options->pidfile); - goto abort; - } - - pidfd = open(options->pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664); - if (pidfd == -1) { - logger(LOG_ERR, "open `%s': %s", options->pidfile, strerror(errno)); - goto abort; - } - - /* Lock the file so that only one instance of dhcpcd runs - * on an interface */ - if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) { - logger(LOG_ERR, "flock `%s': %s", options->pidfile, strerror(errno)); - goto abort; - } - - /* dhcpcd.sh should not interhit this fd */ - if ((i = fcntl(pidfd, F_GETFD, 0)) == -1 || fcntl(pidfd, F_SETFD, i - | FD_CLOEXEC) == -1) - logger(LOG_ERR, "fcntl: %s", strerror(errno)); - - writepid(pidfd, getpid()); - logger(LOG_INFO, PACKAGE " " VERSION " starting"); - } - - /* Seed random */ - srandomdev(); - - /* Massage our filters per platform */ - setup_packet_filters(); - - /** - * here the dhcp magic begins. - * the dhcp protocol is started here - */ - if (dhcp_run(options, &pidfd) == 0) - retval = EXIT_SUCCESS; - - abort: - /* If we didn't daemonise then we need to punt the pidfile now */ - if (pidfd > -1) { - close(pidfd); - unlink(options->pidfile); - } - - free(options); - -#ifdef THERE_IS_NO_FORK - /* There may have been an error before the dhcp_run function - * clears this, so just do it here to be safe */ - free (dhcpcd_skiproutes); -#endif - - logger(LOG_INFO, "exiting"); - - exit(retval); - /* NOTREACHED */ +// connect(socket, SIGNAL(readyRead()), this, ) + connect(skt, SIGNAL(error(QLocalSocket::LocalSocketError)), + this, SLOT(displayError(QLocalSocket::LocalSocketError))); } +void NetworkDiscovery::displayError(QLocalSocket::LocalSocketError socketError) + { + switch (socketError) { + case QLocalSocket::ServerNotFoundError: + QMessageBox::information(this, tr("Fortune Client"), + tr("The host was not found. Please check the " + "host name and port settings.")); + break; + case QLocalSocket::ConnectionRefusedError: + QMessageBox::information(this, tr("Fortune Client"), + tr("The connection was refused by the peer. " + "Make sure the fortune server is running, " + "and check that the host name and port " + "settings are correct.")); + break; + case QLocalSocket::PeerClosedError: + break; + default: + QMessageBox::information(this, tr("Fortune Client"), + tr("The following error occurred: %1.") + .arg(skt->errorString())); + } + } + diff --git a/workspace/networkDiscovery/networkdiscovery.h b/workspace/networkDiscovery/networkdiscovery.h index 2b4e664..8001bdf 100644 --- a/workspace/networkDiscovery/networkdiscovery.h +++ b/workspace/networkDiscovery/networkdiscovery.h @@ -1,8 +1,10 @@ #ifndef NETWORKDISCOVERY_H #define NETWORKDISCOVERY_H + #include #include +#include #include "ui_networkdiscovery.h" class NetworkDiscovery : public QWidget @@ -13,11 +15,17 @@ public: NetworkDiscovery(QWidget *parent = 0); ~NetworkDiscovery(); +public slots: + void displayError(QLocalSocket::LocalSocketError socketError); + private: Ui::NetworkDiscoveryClass ui; QList getListOfNetworkInterfaces(); int doDHCP(QList interfaces); - int doDhcp(QNetworkInterface interface) + int doDhcp(QNetworkInterface interface); + void listenToLogger(); + + QLocalSocket *skt; }; -- cgit v1.2.3-55-g7522