xref: /haiku/src/tests/kits/net/wlan_test.cpp (revision 445d4fd926c569e7b9ae28017da86280aaecbae2)
1 /*
2  * Copyright 2010, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include <NetworkDevice.h>
11 
12 
13 extern const char* __progname;
14 
15 
16 void
17 usage()
18 {
19 	fprintf(stderr, "%s: <device> join|leave|list [<network> [<password>]]\n",
20 		__progname);
21 	exit(1);
22 }
23 
24 
25 const char*
26 get_authentication_mode(uint32 mode)
27 {
28 	switch (mode) {
29 		default:
30 		case B_NETWORK_AUTHENTICATION_NONE:
31 			return "-";
32 		case B_NETWORK_AUTHENTICATION_WEP:
33 			return "WEP";
34 		case B_NETWORK_AUTHENTICATION_WPA:
35 			return "WPA";
36 		case B_NETWORK_AUTHENTICATION_WPA2:
37 			return "WPA2";
38 	}
39 }
40 
41 
42 void
43 show(const wireless_network& network)
44 {
45 	printf("%-32s  %s  %4g dB %s C%02" B_PRIx32 " K%02" B_PRIx32
46 		" %s\n", network.name, network.address.ToString().String(),
47 		network.signal_strength / 2.0, get_authentication_mode(
48 			network.authentication_mode), network.cipher, network.key_mode,
49 		(network.flags & B_NETWORK_IS_ENCRYPTED) != 0 ? " (encrypted)" : "");
50 }
51 
52 
53 int
54 main(int argc, char** argv)
55 {
56 	if (argc < 2)
57 		usage();
58 
59 	BNetworkDevice device(argv[1]);
60 	if (!device.Exists()) {
61 		fprintf(stderr, "\"%s\" does not exist!\n", argv[1]);
62 		return 1;
63 	}
64 	if (!device.IsWireless()) {
65 		fprintf(stderr, "\"%s\" is not a WLAN device!\n", argv[1]);
66 		return 1;
67 	}
68 
69 	if (argc > 2) {
70 		if (!strcmp(argv[2], "join")) {
71 			if (argc < 4)
72 				usage();
73 			const char* password = NULL;
74 			if (argc == 5)
75 				password = argv[4];
76 
77 			status_t status = device.JoinNetwork(argv[3], password);
78 			if (status != B_OK) {
79 				fprintf(stderr, "joining network failed: %s\n",
80 					strerror(status));
81 				return 1;
82 			}
83 		} else if (!strcmp(argv[2], "leave")) {
84 			if (argc < 4)
85 				usage();
86 
87 			status_t status = device.LeaveNetwork(argv[3]);
88 			if (status != B_OK) {
89 				fprintf(stderr, "leaving network failed: %s\n",
90 					strerror(status));
91 				return 1;
92 			}
93 		} else if (!strcmp(argv[2], "list")) {
94 			if (argc == 4) {
95 				// list the named entry
96 				wireless_network network;
97 				BNetworkAddress link;
98 				status_t status = link.SetTo(AF_LINK, argv[3]);
99 				if (status == B_OK)
100 					status = device.GetNetwork(link, network);
101 				else
102 					status = device.GetNetwork(argv[3], network);
103 				if (status != B_OK) {
104 					fprintf(stderr, "getting network failed: %s\n",
105 						strerror(status));
106 					return 1;
107 				}
108 				show(network);
109 			} else {
110 				// list all
111 				uint32 networksCount = 0;
112 				wireless_network* networks = NULL;
113 				device.GetNetworks(networks, networksCount);
114 				for (uint32 i = 0; i < networksCount; i++)
115 					show(networks[i]);
116 				delete[] networks;
117 			}
118 		} else
119 			usage();
120 	} else {
121 		// show associated networks
122 		wireless_network network;
123 		uint32 cookie = 0;
124 		while (device.GetNextAssociatedNetwork(cookie, network) == B_OK) {
125 			show(network);
126 		}
127 		if (cookie == 0)
128 			puts("no associated networks found.");
129 	}
130 
131 	return 0;
132 }
133