summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/wellenreiter/daemon/source/daemon.cc18
1 files changed, 11 insertions, 7 deletions
diff --git a/noncore/net/wellenreiter/daemon/source/daemon.cc b/noncore/net/wellenreiter/daemon/source/daemon.cc
index d77d987..3a28217 100644
--- a/noncore/net/wellenreiter/daemon/source/daemon.cc
+++ b/noncore/net/wellenreiter/daemon/source/daemon.cc
@@ -27,176 +27,180 @@ int main(int argc, char **argv)
27 27
28 fprintf(stderr, "wellenreiterd %s\n\n", VERSION); 28 fprintf(stderr, "wellenreiterd %s\n\n", VERSION);
29 fprintf(stderr, "(c) 2002 by M-M-M\n\n"); 29 fprintf(stderr, "(c) 2002 by M-M-M\n\n");
30 30
31 if(argc < 3) 31 if(argc < 3)
32 usage(); 32 usage();
33 33
34 /* Set sniffer device */ 34 /* Set sniffer device */
35 memset(cardtype.iface, 0, sizeof(cardtype.iface)); 35 memset(cardtype.iface, 0, sizeof(cardtype.iface));
36 strncpy(cardtype.iface, (char *)argv[1], sizeof(cardtype.iface) - 1); 36 strncpy(cardtype.iface, (char *)argv[1], sizeof(cardtype.iface) - 1);
37 37
38 /* Set card type */ 38 /* Set card type */
39 cardtype.type = atoi(argv[2]); 39 cardtype.type = atoi(argv[2]);
40 if(cardtype.type < 1 || cardtype.type > 4) 40 if(cardtype.type < 1 || cardtype.type > 4)
41 usage(); 41 usage();
42 42
43 /* set card into monitor mode */ 43 /* set card into monitor mode */
44 if(!card_into_monitormode(&handletopcap, cardtype.iface, 44 if(!card_into_monitormode(&handletopcap, cardtype.iface,
45 cardtype.type)) 45 cardtype.type))
46 { 46 {
47 wl_logerr("Cannot initialize the wireless-card, aborting"); 47 wl_logerr("Cannot initialize the wireless-card, aborting");
48 exit(EXIT_FAILURE); 48 exit(EXIT_FAILURE);
49 } 49 }
50 wl_loginfo("Set card into monitor mode"); 50 wl_loginfo("Set card into monitor mode");
51 51
52 /* setup pcap */ 52 /* setup pcap */
53 if((handletopcap = pcap_open_live(cardtype.iface, 53 if((handletopcap = pcap_open_live(cardtype.iface,
54 BUFSIZ, 1, 0, NULL)) == NULL) 54 BUFSIZ, 1, 0, NULL)) == NULL)
55 { 55 {
56 wl_logerr("pcap_open_live() failed: %s", strerror(errno)); 56 wl_logerr("pcap_open_live() failed: %s", strerror(errno));
57 exit(EXIT_FAILURE); 57 exit(EXIT_FAILURE);
58 } 58 }
59 59
60#ifdef HAVE_PCAP_NONBLOCK 60#ifdef HAVE_PCAP_NONBLOCK
61 pcap_setnonblock(handletopcap, 1, NULL); 61 pcap_setnonblock(handletopcap, 1, NULL);
62#endif 62#endif
63 63
64 /* Setup socket for incoming commands */ 64 /* Setup socket for incoming commands */
65 if((sock=wl_setupsock(DAEMONADDR, DAEMONPORT, saddr)) < 0) 65 if((sock=wl_setupsock(DAEMONADDR, DAEMONPORT, saddr)) < 0)
66 { 66 {
67 wl_logerr("Cannot setup socket"); 67 wl_logerr("Cannot setup socket");
68 exit(EXIT_FAILURE); 68 exit(EXIT_FAILURE);
69 } 69 }
70 wl_loginfo("Set up socket '%d' for GUI communication", sock); 70 wl_loginfo("Set up socket '%d' for GUI communication", sock);
71 71
72 /* Create channelswitching thread */ 72 /* Create channelswitching thread */
73 if(pthread_create(&sub, NULL, channel_switcher, 73 if(pthread_create(&sub, NULL, channel_switcher,
74 (void *)&cardtype) != 0) 74 (void *)&cardtype) != 0)
75 { 75 {
76 wl_logerr("Cannot create thread: %s", strerror(errno)); 76 wl_logerr("Cannot create thread: %s", strerror(errno));
77 close(sock); 77 close(sock);
78 exit(EXIT_FAILURE); 78 exit(EXIT_FAILURE);
79 } 79 }
80 if(pthread_detach(sub)) 80 if(pthread_detach(sub))
81 { 81 {
82 wl_logerr("Error detaching thread"); 82 wl_logerr("Error detaching thread");
83 close(sock); 83 close(sock);
84 pthread_exit((pthread_t *)sub); 84 pthread_exit((pthread_t *)sub);
85 exit(EXIT_FAILURE); 85 exit(EXIT_FAILURE);
86 } 86 }
87 wl_loginfo("Created and detached channel switching thread"); 87 wl_loginfo("Created and detached channel switching thread");
88 88
89 FD_ZERO(&rset); 89 FD_ZERO(&rset);
90 90
91 /* Start main loop */ 91 /* Start main loop */
92 wl_loginfo("Starting main loop"); 92 wl_loginfo("Starting main loop");
93 while(1) 93 while(1)
94 { 94 {
95 95
96 FD_SET(sock, &rset); 96 FD_SET(sock, &rset);
97 FD_SET(pcap_fileno(handletopcap), &rset); 97 FD_SET(pcap_fileno(handletopcap), &rset);
98 98
99 /* maxfd = biggest filefd */ 99 /* maxfd = biggest filefd */
100 maxfd = (sock > pcap_fileno(handletopcap) ? 100 maxfd = (sock > pcap_fileno(handletopcap) ?
101 sock + 1 : pcap_fileno(handletopcap)) + 1; 101 sock + 1 : pcap_fileno(handletopcap)) + 1;
102 102
103 if(select(maxfd, &rset, NULL, NULL, NULL) < 0) 103 if(select(maxfd, &rset, NULL, NULL, NULL) < 0)
104 { 104 {
105 wl_logerr("Error calling select: %s", strerror(errno)); 105 wl_logerr("Error calling select: %s", strerror(errno));
106 break; 106 break;
107 } 107 }
108 108
109 /* Got data on local socket from GUI */ 109 /* Got data on local socket from GUI */
110 if(FD_ISSET(sock, &rset)) 110 if(FD_ISSET(sock, &rset))
111 { 111 {
112 /* Receive data from socket */ 112 /* Receive data from socket */
113 if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0) 113 if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0)
114 { 114 {
115 wl_logerr("Error trying to read: %s", strerror(errno)); 115 wl_logerr("Error trying to read: %s", strerror(errno));
116 break; 116 break;
117 } 117 }
118 else 118 else
119 { 119 {
120 /* check type of packet and start function according to it */ 120 /* check type of packet and start function according to it */
121 switch(retval) 121 switch(retval)
122 { 122 {
123 case 98: 123 case STARTSNIFF:
124 wl_loginfo("Received STARTSNIFF command"); 124 wl_loginfo("Received STARTSNIFF command");
125 break; 125 if(!send_ok(GUIADDR, GUIPORT, STARTSNIFF))
126 case 99: 126 wl_logerr("Cannot set OK_CMD to GUI");
127 wl_loginfo("Received STOPSNIFF command"); 127 break;
128 break; 128 case STOPSNIFF:
129 default: 129 wl_loginfo("Received STOPSNIFF command");
130 if(!send_ok(GUIADDR, GUIPORT, STOPSNIFF))
131 wl_logerr("Cannot set FAIL_CMD to GUI");
132 break;
133 default:
130 wl_logerr("Received unknown command: %d", retval); 134 wl_logerr("Received unknown command: %d", retval);
131 break; 135 break;
132 } 136 }
133 } 137 }
134 } /* FD_ISSET */ 138 } /* FD_ISSET */
135 139
136 /* Check pcap lib for packets */ 140 /* Check pcap lib for packets */
137 if(FD_ISSET(pcap_fileno(handletopcap), &rset)) 141 if(FD_ISSET(pcap_fileno(handletopcap), &rset))
138 { 142 {
139 143
140 /* Grab one single packet */ 144 /* Grab one single packet */
141 packet = pcap_next(handletopcap, &header); 145 packet = pcap_next(handletopcap, &header);
142 146
143 /* process the packet */ 147 /* process the packet */
144 process_packets(&header,*&packet, GUIADDR, GUIPORT); 148 process_packets(&header,*&packet, GUIADDR, GUIPORT);
145 } 149 }
146 150
147 } /* while(1) */ 151 } /* while(1) */
148 152
149 close(sock); 153 close(sock);
150 exit(EXIT_SUCCESS); 154 exit(EXIT_SUCCESS);
151} 155}
152 156
153void 157void
154usage(void) 158usage(void)
155{ 159{
156 fprintf(stderr, "Usage: wellenreiter <device> <cardtype>\n" \ 160 fprintf(stderr, "Usage: wellenreiter <device> <cardtype>\n" \
157 "\t<device> = Wirelessdevice (e.g. wlan0)\n" \ 161 "\t<device> = Wirelessdevice (e.g. wlan0)\n" \
158 "\t<cardtype> = Cardtype:\tCisco\t= 1\n" \ 162 "\t<cardtype> = Cardtype:\tCisco\t= 1\n" \
159 "\t\t\t\tNG\t= 2\n" \ 163 "\t\t\t\tNG\t= 2\n" \
160 "\t\t\t\tHOSTAP\t= 3\n" \ 164 "\t\t\t\tHOSTAP\t= 3\n" \
161 "\t\t\t\tLUCENT\t= 4\n"); 165 "\t\t\t\tLUCENT\t= 4\n");
162 exit(EXIT_FAILURE); 166 exit(EXIT_FAILURE);
163} 167}
164 168
165void * 169void *
166channel_switcher(void *cardtypeptr) 170channel_switcher(void *cardtypeptr)
167{ 171{
168 wl_cardtype_t *cardtype; 172 wl_cardtype_t *cardtype;
169 int maxchan=0; 173 int maxchan=0;
170 int channel=1; 174 int channel=1;
171 /* Get card info struct */ 175 /* Get card info struct */
172 cardtype = (wl_cardtype_t *)cardtypeptr; 176 cardtype = (wl_cardtype_t *)cardtypeptr;
173 /* Get from the wireless extension the amount of available channels 177 /* Get from the wireless extension the amount of available channels
174 this prevents a fail in switching on us cards */ 178 this prevents a fail in switching on us cards */
175 maxchan = card_detect_channels(cardtype->iface); 179 maxchan = card_detect_channels(cardtype->iface);
176 180
177 if (maxchan < MAXCHANNEL) 181 if (maxchan < MAXCHANNEL)
178 { 182 {
179 #undef MAXCHANNEL 183 #undef MAXCHANNEL
180 #define MAXCHANNEL maxchan 184 #define MAXCHANNEL maxchan
181 } 185 }
182 186
183 while(1) 187 while(1)
184 { 188 {
185 189
186 /* If channel bigger than maxchannel, set to 1 */ 190 /* If channel bigger than maxchannel, set to 1 */
187 if(channel > MAXCHANNEL) 191 if(channel > MAXCHANNEL)
188 channel=1; 192 channel=1;
189 193
190 /* Set channel */ 194 /* Set channel */
191 if(!card_set_channel(cardtype->iface, channel, cardtype->type)) 195 if(!card_set_channel(cardtype->iface, channel, cardtype->type))
192 { 196 {
193 wl_logerr("Cannot set channel, thread exiting"); 197 wl_logerr("Cannot set channel, thread exiting");
194 pthread_exit(NULL); 198 pthread_exit(NULL);
195 } 199 }
196 200
197 /* sleep */ 201 /* sleep */
198 usleep(CHANINTERVAL); 202 usleep(CHANINTERVAL);
199 203
200 channel++; 204 channel++;
201 } /* while */ 205 } /* while */
202} 206}