Browse code

sipdump: use fflush() to get data stored more realtime

- use inet_pton() for generating the data for pcap file

Daniel-Constantin Mierla authored on 15/07/2020 09:22:09
Showing 2 changed files
... ...
@@ -20,6 +20,9 @@
20 20
  *
21 21
  */
22 22
 
23
+#include <stdio.h>
24
+#include <arpa/inet.h>
25
+
23 26
 #include "../../core/ip_addr.h"
24 27
 #include "../../core/resolve.h"
25 28
 
... ...
@@ -105,6 +108,8 @@ void sipdump_init_pcap(FILE *fs)
105 108
 	LM_DBG("writing the pcap file header\n");
106 109
 	if(fwrite(&v_pcap_header, sizeof(struct pcap_header), 1, fs) != 1) {
107 110
 		LM_ERR("failed to write the pcap file header\n");
111
+	} else {
112
+		fflush(fs);
108 113
 	}
109 114
 }
110 115
 
... ...
@@ -119,7 +124,7 @@ void sipdump_write_pcap(FILE *fs, sipdump_data_t *spd)
119 124
 	};
120 125
 	struct pcap_ipv4_header v_pcap_ipv4_header = {
121 126
 		.ver_ihl = 0x45, /* IPv4 + 20 bytes of header */
122
-		.ip_ttl = 128, /* We always put a TTL of 128 to keep Wireshark less blue */
127
+		.ip_ttl = 128, /* put a TTL of 128 to keep Wireshark less blue */
123 128
 	};
124 129
 	struct pcap_ipv6_header v_pcap_ipv6_header = {
125 130
 		.ip6_ctlun.ip6_un2_vfc = 0x60,
... ...
@@ -127,8 +132,8 @@ void sipdump_write_pcap(FILE *fs, sipdump_data_t *spd)
127 132
 	void *pcap_ip_header;
128 133
 	size_t pcap_ip_header_len;
129 134
 	struct pcap_udp_header v_pcap_udp_header;
130
-	ip_addr_t src_ip;
131
-	ip_addr_t dst_ip;
135
+	struct in_addr ip4addr;
136
+	struct in6_addr ip6addr;
132 137
 
133 138
 	if(fs == NULL || spd == NULL) {
134 139
 		return;
... ...
@@ -141,24 +146,38 @@ void sipdump_write_pcap(FILE *fs, sipdump_data_t *spd)
141 146
 
142 147
 	/* IP header */
143 148
 	if (spd->afid == AF_INET6) {
149
+		LM_DBG("ipv6 = %s -> %s\n", spd->src_ip.s, spd->dst_ip.s);
144 150
 		v_pcap_ethernet_header.type = htons(0x86DD); /* IPv6 packet */
145 151
 		pcap_ip_header = &v_pcap_ipv6_header;
146 152
 		pcap_ip_header_len = sizeof(struct pcap_ipv6_header);
147
-		str2ip6buf(&spd->src_ip, &src_ip);
148
-		memcpy(&v_pcap_ipv6_header.ip6_src, src_ip.u.addr16, src_ip.len);
149
-		str2ip6buf(&spd->dst_ip, &dst_ip);
150
-		memcpy(&v_pcap_ipv6_header.ip6_dst, dst_ip.u.addr16, dst_ip.len);
153
+		if (inet_pton(AF_INET6, spd->src_ip.s, &ip6addr) != 1) {
154
+			LM_ERR("failed to parse IPv6 address %s\n", spd->src_ip.s);
155
+			return;
156
+		}
157
+		memcpy(&v_pcap_ipv6_header.ip6_src, &ip6addr, sizeof(struct in6_addr));
158
+		if (inet_pton(AF_INET, spd->dst_ip.s, &ip6addr) != 1) {
159
+			LM_ERR("failed to parse IPv4 address %s\n", spd->dst_ip.s);
160
+			return;
161
+		}
162
+		memcpy(&v_pcap_ipv6_header.ip6_dst, &ip6addr, sizeof(struct in6_addr));
151 163
 		v_pcap_ipv6_header.ip6_ctlun.ip6_un1.ip6_un1_plen = htons(sizeof(struct pcap_udp_header)
152 164
 					+ spd->data.len);
153 165
 		v_pcap_ipv6_header.ip6_ctlun.ip6_un1.ip6_un1_nxt = IPPROTO_UDP;
154 166
 	} else {
167
+		LM_DBG("ipv4 = %s -> %s\n", spd->src_ip.s, spd->dst_ip.s);
155 168
 		v_pcap_ethernet_header.type = htons(0x0800); /* IPv4 packet */
156 169
 		pcap_ip_header = &v_pcap_ipv4_header;
157 170
 		pcap_ip_header_len = sizeof(struct pcap_ipv4_header);
158
-		str2ipbuf(&spd->src_ip, &src_ip);
159
-		memcpy(&v_pcap_ipv6_header.ip6_src, src_ip.u.addr, src_ip.len);
160
-		str2ipbuf(&spd->dst_ip, &dst_ip);
161
-		memcpy(&v_pcap_ipv6_header.ip6_dst, dst_ip.u.addr, dst_ip.len);
171
+		if (inet_pton(AF_INET, spd->src_ip.s, &ip4addr) != 1) {
172
+			LM_ERR("failed to parse IPv4 address %s\n", spd->src_ip.s);
173
+			return;
174
+		}
175
+		memcpy(&v_pcap_ipv4_header.ip_src, &ip4addr, sizeof(uint32_t));
176
+		if (inet_pton(AF_INET, spd->dst_ip.s, &ip4addr) != 1) {
177
+			LM_ERR("failed to parse IPv4 address %s\n", spd->dst_ip.s);
178
+			return;
179
+		}
180
+		memcpy(&v_pcap_ipv4_header.ip_dst, &ip4addr, sizeof(uint32_t));
162 181
 		v_pcap_ipv4_header.ip_len = htons(sizeof(struct pcap_udp_header)
163 182
 					+ sizeof(struct pcap_ipv4_header) + spd->data.len);
164 183
 		v_pcap_ipv4_header.ip_protocol = IPPROTO_UDP; /* UDP */
... ...
@@ -184,4 +203,5 @@ void sipdump_write_pcap(FILE *fs, sipdump_data_t *spd)
184 203
 	if (fwrite(spd->data.s, spd->data.len, 1, fs) != 1) {
185 204
 		LM_ERR("writing UDP payload to pcap failed: %s\n", strerror(errno));
186 205
 	}
206
+	fflush(fs);
187 207
 }
188 208
\ No newline at end of file
... ...
@@ -395,6 +395,7 @@ void sipdump_timer_exec(unsigned int ticks, void *param)
395 395
 			/* LM_NOTICE("writing: [[%.*s]] (%d)\n", odata.len,
396 396
 					odata.s, odata.len); */
397 397
 			fwrite(odata.s, odata.len, 1, _sipdump_text_file);
398
+			fflush(_sipdump_text_file);
398 399
 		}
399 400
 		if(sipdump_mode & SIPDUMP_MODE_WPCAP) {
400 401
 			if(_sipdump_pcap_file==NULL) {