]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/sslSniffer/sslSnifferTest/snifftest.c
Final commit before tagging - cosmetic changes only.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / sslSniffer / sslSnifferTest / snifftest.c
1 /* snifftest.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/settings.h>
27
28 #ifdef _WIN32
29     #define CYASSL_SNIFFER
30 #endif
31
32 #ifndef CYASSL_SNIFFER
33
34 /* blank build */
35 #include <stdio.h>
36 #include <stdlib.h>
37 int main(void)
38 {
39     printf("do ./configure --enable-sniffer to enable build support\n");
40     return EXIT_SUCCESS;
41 }
42
43 #else
44 /* do a full build */
45
46 #ifdef _MSC_VER
47         /* builds on *nix too, for scanf device and port */
48         #define _CRT_SECURE_NO_WARNINGS
49 #endif
50
51 #include <pcap/pcap.h>     /* pcap stuff */
52 #include <stdio.h>         /* printf */
53 #include <stdlib.h>        /* EXIT_SUCCESS */
54 #include <string.h>        /* strcmp */
55 #include <signal.h>        /* signal */
56
57 #include <cyassl/sniffer.h>
58
59
60 #ifndef _WIN32
61     #include <arpa/inet.h>
62 #endif
63
64 typedef unsigned char byte;
65
66 enum {
67     ETHER_IF_FRAME_LEN = 14,   /* ethernet interface frame length */
68     NULL_IF_FRAME_LEN =   4,   /* no link interface frame length  */
69 };
70
71
72 pcap_t* pcap = NULL;
73 pcap_if_t* alldevs = NULL;
74
75
76 static void FreeAll(void)
77 {
78     if (pcap)
79         pcap_close(pcap);
80     if (alldevs)
81         pcap_freealldevs(alldevs);
82 #ifndef _WIN32
83     ssl_FreeSniffer();
84 #endif
85 }
86
87 static void sig_handler(const int sig) 
88 {
89     printf("SIGINT handled = %d.\n", sig);
90     FreeAll();
91     if (sig)
92         exit(EXIT_SUCCESS);
93 }
94
95
96 static void err_sys(const char* msg)
97 {
98         fprintf(stderr, "%s\n", msg);
99     if (msg)
100             exit(EXIT_FAILURE);
101 }
102
103
104 #ifdef _WIN32
105         #define SNPRINTF _snprintf
106 #else
107         #define SNPRINTF snprintf
108 #endif
109
110
111 static char* iptos(unsigned int addr)
112 {
113         static char    output[32];
114         byte *p = (byte*)&addr;
115
116         SNPRINTF(output, sizeof(output), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
117
118         return output;
119 }
120
121
122 int main(int argc, char** argv)
123 {
124     int          ret = 0;
125         int                  inum;
126         int                  port;
127     int          saveFile = 0;
128         int                  i = 0;
129     int          frame = ETHER_IF_FRAME_LEN; 
130     char         err[PCAP_ERRBUF_SIZE];
131         char         filter[32];
132         const char  *server = NULL;
133         struct       bpf_program fp;
134         pcap_if_t   *d;
135         pcap_addr_t *a;
136
137     signal(SIGINT, sig_handler);
138
139 #ifndef _WIN32
140     ssl_InitSniffer();   /* dll load on Windows */
141 #endif
142     ssl_Trace("./tracefile.txt", err);
143
144     if (argc == 1) {
145         /* normal case, user chooses device and port */
146
147             if (pcap_findalldevs(&alldevs, err) == -1)
148                     err_sys("Error in pcap_findalldevs");
149
150             for (d = alldevs; d; d=d->next) {
151                     printf("%d. %s", ++i, d->name);
152                     if (d->description)
153                             printf(" (%s)\n", d->description);
154                     else
155                             printf(" (No description available)\n");
156             }
157
158             if (i == 0)
159                     err_sys("No interfaces found! Make sure pcap or WinPcap is"
160                     " installed correctly and you have sufficient permissions");
161
162             printf("Enter the interface number (1-%d): ", i);
163             ret = scanf("%d", &inum);
164         if (ret != 1)
165             printf("scanf port failed\n");
166
167             if (inum < 1 || inum > i)
168                     err_sys("Interface number out of range");
169
170             /* Jump to the selected adapter */
171             for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
172
173             pcap = pcap_create(d->name, err);
174
175         if (pcap == NULL) printf("pcap_create failed %s\n", err);
176
177             /* get an IPv4 address */
178             for (a = d->addresses; a; a = a->next) {
179                     switch(a->addr->sa_family)
180                     {
181                             case AF_INET:
182                                     server = 
183                         iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr);
184                                     printf("server = %s\n", server);
185                                     break;
186
187                 default:
188                     break;
189                     }
190             }
191             if (server == NULL)
192                     err_sys("Unable to get device IPv4 address");
193
194         ret = pcap_set_snaplen(pcap, 65536);
195         if (ret != 0) printf("pcap_set_snaplen failed %s\n", pcap_geterr(pcap));
196
197         ret = pcap_set_timeout(pcap, 1000); 
198         if (ret != 0) printf("pcap_set_timeout failed %s\n", pcap_geterr(pcap));
199
200         ret = pcap_set_buffer_size(pcap, 1000000); 
201         if (ret != 0)
202                     printf("pcap_set_buffer_size failed %s\n", pcap_geterr(pcap));
203
204         ret = pcap_set_promisc(pcap, 1); 
205         if (ret != 0) printf("pcap_set_promisc failed %s\n", pcap_geterr(pcap));
206
207
208         ret = pcap_activate(pcap);
209         if (ret != 0) printf("pcap_activate failed %s\n", pcap_geterr(pcap));
210
211             printf("Enter the port to scan: ");
212             ret = scanf("%d", &port);
213         if (ret != 1)
214             printf("scanf port failed\n");
215
216             SNPRINTF(filter, sizeof(filter), "tcp and port %d", port);
217
218             ret = pcap_compile(pcap, &fp, filter, 0, 0);
219         if (ret != 0) printf("pcap_compile failed %s\n", pcap_geterr(pcap));
220
221         ret = pcap_setfilter(pcap, &fp);
222         if (ret != 0) printf("pcap_setfilter failed %s\n", pcap_geterr(pcap));
223
224         ret = ssl_SetPrivateKey(server, port, "../../certs/server-key.pem",
225                                FILETYPE_PEM, NULL, err);
226         if (ret != 0) {
227             printf("Please run directly from sslSniffer/sslSnifferTest dir\n");
228         }
229     }
230     else if (argc >= 3) {
231         saveFile = 1;
232         pcap = pcap_open_offline(argv[1], err);
233         if (pcap == NULL) {
234             printf("pcap_open_offline failed %s\n", err);
235             ret = -1;
236         }
237         else {
238             const char* passwd = NULL;
239             /* defaults for server and port */
240             port = 443;
241             server = "127.0.0.1";
242
243             if (argc >= 4)
244                 server = argv[3];
245
246             if (argc >= 5)
247                 port = atoi(argv[4]);
248
249             if (argc >= 6)
250                 passwd = argv[5];
251
252             ret = ssl_SetPrivateKey(server, port, argv[2],
253                                     FILETYPE_PEM, passwd, err);
254         }
255     }
256     else {
257         /* usage error */
258         printf( "usage: ./snifftest or ./snifftest dump pemKey"
259                 " [server] [port] [password]\n");
260         exit(EXIT_FAILURE);
261     }
262
263     if (ret != 0)
264         err_sys(err);
265
266     if (pcap_datalink(pcap) == DLT_NULL) 
267         frame = NULL_IF_FRAME_LEN;
268
269     while (1) {
270         static int packetNumber = 0;
271         struct pcap_pkthdr header;
272         const unsigned char* packet = pcap_next(pcap, &header);
273         packetNumber++;
274         if (packet) {
275
276             byte data[65535+16384];  /* may have a partial 16k record cached */
277
278             if (header.caplen > 40)  { /* min ip(20) + min tcp(20) */
279                                 packet        += frame;
280                                 header.caplen -= frame;                                 
281             }
282             else
283                 continue;
284
285             ret = ssl_DecodePacket(packet, header.caplen, data, err);
286             if (ret < 0)
287                 printf("ssl_Decode ret = %d, %s\n", ret, err);
288             if (ret > 0) {
289                 data[ret] = 0;
290                                 printf("SSL App Data(%d:%d):%s\n", packetNumber, ret, data);
291             }
292         }
293         else if (saveFile)
294             break;      /* we're done reading file */
295     }
296     FreeAll();
297
298     return EXIT_SUCCESS;
299 }
300
301 #endif /* full build */