13 printf("do ./configure --enable-sniffer to enable build support\n");
21 /* builds on *nix too, for scanf device and port */
22 #define _CRT_SECURE_NO_WARNINGS
25 #include <pcap/pcap.h> /* pcap stuff */
26 #include <stdio.h> /* printf */
27 #include <stdlib.h> /* EXIT_SUCCESS */
28 #include <signal.h> /* signal */
34 #include <arpa/inet.h>
37 typedef unsigned char byte
;
40 ETHER_IF_FRAME_LEN
= 14, /* ethernet interface frame length */
41 LOCAL_IF_FRAME_LEN
= 4, /* localhost interface frame length */
48 static void sig_handler(const int sig
)
50 printf("SIGINT handled.\n");
53 pcap_freealldevs(alldevs
);
61 void err_sys(const char* msg
)
63 fprintf(stderr
, "%s\n", msg
);
69 #define SNPRINTF _snprintf
71 #define SNPRINTF snprintf
75 char* iptos(unsigned int addr
)
77 static char output
[32];
78 byte
*p
= (byte
*)&addr
;
80 SNPRINTF(output
, sizeof(output
), "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
86 int main(int argc
, char** argv
)
92 char err
[PCAP_ERRBUF_SIZE
];
96 struct bpf_program fp
;
100 signal(SIGINT
, sig_handler
);
105 ssl_Trace("./tracefile.txt", err
);
107 if (pcap_findalldevs(&alldevs
, err
) == -1)
108 err_sys("Error in pcap_findalldevs");
110 for (d
= alldevs
; d
; d
=d
->next
) {
111 printf("%d. %s", ++i
, d
->name
);
113 printf(" (%s)\n", d
->description
);
115 printf(" (No description available)\n");
119 err_sys("No interfaces found! Make sure pcap or WinPcap is installed "
120 "correctly and you have sufficient permissions");
122 printf("Enter the interface number (1-%d): ", i
);
125 if (inum
< 1 || inum
> i
)
126 err_sys("Interface number out of range");
128 /* Jump to the selected adapter */
129 for (d
= alldevs
, i
= 0; i
< inum
- 1; d
= d
->next
, i
++);
131 pcap
= pcap_create(d
->name
, err
);
133 if (pcap
== NULL
) printf("pcap_create failed %s\n", err
);
135 if (d
->flags
& PCAP_IF_LOOPBACK
)
138 /* get an IPv4 address */
139 for (a
= d
->addresses
; a
; a
= a
->next
) {
140 switch(a
->addr
->sa_family
)
143 server
=iptos(((struct sockaddr_in
*)a
->addr
)->sin_addr
.s_addr
);
144 printf("server = %s\n", server
);
149 err_sys("Unable to get device IPv4 address");
151 ret
= pcap_set_snaplen(pcap
, 65536);
152 if (ret
!= 0) printf("pcap_set_snaplen failed %s\n", pcap_geterr(pcap
));
154 ret
= pcap_set_timeout(pcap
, 1000);
155 if (ret
!= 0) printf("pcap_set_timeout failed %s\n", pcap_geterr(pcap
));
157 ret
= pcap_set_buffer_size(pcap
, 1000000);
159 printf("pcap_set_buffer_size failed %s\n", pcap_geterr(pcap
));
161 ret
= pcap_set_promisc(pcap
, 1);
162 if (ret
!= 0) printf("pcap_set_promisc failed %s\n", pcap_geterr(pcap
));
165 ret
= pcap_activate(pcap
);
166 if (ret
!= 0) printf("pcap_activate failed %s\n", pcap_geterr(pcap
));
168 printf("Enter the port to scan: ");
171 SNPRINTF(filter
, sizeof(filter
), "tcp and port %d", port
);
173 ret
= pcap_compile(pcap
, &fp
, filter
, 0, 0);
174 if (ret
!= 0) printf("pcap_compile failed %s\n", pcap_geterr(pcap
));
176 ret
= pcap_setfilter(pcap
, &fp
);
177 if (ret
!= 0) printf("pcap_setfilter failed %s\n", pcap_geterr(pcap
));
179 ret
= ssl_SetPrivateKey(server
, port
, "../../certs/server-key.pem",
180 FILETYPE_PEM
, NULL
, err
);
185 struct pcap_pkthdr header
;
186 const unsigned char* packet
= pcap_next(pcap
, &header
);
191 if (header
.caplen
> 40) { /* min ip(20) + min tcp(20) */
192 int frame
= ETHER_IF_FRAME_LEN
;
194 frame
= LOCAL_IF_FRAME_LEN
;
196 header
.caplen
-= frame
;
201 ret
= ssl_DecodePacket(packet
, header
.caplen
, data
, err
);
203 printf("ssl_Decode ret = %d, %s\n", ret
, err
);
206 printf("SSL App Data:%s\n", data
);
214 #endif /* full build */