3 /* $KAME: dump.c,v 1.3 2000/09/23 15:31:05 itojun Exp $ */
6 * Copyright (C) 2000 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/ip6.h>
45 #include <netinet/udp.h>
51 #if TIME_WITH_SYS_TIME
52 # include <sys/time.h>
56 # include <sys/time.h>
70 /* copied from pcap-int.h */
72 u_int32_t tv_sec
; /* seconds */
73 u_int32_t tv_usec
; /* microseconds */
76 struct pcap_sf_pkthdr
{
77 struct pcap_timeval ts
; /* time stamp */
78 u_int32_t caplen
; /* length of portion present */
79 u_int32_t len
; /* length this packet (off wire) */
82 #define TCPDUMP_MAGIC 0xa1b2c3d4
87 isakmp_dump_open(path
)
90 struct pcap_file_header hdr
;
97 fd
= open(path
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600);
101 memset(&hdr
, 0, sizeof(hdr
));
102 hdr
.magic
= TCPDUMP_MAGIC
;
103 hdr
.version_major
= PCAP_VERSION_MAJOR
;
104 hdr
.version_minor
= PCAP_VERSION_MINOR
;
107 hdr
.snaplen
= 60000; /* should be enough */
109 hdr
.linktype
= DLT_NULL
;
111 if (write(fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
126 isakmp_dump(msg
, from
, my
)
128 struct sockaddr
*from
;
136 int32_t af
; /*llhdr for DLT_NULL*/
137 struct pcap_sf_pkthdr sf_hdr
;
142 if (from
->sa_family
== my
->sa_family
)
143 af
= from
->sa_family
;
147 af
= from
->sa_family
;
151 af
= AF_INET
; /*assume it*/
162 memset(&sf_hdr
, 0, sizeof(sf_hdr
));
163 gettimeofday(&tv
, NULL
);
164 sf_hdr
.ts
.tv_sec
= tv
.tv_sec
;
165 sf_hdr
.ts
.tv_usec
= tv
.tv_usec
;
167 /* write out timestamp and llhdr */
168 switch (af
== AF_INET
) {
170 sf_hdr
.caplen
= sf_hdr
.len
= sizeof(ip
);
173 sf_hdr
.caplen
= sf_hdr
.len
= sizeof(ip6
);
176 sf_hdr
.caplen
+= sizeof(af
) + sizeof(uh
) + msg
->l
;
177 sf_hdr
.len
+= sizeof(af
) + sizeof(uh
) + msg
->l
;
178 if (write(fd
, &sf_hdr
, sizeof(sf_hdr
)) < sizeof(sf_hdr
))
180 if (write(fd
, &af
, sizeof(af
)) < sizeof(af
))
183 /* write out llhdr and ip header */
185 memset(&ip
, 0, sizeof(ip
));
187 ip
.ip_hl
= sizeof(ip
) >> 2;
189 ip
.ip_src
= ((struct sockaddr_in
*)from
)->sin_addr
;
191 ip
.ip_dst
= ((struct sockaddr_in
*)my
)->sin_addr
;
192 ip
.ip_p
= IPPROTO_UDP
;
194 ip
.ip_len
= htons(sizeof(ip
) + sizeof(uh
) + msg
->l
);
195 if (write(fd
, &ip
, sizeof(ip
)) < sizeof(ip
))
197 } else if (af
== AF_INET6
) {
198 memset(&ip6
, 0, sizeof(ip6
));
199 ip6
.ip6_vfc
= IPV6_VERSION
;
201 ip6
.ip6_src
= ((struct sockaddr_in6
*)from
)->sin6_addr
;
203 ip6
.ip6_dst
= ((struct sockaddr_in6
*)my
)->sin6_addr
;
204 ip6
.ip6_nxt
= IPPROTO_UDP
;
205 ip6
.ip6_plen
= htons(sizeof(uh
) + msg
->l
);
207 if (write(fd
, &ip6
, sizeof(ip6
)) < sizeof(ip6
))
211 /* write out udp header */
212 memset(&uh
, 0, sizeof(uh
));
213 uh
.uh_sport
= htons(500);
214 uh
.uh_dport
= htons(500);
215 uh
.uh_ulen
= htons(msg
->l
& 0xffff);
216 uh
.uh_sum
= htons(0x0000); /*no checksum - invalid for IPv6*/
217 if (write(fd
, &uh
, sizeof(uh
)) < sizeof(uh
))
220 /* write out payload */
221 if (write(fd
, msg
->v
, msg
->l
) != msg
->l
)