]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/sslSniffer/sslSnifferTest/snifftest.c
Add FreeRTOS-Plus directory with new directory structure so it matches the FreeRTOS...
[freertos] / FreeRTOS-Plus / Source / CyaSSL / sslSniffer / sslSnifferTest / snifftest.c
1 /* snifftest.c
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #ifdef _WIN32
27     #define CYASSL_SNIFFER
28 #endif
29
30 #ifndef CYASSL_SNIFFER
31
32 /* blank build */
33 #include <stdio.h>
34 #include <stdlib.h>
35 int main()
36 {
37     printf("do ./configure --enable-sniffer to enable build support\n");
38     return EXIT_SUCCESS;
39 }
40
41 #else
42 /* do a full build */
43
44 #ifdef _MSC_VER
45         /* builds on *nix too, for scanf device and port */
46         #define _CRT_SECURE_NO_WARNINGS
47 #endif
48
49 #include <pcap/pcap.h>     /* pcap stuff */
50 #include <stdio.h>         /* printf */
51 #include <stdlib.h>        /* EXIT_SUCCESS */
52 #include <string.h>        /* strcmp */
53 #include <signal.h>        /* signal */
54
55 #include <cyassl/sniffer.h>
56
57
58 #ifndef _WIN32
59     #include <arpa/inet.h>
60 #endif
61
62 typedef unsigned char byte;
63
64 enum {
65     ETHER_IF_FRAME_LEN = 14,   /* ethernet interface frame length */
66     NULL_IF_FRAME_LEN =   4,   /* no link interface frame length  */
67 };
68
69
70 pcap_t* pcap = 0;
71 pcap_if_t *alldevs;
72
73 static void sig_handler(const int sig) 
74 {
75     printf("SIGINT handled = %d.\n", sig);
76     if (pcap)
77         pcap_close(pcap);
78         pcap_freealldevs(alldevs);
79 #ifndef _WIN32
80     ssl_FreeSniffer();
81 #endif
82     exit(EXIT_SUCCESS);
83 }
84
85
86 static void err_sys(const char* msg)
87 {
88         fprintf(stderr, "%s\n", msg);
89         exit(EXIT_FAILURE);
90 }
91
92
93 #ifdef _WIN32
94         #define SNPRINTF _snprintf
95 #else
96         #define SNPRINTF snprintf
97 #endif
98
99
100 static char* iptos(unsigned int addr)
101 {
102         static char    output[32];
103         byte *p = (byte*)&addr;
104
105         SNPRINTF(output, sizeof(output), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
106
107         return output;
108 }
109
110
111 int main(int argc, char** argv)
112 {
113     int          ret;
114         int                  inum;
115         int                  port;
116     int          saveFile = 0;
117         int                  i = 0;
118     int          frame = ETHER_IF_FRAME_LEN; 
119     char         err[PCAP_ERRBUF_SIZE];
120         char         filter[32];
121         const char  *server = NULL;
122         struct       bpf_program fp;
123         pcap_if_t   *d;
124         pcap_addr_t *a;
125
126     signal(SIGINT, sig_handler);
127
128 #ifndef _WIN32
129     ssl_InitSniffer();   /* dll load on Windows */
130 #endif
131     ssl_Trace("./tracefile.txt", err);
132
133     if (argc == 1) {
134         /* normal case, user chooses device and port */
135
136             if (pcap_findalldevs(&alldevs, err) == -1)
137                     err_sys("Error in pcap_findalldevs");
138
139             for (d = alldevs; d; d=d->next) {
140                     printf("%d. %s", ++i, d->name);
141                     if (d->description)
142                             printf(" (%s)\n", d->description);
143                     else
144                             printf(" (No description available)\n");
145             }
146
147             if (i == 0)
148                     err_sys("No interfaces found! Make sure pcap or WinPcap is"
149                     " installed correctly and you have sufficient permissions");
150
151             printf("Enter the interface number (1-%d): ", i);
152             scanf("%d", &inum);
153
154             if (inum < 1 || inum > i)
155                     err_sys("Interface number out of range");
156
157             /* Jump to the selected adapter */
158             for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
159
160             pcap = pcap_create(d->name, err);
161
162         if (pcap == NULL) printf("pcap_create failed %s\n", err);
163
164             /* get an IPv4 address */
165             for (a = d->addresses; a; a = a->next) {
166                     switch(a->addr->sa_family)
167                     {
168                             case AF_INET:
169                                     server = 
170                         iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr);
171                                     printf("server = %s\n", server);
172                                     break;
173
174                 default:
175                     break;
176                     }
177             }
178             if (server == NULL)
179                     err_sys("Unable to get device IPv4 address");
180
181         ret = pcap_set_snaplen(pcap, 65536);
182         if (ret != 0) printf("pcap_set_snaplen failed %s\n", pcap_geterr(pcap));
183
184         ret = pcap_set_timeout(pcap, 1000); 
185         if (ret != 0) printf("pcap_set_timeout failed %s\n", pcap_geterr(pcap));
186
187         ret = pcap_set_buffer_size(pcap, 1000000); 
188         if (ret != 0)
189                     printf("pcap_set_buffer_size failed %s\n", pcap_geterr(pcap));
190
191         ret = pcap_set_promisc(pcap, 1); 
192         if (ret != 0) printf("pcap_set_promisc failed %s\n", pcap_geterr(pcap));
193
194
195         ret = pcap_activate(pcap);
196         if (ret != 0) printf("pcap_activate failed %s\n", pcap_geterr(pcap));
197
198             printf("Enter the port to scan: ");
199             scanf("%d", &port);
200
201             SNPRINTF(filter, sizeof(filter), "tcp and port %d", port);
202
203             ret = pcap_compile(pcap, &fp, filter, 0, 0);
204         if (ret != 0) printf("pcap_compile failed %s\n", pcap_geterr(pcap));
205
206         ret = pcap_setfilter(pcap, &fp);
207         if (ret != 0) printf("pcap_setfilter failed %s\n", pcap_geterr(pcap));
208
209         ret = ssl_SetPrivateKey(server, port, "../../certs/server-key.pem",
210                                FILETYPE_PEM, NULL, err);
211     }
212     else if (argc >= 3) {
213         saveFile = 1;
214         pcap = pcap_open_offline(argv[1], err);
215         if (pcap == NULL) {
216             printf("pcap_open_offline failed %s\n", err);
217             ret = -1;
218         }
219         else {
220             /* defaults for server and port */
221             port = 443;
222             server = "127.0.0.1";
223
224             if (argc >= 4)
225                 server = argv[3];
226
227             if (argc >= 5)
228                 port = atoi(argv[4]);
229
230             ret = ssl_SetPrivateKey(server, port, argv[2],
231                                     FILETYPE_PEM, NULL, err);
232         }
233     }
234     else {
235         /* usage error */
236         printf(
237              "usage: ./snifftest or ./snifftest dump pemKey [server] [port]\n");
238         exit(EXIT_FAILURE);
239     }
240
241     if (ret != 0)
242         err_sys(err);
243
244     if (pcap_datalink(pcap) == DLT_NULL) 
245         frame = NULL_IF_FRAME_LEN;
246
247     while (1) {
248         struct pcap_pkthdr header;
249         const unsigned char* packet = pcap_next(pcap, &header);
250         if (packet) {
251
252             byte data[65535];
253
254             if (header.caplen > 40)  { /* min ip(20) + min tcp(20) */
255                                 packet        += frame;
256                                 header.caplen -= frame;                                 
257             }
258             else
259                 continue;
260
261             ret = ssl_DecodePacket(packet, header.caplen, data, err);
262             if (ret < 0)
263                 printf("ssl_Decode ret = %d, %s\n", ret, err);
264             if (ret > 0) {
265                 data[ret] = 0;
266                                 printf("SSL App Data:%s\n", data);
267             }
268         }
269         else if (saveFile)
270             break;      /* we're done reading file */
271     }
272
273     return EXIT_SUCCESS;
274 }
275
276 #endif /* full build */