1/*
2 * Copyright 2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _NETWORK_DEVICE_H
6#define _NETWORK_DEVICE_H
7
8
9#include <net/if.h>
10#include <string.h>
11
12
13// authentication modes
14enum {
15	B_NETWORK_AUTHENTICATION_NONE		= 0,
16	B_NETWORK_AUTHENTICATION_WEP		= 1,
17	B_NETWORK_AUTHENTICATION_WPA		= 2,
18	B_NETWORK_AUTHENTICATION_WPA2		= 3,
19	B_NETWORK_AUTHENTICATION_EAP		= 4
20};
21
22// cipher algorithms
23enum {
24	B_NETWORK_CIPHER_NONE				= 0x01,
25	B_NETWORK_CIPHER_WEP_40				= 0x02,
26	B_NETWORK_CIPHER_WEP_104			= 0x04,
27	B_NETWORK_CIPHER_TKIP				= 0x08,
28	B_NETWORK_CIPHER_CCMP				= 0x10,
29	B_NETWORK_CIPHER_AES_128_CMAC		= 0x20
30};
31
32// key modes
33enum {
34	B_KEY_MODE_IEEE802_1X				= 0x0001,
35	B_KEY_MODE_PSK						= 0x0002,
36	B_KEY_MODE_NONE						= 0x0004,
37	B_KEY_MODE_FT_IEEE802_1X			= 0x0020,
38	B_KEY_MODE_FT_PSK					= 0x0040,
39	B_KEY_MODE_IEEE802_1X_SHA256		= 0x0080,
40	B_KEY_MODE_PSK_SHA256				= 0x0100,
41	B_KEY_MODE_WPS						= 0x0200
42};
43
44// eap encapsulation method (IEEE 802.1x)
45enum {
46	B_NETWORK_EAP_ENCAPSULATION_NONE	= 0x0000,
47	B_NETWORK_EAP_ENCAPSULATION_PEAP	= 0x0001,
48	B_NETWORK_EAP_ENCAPSULATION_TLS		= 0x0002
49};
50
51
52#if defined(__cplusplus) && !defined(_KERNEL_MODE)
53
54#include <NetworkAddress.h>
55
56
57struct wireless_network {
58	char				name[32];
59	BNetworkAddress		address;
60	uint8				noise_level;
61	uint8				signal_strength;
62	uint32				flags;
63	uint32				authentication_mode;
64	uint32				cipher;
65	uint32				group_cipher;
66	uint32				key_mode;
67
68	bool operator==(const wireless_network& other) {
69		return strncmp(name, other.name, 32) == 0
70			// ignore address difference
71			&& noise_level == other.noise_level
72			&& signal_strength == other.signal_strength
73			&& flags == other.flags
74			&& authentication_mode == other.authentication_mode
75			&& cipher == other.cipher
76			&& group_cipher == other.group_cipher
77			&& key_mode == other.key_mode;
78	}
79};
80
81// flags
82#define B_NETWORK_IS_ENCRYPTED			0x01
83#define B_NETWORK_IS_PERSISTENT			0x02
84
85
86class BNetworkDevice {
87public:
88								BNetworkDevice();
89								BNetworkDevice(const char* name);
90								~BNetworkDevice();
91
92			void				Unset();
93			void				SetTo(const char* name);
94
95			const char*			Name() const;
96			bool				Exists() const;
97			uint32				Index() const;
98
99			uint32				Flags() const;
100			bool				HasLink() const;
101
102			int32				Media() const;
103			status_t			SetMedia(int32 media);
104
105			status_t			GetHardwareAddress(BNetworkAddress& address);
106
107			bool				IsEthernet();
108			bool				IsWireless();
109
110			status_t			Control(int option, void* request);
111
112			status_t			Scan(bool wait = true,
113									bool forceRescan = true);
114
115			status_t			GetNetworks(wireless_network*& networks,
116									uint32& count);
117			status_t			GetNetwork(const char* name,
118									wireless_network& network);
119			status_t			GetNetwork(const BNetworkAddress& address,
120									wireless_network& network);
121
122			status_t			JoinNetwork(const char* name,
123									const char* password = NULL);
124			status_t			JoinNetwork(const wireless_network& network,
125									const char* password = NULL);
126			status_t			JoinNetwork(const BNetworkAddress& address,
127									const char* password = NULL);
128
129			status_t			LeaveNetwork(const char* name);
130			status_t			LeaveNetwork(const wireless_network& network);
131			status_t			LeaveNetwork(const BNetworkAddress& address);
132
133			status_t			GetNextAssociatedNetwork(uint32& cookie,
134									wireless_network& network);
135			status_t			GetNextAssociatedNetwork(uint32& cookie,
136									BNetworkAddress& address);
137
138private:
139			char				fName[IF_NAMESIZE];
140};
141
142#endif	// __cplusplus && !_KERNEL_MODE
143
144#endif	// _NETWORK_DEVICE_H
145