Revision 731d5856
b/net/dump.c | ||
---|---|---|
28 | 28 |
#include "qemu-log.h" |
29 | 29 |
|
30 | 30 |
typedef struct DumpState { |
31 |
VLANClientState *pcap_vc;
|
|
31 |
VLANClientState nc;
|
|
32 | 32 |
int fd; |
33 | 33 |
int pcap_caplen; |
34 | 34 |
} DumpState; |
... | ... | |
54 | 54 |
uint32_t len; |
55 | 55 |
}; |
56 | 56 |
|
57 |
static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
|
|
57 |
static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
|
|
58 | 58 |
{ |
59 |
DumpState *s = vc->opaque;
|
|
59 |
DumpState *s = DO_UPCAST(DumpState, nc, nc);
|
|
60 | 60 |
struct pcap_sf_pkthdr hdr; |
61 | 61 |
int64_t ts; |
62 | 62 |
int caplen; |
... | ... | |
83 | 83 |
return size; |
84 | 84 |
} |
85 | 85 |
|
86 |
static void net_dump_cleanup(VLANClientState *vc)
|
|
86 |
static void dump_cleanup(VLANClientState *nc)
|
|
87 | 87 |
{ |
88 |
DumpState *s = vc->opaque;
|
|
88 |
DumpState *s = DO_UPCAST(DumpState, nc, nc);
|
|
89 | 89 |
|
90 | 90 |
close(s->fd); |
91 |
qemu_free(s); |
|
92 | 91 |
} |
93 | 92 |
|
93 |
static NetClientInfo net_dump_info = { |
|
94 |
.type = NET_CLIENT_TYPE_DUMP, |
|
95 |
.size = sizeof(DumpState), |
|
96 |
.receive = dump_receive, |
|
97 |
.cleanup = dump_cleanup, |
|
98 |
}; |
|
99 |
|
|
94 | 100 |
static int net_dump_init(VLANState *vlan, const char *device, |
95 | 101 |
const char *name, const char *filename, int len) |
96 | 102 |
{ |
97 | 103 |
struct pcap_file_hdr hdr; |
104 |
VLANClientState *nc; |
|
98 | 105 |
DumpState *s; |
106 |
int fd; |
|
99 | 107 |
|
100 |
s = qemu_malloc(sizeof(DumpState)); |
|
101 |
|
|
102 |
s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
|
103 |
if (s->fd < 0) { |
|
108 |
fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
|
109 |
if (fd < 0) { |
|
104 | 110 |
qemu_error("-net dump: can't open %s\n", filename); |
105 | 111 |
return -1; |
106 | 112 |
} |
107 | 113 |
|
108 |
s->pcap_caplen = len; |
|
109 |
|
|
110 | 114 |
hdr.magic = PCAP_MAGIC; |
111 | 115 |
hdr.version_major = 2; |
112 | 116 |
hdr.version_minor = 4; |
113 | 117 |
hdr.thiszone = 0; |
114 | 118 |
hdr.sigfigs = 0; |
115 |
hdr.snaplen = s->pcap_caplen;
|
|
119 |
hdr.snaplen = len; |
|
116 | 120 |
hdr.linktype = 1; |
117 | 121 |
|
118 |
if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
|
|
122 |
if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
|
119 | 123 |
qemu_error("-net dump write error: %s\n", strerror(errno)); |
120 |
close(s->fd); |
|
121 |
qemu_free(s); |
|
124 |
close(fd); |
|
122 | 125 |
return -1; |
123 | 126 |
} |
124 | 127 |
|
125 |
s->pcap_vc = qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP, |
|
126 |
vlan, NULL, device, name, NULL, |
|
127 |
dump_receive, NULL, NULL, |
|
128 |
net_dump_cleanup, s); |
|
129 |
snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), |
|
128 |
nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name); |
|
129 |
|
|
130 |
snprintf(nc->info_str, sizeof(nc->info_str), |
|
130 | 131 |
"dump to %s (len=%d)", filename, len); |
132 |
|
|
133 |
s = DO_UPCAST(DumpState, nc, nc); |
|
134 |
|
|
135 |
s->fd = fd; |
|
136 |
s->pcap_caplen = len; |
|
137 |
|
|
131 | 138 |
return 0; |
132 | 139 |
} |
133 | 140 |
|
Also available in: Unified diff