-rw-r--r-- | noncore/net/wellenreiter/daemon/source/daemon.cc | 109 | ||||
-rw-r--r-- | noncore/net/wellenreiter/daemon/source/daemon.hh | 3 |
2 files changed, 69 insertions, 43 deletions
diff --git a/noncore/net/wellenreiter/daemon/source/daemon.cc b/noncore/net/wellenreiter/daemon/source/daemon.cc index b57e6a0..4407436 100644 --- a/noncore/net/wellenreiter/daemon/source/daemon.cc +++ b/noncore/net/wellenreiter/daemon/source/daemon.cc | |||
@@ -6,29 +6,11 @@ | |||
6 | 6 | ||
7 | #include "config.hh" | 7 | #include "config.hh" |
8 | #include "daemon.hh" | 8 | #include "daemon.hh" |
9 | 9 | ||
10 | // temporary solution, will be removed soon | 10 | /* should be parsed from cfg-file */ |
11 | #define MAXCHANNEL 13 | 11 | #define MAXCHANNEL 13 |
12 | int card_type; | 12 | #define CHANINTERVAL 100000 |
13 | char sniffer_device[6]; | ||
14 | int channel=0; | ||
15 | int timedout=1; | ||
16 | |||
17 | void chanswitch(int blah) | ||
18 | { | ||
19 | if(channel >= MAXCHANNEL) | ||
20 | { | ||
21 | channel=1; | ||
22 | } | ||
23 | else | ||
24 | { | ||
25 | channel++; | ||
26 | } | ||
27 | card_set_channel(sniffer_device, channel, card_type); | ||
28 | timedout=0; | ||
29 | alarm(1); | ||
30 | } | ||
31 | 13 | ||
32 | /* Main function of wellenreiterd */ | 14 | /* Main function of wellenreiterd */ |
33 | int main(int argc, char **argv) | 15 | int main(int argc, char **argv) |
34 | { | 16 | { |
@@ -36,8 +18,10 @@ int main(int argc, char **argv) | |||
36 | char buffer[WL_SOCKBUF]; | 18 | char buffer[WL_SOCKBUF]; |
37 | struct pcap_pkthdr header; | 19 | struct pcap_pkthdr header; |
38 | struct sockaddr_in saddr; | 20 | struct sockaddr_in saddr; |
39 | pcap_t *handletopcap; | 21 | pcap_t *handletopcap; |
22 | wl_cardtype_t cardtype; | ||
23 | pthread_t sub; | ||
40 | const unsigned char *packet; | 24 | const unsigned char *packet; |
41 | 25 | ||
42 | fd_set rset; | 26 | fd_set rset; |
43 | 27 | ||
@@ -46,48 +30,62 @@ int main(int argc, char **argv) | |||
46 | 30 | ||
47 | if(argc < 3) | 31 | if(argc < 3) |
48 | usage(); | 32 | usage(); |
49 | 33 | ||
50 | // removed soon, see above | ||
51 | signal(SIGALRM, chanswitch); | ||
52 | alarm(1); | ||
53 | /* Set sniffer device */ | 34 | /* Set sniffer device */ |
54 | memset(sniffer_device, 0, sizeof(sniffer_device)); | 35 | memset(cardtype.iface, 0, sizeof(cardtype.iface)); |
55 | strncpy(sniffer_device, (char *)argv[1], sizeof(sniffer_device) - 1); | 36 | strncpy(cardtype.iface, (char *)argv[1], sizeof(cardtype.iface) - 1); |
56 | 37 | ||
57 | /* Set card type */ | 38 | /* Set card type */ |
58 | card_type = atoi(argv[2]); | 39 | cardtype.type = atoi(argv[2]); |
59 | if(card_type < 1 || card_type > 4) | 40 | if(cardtype.type < 1 || cardtype.type > 4) |
60 | usage(); | 41 | usage(); |
61 | 42 | ||
62 | if(!card_into_monitormode(&handletopcap, sniffer_device, card_type)) | 43 | if(!card_into_monitormode(&handletopcap, cardtype.iface, cardtype.type)) |
63 | { | 44 | { |
64 | wl_logerr("Cannot initialize the wireless-card, aborting"); | 45 | wl_logerr("Cannot initialize the wireless-card, aborting"); |
65 | exit(-1); | 46 | exit(EXIT_FAILURE); |
66 | } | 47 | } |
67 | wl_loginfo("Set card into monitor mode"); | 48 | wl_loginfo("Set card into monitor mode"); |
68 | 49 | ||
50 | |||
69 | /////// following line will be moved to lib as soon as possible //////////// | 51 | /////// following line will be moved to lib as soon as possible //////////// |
70 | if((handletopcap = pcap_open_live(sniffer_device, BUFSIZ, 1, 0, NULL)) == NULL) | 52 | if((handletopcap = pcap_open_live(cardtype.iface, BUFSIZ, 1, 0, NULL)) == NULL) |
71 | { | 53 | { |
72 | wl_logerr("pcap_open_live() failed: %s", strerror(errno)); | 54 | wl_logerr("pcap_open_live() failed: %s", strerror(errno)); |
73 | exit(-1); | 55 | exit(EXIT_FAILURE); |
74 | } | 56 | } |
75 | 57 | ||
76 | #ifdef HAVE_PCAP_NONBLOCK | 58 | #ifdef HAVE_PCAP_NONBLOCK |
77 | pcap_setnonblock(handletopcap, 1, NULL); | 59 | pcap_setnonblock(handletopcap, 1, NULL); |
78 | #endif | 60 | #endif |
79 | 61 | //////////////////////////////////////// | |
80 | //////////////////////////////////////// | ||
81 | 62 | ||
63 | |||
82 | /* Setup socket for incoming commands */ | 64 | /* Setup socket for incoming commands */ |
83 | if((sock=wl_setupsock(DAEMONADDR, DAEMONPORT, saddr)) < 0) | 65 | if((sock=wl_setupsock(DAEMONADDR, DAEMONPORT, saddr)) < 0) |
84 | { | 66 | { |
85 | wl_logerr("Cannot setup socket"); | 67 | wl_logerr("Cannot setup socket"); |
86 | exit(-1); | 68 | exit(EXIT_FAILURE); |
87 | } | 69 | } |
88 | wl_loginfo("Set up socket '%d' for GUI communication", sock); | 70 | wl_loginfo("Set up socket '%d' for GUI communication", sock); |
89 | 71 | ||
72 | /* Create channelswitching thread */ | ||
73 | if(pthread_create(&sub, NULL, channel_switcher, (void *)&cardtype) != 0) | ||
74 | { | ||
75 | wl_logerr("Cannot create thread: %s", strerror(errno)); | ||
76 | close(sock); | ||
77 | exit(EXIT_FAILURE); | ||
78 | } | ||
79 | if(pthread_detach(sub)) | ||
80 | { | ||
81 | wl_logerr("Error detaching thread"); | ||
82 | close(sock); | ||
83 | pthread_exit((pthread_t *)sub); | ||
84 | exit(EXIT_FAILURE); | ||
85 | } | ||
86 | wl_loginfo("Created and detached channel switching thread"); | ||
87 | |||
90 | FD_ZERO(&rset); | 88 | FD_ZERO(&rset); |
91 | 89 | ||
92 | /* Start main loop */ | 90 | /* Start main loop */ |
93 | wl_loginfo("Starting main loop"); | 91 | wl_loginfo("Starting main loop"); |
@@ -96,15 +94,12 @@ int main(int argc, char **argv) | |||
96 | 94 | ||
97 | FD_SET(sock, &rset); | 95 | FD_SET(sock, &rset); |
98 | FD_SET(pcap_fileno(handletopcap), &rset); | 96 | FD_SET(pcap_fileno(handletopcap), &rset); |
99 | 97 | ||
100 | // blah | ||
101 | timedout=1; | ||
102 | |||
103 | /* socket or pcap handle bigger? Will be cleaned up, have to check pcap */ | 98 | /* socket or pcap handle bigger? Will be cleaned up, have to check pcap */ |
104 | maxfd = (sock > pcap_fileno(handletopcap) ? sock : pcap_fileno(handletopcap)) + 1; | 99 | maxfd = (sock > pcap_fileno(handletopcap) ? sock + 1: pcap_fileno(handletopcap)) + 1; |
105 | 100 | ||
106 | if(select(maxfd, &rset, NULL, NULL, NULL) < 0 && timedout) | 101 | if(select(maxfd, &rset, NULL, NULL, NULL) < 0) |
107 | { | 102 | { |
108 | wl_logerr("Error calling select: %s", strerror(errno)); | 103 | wl_logerr("Error calling select: %s", strerror(errno)); |
109 | break; | 104 | break; |
110 | } | 105 | } |
@@ -112,9 +107,9 @@ int main(int argc, char **argv) | |||
112 | /* Got data on local socket from GUI */ | 107 | /* Got data on local socket from GUI */ |
113 | if(FD_ISSET(sock, &rset)) | 108 | if(FD_ISSET(sock, &rset)) |
114 | { | 109 | { |
115 | /* Receive data from socket */ | 110 | /* Receive data from socket */ |
116 | if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0 && timedout) | 111 | if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0) |
117 | { | 112 | { |
118 | wl_logerr("Error trying to read: %s", strerror(errno)); | 113 | wl_logerr("Error trying to read: %s", strerror(errno)); |
119 | break; | 114 | break; |
120 | } | 115 | } |
@@ -149,9 +144,9 @@ int main(int argc, char **argv) | |||
149 | 144 | ||
150 | } /* while(1) */ | 145 | } /* while(1) */ |
151 | 146 | ||
152 | close(sock); | 147 | close(sock); |
153 | exit(0); | 148 | exit(EXIT_SUCCESS); |
154 | } | 149 | } |
155 | 150 | ||
156 | void usage(void) | 151 | void usage(void) |
157 | { | 152 | { |
@@ -160,6 +155,36 @@ void usage(void) | |||
160 | "\t<cardtype> = Cardtype:\tCisco\t= 1\n" \ | 155 | "\t<cardtype> = Cardtype:\tCisco\t= 1\n" \ |
161 | "\t\t\t\tNG\t= 2\n" \ | 156 | "\t\t\t\tNG\t= 2\n" \ |
162 | "\t\t\t\tHOSTAP\t= 3\n" \ | 157 | "\t\t\t\tHOSTAP\t= 3\n" \ |
163 | "\t\t\t\tLUCENT\t= 4\n"); | 158 | "\t\t\t\tLUCENT\t= 4\n"); |
164 | exit(-1); | 159 | exit(EXIT_FAILURE); |
160 | } | ||
161 | |||
162 | void * | ||
163 | channel_switcher(void *cardtypeptr) | ||
164 | { | ||
165 | wl_cardtype_t *cardtype; | ||
166 | int channel=1; | ||
167 | |||
168 | /* Get card info struct */ | ||
169 | cardtype = (wl_cardtype_t *)cardtypeptr; | ||
170 | |||
171 | while(1) | ||
172 | { | ||
173 | |||
174 | /* If channel bigger than maxchannel, set to 1 */ | ||
175 | if(channel > MAXCHANNEL) | ||
176 | channel=1; | ||
177 | |||
178 | /* Set channel */ | ||
179 | if(!card_set_channel(cardtype->iface, channel, cardtype->type)) | ||
180 | { | ||
181 | wl_logerr("Cannot set channel, thread exiting"); | ||
182 | pthread_exit(NULL); | ||
183 | } | ||
184 | |||
185 | /* sleep */ | ||
186 | usleep(CHANINTERVAL); | ||
187 | |||
188 | channel++; | ||
189 | } /* while */ | ||
165 | } | 190 | } |
diff --git a/noncore/net/wellenreiter/daemon/source/daemon.hh b/noncore/net/wellenreiter/daemon/source/daemon.hh index d50487a..f9ac45e 100644 --- a/noncore/net/wellenreiter/daemon/source/daemon.hh +++ b/noncore/net/wellenreiter/daemon/source/daemon.hh | |||
@@ -9,11 +9,11 @@ | |||
9 | #include <sys/time.h> | 9 | #include <sys/time.h> |
10 | #include <sys/socket.h> | 10 | #include <sys/socket.h> |
11 | #include <netinet/in.h> | 11 | #include <netinet/in.h> |
12 | #include <arpa/inet.h> | 12 | #include <arpa/inet.h> |
13 | #include <pthread.h> | ||
13 | #include <unistd.h> | 14 | #include <unistd.h> |
14 | #include <errno.h> | 15 | #include <errno.h> |
15 | #include <signal.h> | ||
16 | 16 | ||
17 | #include <libwellenreiter/source/wl_sock.hh> | 17 | #include <libwellenreiter/source/wl_sock.hh> |
18 | #include <libwellenreiter/source/wl_log.hh> | 18 | #include <libwellenreiter/source/wl_log.hh> |
19 | #include <libwellenreiter/source/wl_types.hh> | 19 | #include <libwellenreiter/source/wl_types.hh> |
@@ -21,6 +21,7 @@ | |||
21 | #include <libwellenreiter/source/cardmode.hh> | 21 | #include <libwellenreiter/source/cardmode.hh> |
22 | #include <libwellenreiter/source/sniff.hh> | 22 | #include <libwellenreiter/source/sniff.hh> |
23 | 23 | ||
24 | void usage(void); | 24 | void usage(void); |
25 | void *channel_switcher(void *); | ||
25 | 26 | ||
26 | #endif /* DAEMON_HH */ | 27 | #endif /* DAEMON_HH */ |