Diffstat (limited to 'noncore/net/wellenreiter/daemon/source/daemon.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/wellenreiter/daemon/source/daemon.cc | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/noncore/net/wellenreiter/daemon/source/daemon.cc b/noncore/net/wellenreiter/daemon/source/daemon.cc index 4407436..5cf4e97 100644 --- a/noncore/net/wellenreiter/daemon/source/daemon.cc +++ b/noncore/net/wellenreiter/daemon/source/daemon.cc | |||
@@ -21,91 +21,93 @@ int main(int argc, char **argv) | |||
21 | pcap_t *handletopcap; | 21 | pcap_t *handletopcap; |
22 | wl_cardtype_t cardtype; | 22 | wl_cardtype_t cardtype; |
23 | pthread_t sub; | 23 | pthread_t sub; |
24 | const unsigned char *packet; | 24 | const unsigned char *packet; |
25 | 25 | ||
26 | fd_set rset; | 26 | fd_set rset; |
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 | if(!card_into_monitormode(&handletopcap, cardtype.iface, cardtype.type)) | 43 | /* set card into monitor mode */ |
44 | if(!card_into_monitormode(&handletopcap, cardtype.iface, | ||
45 | cardtype.type)) | ||
44 | { | 46 | { |
45 | wl_logerr("Cannot initialize the wireless-card, aborting"); | 47 | wl_logerr("Cannot initialize the wireless-card, aborting"); |
46 | exit(EXIT_FAILURE); | 48 | exit(EXIT_FAILURE); |
47 | } | 49 | } |
48 | wl_loginfo("Set card into monitor mode"); | 50 | wl_loginfo("Set card into monitor mode"); |
49 | 51 | ||
50 | 52 | /* setup pcap */ | |
51 | /////// following line will be moved to lib as soon as possible //////////// | 53 | if((handletopcap = pcap_open_live(cardtype.iface, |
52 | if((handletopcap = pcap_open_live(cardtype.iface, BUFSIZ, 1, 0, NULL)) == NULL) | 54 | BUFSIZ, 1, 0, NULL)) == NULL) |
53 | { | 55 | { |
54 | wl_logerr("pcap_open_live() failed: %s", strerror(errno)); | 56 | wl_logerr("pcap_open_live() failed: %s", strerror(errno)); |
55 | exit(EXIT_FAILURE); | 57 | exit(EXIT_FAILURE); |
56 | } | 58 | } |
57 | 59 | ||
58 | #ifdef HAVE_PCAP_NONBLOCK | 60 | #ifdef HAVE_PCAP_NONBLOCK |
59 | pcap_setnonblock(handletopcap, 1, NULL); | 61 | pcap_setnonblock(handletopcap, 1, NULL); |
60 | #endif | 62 | #endif |
61 | //////////////////////////////////////// | 63 | |
62 | |||
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, (void *)&cardtype) != 0) | 73 | if(pthread_create(&sub, NULL, channel_switcher, |
74 | (void *)&cardtype) != 0) | ||
74 | { | 75 | { |
75 | wl_logerr("Cannot create thread: %s", strerror(errno)); | 76 | wl_logerr("Cannot create thread: %s", strerror(errno)); |
76 | close(sock); | 77 | close(sock); |
77 | exit(EXIT_FAILURE); | 78 | exit(EXIT_FAILURE); |
78 | } | 79 | } |
79 | if(pthread_detach(sub)) | 80 | if(pthread_detach(sub)) |
80 | { | 81 | { |
81 | wl_logerr("Error detaching thread"); | 82 | wl_logerr("Error detaching thread"); |
82 | close(sock); | 83 | close(sock); |
83 | pthread_exit((pthread_t *)sub); | 84 | pthread_exit((pthread_t *)sub); |
84 | exit(EXIT_FAILURE); | 85 | exit(EXIT_FAILURE); |
85 | } | 86 | } |
86 | wl_loginfo("Created and detached channel switching thread"); | 87 | wl_loginfo("Created and detached channel switching thread"); |
87 | 88 | ||
88 | FD_ZERO(&rset); | 89 | FD_ZERO(&rset); |
89 | 90 | ||
90 | /* Start main loop */ | 91 | /* Start main loop */ |
91 | wl_loginfo("Starting main loop"); | 92 | wl_loginfo("Starting main loop"); |
92 | while(1) | 93 | while(1) |
93 | { | 94 | { |
94 | 95 | ||
95 | FD_SET(sock, &rset); | 96 | FD_SET(sock, &rset); |
96 | FD_SET(pcap_fileno(handletopcap), &rset); | 97 | FD_SET(pcap_fileno(handletopcap), &rset); |
97 | 98 | ||
98 | /* socket or pcap handle bigger? Will be cleaned up, have to check pcap */ | 99 | /* maxfd = biggest filefd */ |
99 | maxfd = (sock > pcap_fileno(handletopcap) ? sock + 1: pcap_fileno(handletopcap)) + 1; | 100 | maxfd = (sock > pcap_fileno(handletopcap) ? |
101 | sock + 1 : pcap_fileno(handletopcap)) + 1; | ||
100 | 102 | ||
101 | if(select(maxfd, &rset, NULL, NULL, NULL) < 0) | 103 | if(select(maxfd, &rset, NULL, NULL, NULL) < 0) |
102 | { | 104 | { |
103 | wl_logerr("Error calling select: %s", strerror(errno)); | 105 | wl_logerr("Error calling select: %s", strerror(errno)); |
104 | break; | 106 | break; |
105 | } | 107 | } |
106 | 108 | ||
107 | /* Got data on local socket from GUI */ | 109 | /* Got data on local socket from GUI */ |
108 | if(FD_ISSET(sock, &rset)) | 110 | if(FD_ISSET(sock, &rset)) |
109 | { | 111 | { |
110 | /* Receive data from socket */ | 112 | /* Receive data from socket */ |
111 | if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0) | 113 | if((retval=wl_recv(&sock, saddr, buffer, sizeof(buffer))) < 0) |
@@ -113,51 +115,52 @@ int main(int argc, char **argv) | |||
113 | wl_logerr("Error trying to read: %s", strerror(errno)); | 115 | wl_logerr("Error trying to read: %s", strerror(errno)); |
114 | break; | 116 | break; |
115 | } | 117 | } |
116 | else | 118 | else |
117 | { | 119 | { |
118 | /* check type of packet and start function according to it */ | 120 | /* check type of packet and start function according to it */ |
119 | switch(retval) | 121 | switch(retval) |
120 | { | 122 | { |
121 | case 98: | 123 | case 98: |
122 | wl_loginfo("Received STARTSNIFF command"); | 124 | wl_loginfo("Received STARTSNIFF command"); |
123 | break; | 125 | break; |
124 | case 99: | 126 | case 99: |
125 | wl_loginfo("Received STOPSNIFF command"); | 127 | wl_loginfo("Received STOPSNIFF command"); |
126 | break; | 128 | break; |
127 | default: | 129 | default: |
128 | wl_logerr("Received unknown command: %d", retval); | 130 | wl_logerr("Received unknown command: %d", retval); |
129 | break; | 131 | break; |
130 | } | 132 | } |
131 | } | 133 | } |
132 | } /* FD_ISSET */ | 134 | } /* FD_ISSET */ |
133 | 135 | ||
134 | /* Check pcap lib for packets */ | 136 | /* Check pcap lib for packets */ |
135 | if(FD_ISSET(pcap_fileno(handletopcap), &rset)) | 137 | if(FD_ISSET(pcap_fileno(handletopcap), &rset)) |
136 | { | 138 | { |
137 | 139 | ||
138 | /* Grab one single packet */ | 140 | /* Grab one single packet */ |
139 | packet = pcap_next(handletopcap, &header); | 141 | packet = pcap_next(handletopcap, &header); |
140 | 142 | ||
141 | /* process the packet */ | 143 | /* process the packet */ |
142 | process_packets(&header,*&packet, GUIADDR, GUIPORT); | 144 | process_packets(&header,*&packet, GUIADDR, GUIPORT); |
143 | } | 145 | } |
144 | 146 | ||
145 | } /* while(1) */ | 147 | } /* while(1) */ |
146 | 148 | ||
147 | close(sock); | 149 | close(sock); |
148 | exit(EXIT_SUCCESS); | 150 | exit(EXIT_SUCCESS); |
149 | } | 151 | } |
150 | 152 | ||
151 | void usage(void) | 153 | void |
154 | usage(void) | ||
152 | { | 155 | { |
153 | fprintf(stderr, "Usage: wellenreiter <device> <cardtype>\n" \ | 156 | fprintf(stderr, "Usage: wellenreiter <device> <cardtype>\n" \ |
154 | "\t<device> = Wirelessdevice (e.g. wlan0)\n" \ | 157 | "\t<device> = Wirelessdevice (e.g. wlan0)\n" \ |
155 | "\t<cardtype> = Cardtype:\tCisco\t= 1\n" \ | 158 | "\t<cardtype> = Cardtype:\tCisco\t= 1\n" \ |
156 | "\t\t\t\tNG\t= 2\n" \ | 159 | "\t\t\t\tNG\t= 2\n" \ |
157 | "\t\t\t\tHOSTAP\t= 3\n" \ | 160 | "\t\t\t\tHOSTAP\t= 3\n" \ |
158 | "\t\t\t\tLUCENT\t= 4\n"); | 161 | "\t\t\t\tLUCENT\t= 4\n"); |
159 | exit(EXIT_FAILURE); | 162 | exit(EXIT_FAILURE); |
160 | } | 163 | } |
161 | 164 | ||
162 | void * | 165 | void * |
163 | channel_switcher(void *cardtypeptr) | 166 | channel_switcher(void *cardtypeptr) |