]>
git.ipfire.org Git - thirdparty/hostap.git/blob - wlantest/monitor.c
2 * Linux packet socket monitor
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include <netpacket/packet.h>
13 #include "utils/common.h"
14 #include "utils/eloop.h"
18 static void monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
20 struct wlantest
*wt
= eloop_ctx
;
24 len
= recv(sock
, buf
, sizeof(buf
), 0);
26 wpa_printf(MSG_INFO
, "recv(PACKET): %s", strerror(errno
));
31 os_free(wt
->decrypted
);
33 write_pcap_captured(wt
, buf
, len
);
34 wlantest_process(wt
, buf
, len
);
35 write_pcapng_captured(wt
, buf
, len
);
39 static void monitor_read_wired(int sock
, void *eloop_ctx
, void *sock_ctx
)
41 struct wlantest
*wt
= eloop_ctx
;
45 len
= recv(sock
, buf
, sizeof(buf
), 0);
47 wpa_printf(MSG_INFO
, "recv(PACKET): %s", strerror(errno
));
51 wlantest_process_wired(wt
, buf
, len
);
55 int monitor_init(struct wlantest
*wt
, const char *ifname
)
57 struct sockaddr_ll ll
;
59 os_memset(&ll
, 0, sizeof(ll
));
60 ll
.sll_family
= AF_PACKET
;
61 ll
.sll_ifindex
= if_nametoindex(ifname
);
62 if (ll
.sll_ifindex
== 0) {
63 wpa_printf(MSG_ERROR
, "Monitor interface '%s' does not exist",
68 wt
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
69 if (wt
->monitor_sock
< 0) {
70 wpa_printf(MSG_ERROR
, "socket(PF_PACKET,SOCK_RAW): %s",
75 if (bind(wt
->monitor_sock
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
76 wpa_printf(MSG_ERROR
, "bind(PACKET): %s", strerror(errno
));
77 close(wt
->monitor_sock
);
78 wt
->monitor_sock
= -1;
82 if (eloop_register_read_sock(wt
->monitor_sock
, monitor_read
, wt
, NULL
))
84 wpa_printf(MSG_ERROR
, "Could not register monitor read "
86 close(wt
->monitor_sock
);
87 wt
->monitor_sock
= -1;
95 int monitor_init_wired(struct wlantest
*wt
, const char *ifname
)
97 struct sockaddr_ll ll
;
99 os_memset(&ll
, 0, sizeof(ll
));
100 ll
.sll_family
= AF_PACKET
;
101 ll
.sll_ifindex
= if_nametoindex(ifname
);
102 if (ll
.sll_ifindex
== 0) {
103 wpa_printf(MSG_ERROR
, "Monitor interface '%s' does not exist",
108 wt
->monitor_wired
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
109 if (wt
->monitor_wired
< 0) {
110 wpa_printf(MSG_ERROR
, "socket(PF_PACKET,SOCK_RAW): %s",
115 if (bind(wt
->monitor_wired
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
116 wpa_printf(MSG_ERROR
, "bind(PACKET): %s", strerror(errno
));
117 close(wt
->monitor_wired
);
118 wt
->monitor_wired
= -1;
122 if (eloop_register_read_sock(wt
->monitor_wired
, monitor_read_wired
,
124 wpa_printf(MSG_ERROR
, "Could not register monitor read "
126 close(wt
->monitor_wired
);
127 wt
->monitor_wired
= -1;
135 void monitor_deinit(struct wlantest
*wt
)
137 if (wt
->monitor_sock
>= 0) {
138 eloop_unregister_read_sock(wt
->monitor_sock
);
139 close(wt
->monitor_sock
);
140 wt
->monitor_sock
= -1;
143 if (wt
->monitor_wired
>= 0) {
144 eloop_unregister_read_sock(wt
->monitor_wired
);
145 close(wt
->monitor_wired
);
146 wt
->monitor_wired
= -1;