]>
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
));
30 write_pcap_captured(wt
, buf
, len
);
31 wlantest_process(wt
, buf
, len
);
35 static void monitor_read_wired(int sock
, void *eloop_ctx
, void *sock_ctx
)
37 struct wlantest
*wt
= eloop_ctx
;
41 len
= recv(sock
, buf
, sizeof(buf
), 0);
43 wpa_printf(MSG_INFO
, "recv(PACKET): %s", strerror(errno
));
47 wlantest_process_wired(wt
, buf
, len
);
51 int monitor_init(struct wlantest
*wt
, const char *ifname
)
53 struct sockaddr_ll ll
;
55 os_memset(&ll
, 0, sizeof(ll
));
56 ll
.sll_family
= AF_PACKET
;
57 ll
.sll_ifindex
= if_nametoindex(ifname
);
58 if (ll
.sll_ifindex
== 0) {
59 wpa_printf(MSG_ERROR
, "Monitor interface '%s' does not exist",
64 wt
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
65 if (wt
->monitor_sock
< 0) {
66 wpa_printf(MSG_ERROR
, "socket(PF_PACKET,SOCK_RAW): %s",
71 if (bind(wt
->monitor_sock
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
72 wpa_printf(MSG_ERROR
, "bind(PACKET): %s", strerror(errno
));
73 close(wt
->monitor_sock
);
74 wt
->monitor_sock
= -1;
78 if (eloop_register_read_sock(wt
->monitor_sock
, monitor_read
, wt
, NULL
))
80 wpa_printf(MSG_ERROR
, "Could not register monitor read "
82 close(wt
->monitor_sock
);
83 wt
->monitor_sock
= -1;
91 int monitor_init_wired(struct wlantest
*wt
, const char *ifname
)
93 struct sockaddr_ll ll
;
95 os_memset(&ll
, 0, sizeof(ll
));
96 ll
.sll_family
= AF_PACKET
;
97 ll
.sll_ifindex
= if_nametoindex(ifname
);
98 if (ll
.sll_ifindex
== 0) {
99 wpa_printf(MSG_ERROR
, "Monitor interface '%s' does not exist",
104 wt
->monitor_wired
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
105 if (wt
->monitor_wired
< 0) {
106 wpa_printf(MSG_ERROR
, "socket(PF_PACKET,SOCK_RAW): %s",
111 if (bind(wt
->monitor_wired
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
112 wpa_printf(MSG_ERROR
, "bind(PACKET): %s", strerror(errno
));
113 close(wt
->monitor_wired
);
114 wt
->monitor_wired
= -1;
118 if (eloop_register_read_sock(wt
->monitor_wired
, monitor_read_wired
,
120 wpa_printf(MSG_ERROR
, "Could not register monitor read "
122 close(wt
->monitor_wired
);
123 wt
->monitor_wired
= -1;
131 void monitor_deinit(struct wlantest
*wt
)
133 if (wt
->monitor_sock
>= 0) {
134 eloop_unregister_read_sock(wt
->monitor_sock
);
135 close(wt
->monitor_sock
);
136 wt
->monitor_sock
= -1;
139 if (wt
->monitor_wired
>= 0) {
140 eloop_unregister_read_sock(wt
->monitor_wired
);
141 close(wt
->monitor_wired
);
142 wt
->monitor_wired
= -1;