1 | /**
|
---|
2 | * pcapif_helper.c - This file is part of lwIP pcapif and provides helper functions
|
---|
3 | * for managing the link state.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include "pcapif_helper.h"
|
---|
7 |
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <stdio.h>
|
---|
10 |
|
---|
11 | #include "lwip/arch.h"
|
---|
12 |
|
---|
13 | #ifdef WIN32
|
---|
14 |
|
---|
15 | #define WIN32_LEAN_AND_MEAN
|
---|
16 |
|
---|
17 | #ifdef _MSC_VER
|
---|
18 | #pragma warning( push, 3 )
|
---|
19 | #endif
|
---|
20 | #include <windows.h>
|
---|
21 | #include <packet32.h>
|
---|
22 | #include <ntddndis.h>
|
---|
23 | #ifdef _MSC_VER
|
---|
24 | #pragma warning ( pop )
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | struct pcapifh_linkstate {
|
---|
28 | LPADAPTER lpAdapter;
|
---|
29 | PPACKET_OID_DATA ppacket_oid_data;
|
---|
30 | };
|
---|
31 |
|
---|
32 | struct pcapifh_linkstate* pcapifh_linkstate_init(char *adapter_name)
|
---|
33 | {
|
---|
34 | struct pcapifh_linkstate* state = (struct pcapifh_linkstate*)malloc(sizeof(struct pcapifh_linkstate));
|
---|
35 | if (state != NULL) {
|
---|
36 | memset(state, 0, sizeof(struct pcapifh_linkstate));
|
---|
37 | state->ppacket_oid_data = (PPACKET_OID_DATA)malloc(sizeof(PACKET_OID_DATA) + sizeof(NDIS_MEDIA_STATE));
|
---|
38 | if (state->ppacket_oid_data == NULL) {
|
---|
39 | free(state);
|
---|
40 | state = NULL;
|
---|
41 | } else {
|
---|
42 | state->lpAdapter = PacketOpenAdapter((char*)adapter_name);
|
---|
43 | if ((state->lpAdapter == NULL) || (state->lpAdapter->hFile == INVALID_HANDLE_VALUE)) {
|
---|
44 | /* failed to open adapter */
|
---|
45 | free(state);
|
---|
46 | state = NULL;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | return state;
|
---|
51 | }
|
---|
52 |
|
---|
53 | enum pcapifh_link_event pcapifh_linkstate_get(struct pcapifh_linkstate* state)
|
---|
54 | {
|
---|
55 | enum pcapifh_link_event ret = PCAPIF_LINKEVENT_UNKNOWN;
|
---|
56 | if (state != NULL) {
|
---|
57 | state->ppacket_oid_data->Oid = OID_GEN_MEDIA_CONNECT_STATUS;
|
---|
58 | state->ppacket_oid_data->Length = sizeof(NDIS_MEDIA_STATE);
|
---|
59 | if (PacketRequest(state->lpAdapter, FALSE, state->ppacket_oid_data)) {
|
---|
60 | NDIS_MEDIA_STATE fNdisMediaState;
|
---|
61 | fNdisMediaState = (*((PNDIS_MEDIA_STATE)(state->ppacket_oid_data->Data)));
|
---|
62 | ret = ((fNdisMediaState == NdisMediaStateConnected) ? PCAPIF_LINKEVENT_UP : PCAPIF_LINKEVENT_DOWN);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return ret;
|
---|
66 | }
|
---|
67 |
|
---|
68 | void pcapifh_linkstate_close(struct pcapifh_linkstate* state)
|
---|
69 | {
|
---|
70 | if (state != NULL) {
|
---|
71 | if (state->lpAdapter != NULL) {
|
---|
72 | PacketCloseAdapter(state->lpAdapter);
|
---|
73 | }
|
---|
74 | if (state->ppacket_oid_data != NULL) {
|
---|
75 | free(state->ppacket_oid_data);
|
---|
76 | }
|
---|
77 | free(state);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | #else /* WIN32 */
|
---|
82 |
|
---|
83 | /* @todo: add linux/unix implementation? */
|
---|
84 |
|
---|
85 | struct pcapifh_linkstate {
|
---|
86 | u8_t empty;
|
---|
87 | };
|
---|
88 |
|
---|
89 | struct pcapifh_linkstate* pcapifh_linkstate_init(char *adapter_name)
|
---|
90 | {
|
---|
91 | LWIP_UNUSED_ARG(adapter_name);
|
---|
92 | return NULL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | enum pcapifh_link_event pcapifh_linkstate_get(struct pcapifh_linkstate* state)
|
---|
96 | {
|
---|
97 | LWIP_UNUSED_ARG(state);
|
---|
98 | return PCAPIF_LINKEVENT_UP;
|
---|
99 | }
|
---|
100 | void pcapifh_linkstate_close(struct pcapifh_linkstate* state)
|
---|
101 | {
|
---|
102 | LWIP_UNUSED_ARG(state);
|
---|
103 | }
|
---|
104 |
|
---|
105 | #endif /* WIN32 */
|
---|