xref: /haiku/src/tests/kits/net/wlan_test.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 				wireless_network network;
112 				uint32 cookie = 0;
113 				while (device.GetNextNetwork(cookie, network) == B_OK)
114 					show(network);
115 			}
116 		} else
117 			usage();
118 	} else {
119 		// show associated networks
120 		wireless_network network;
121 		uint32 cookie = 0;
122 		while (device.GetNextAssociatedNetwork(cookie, network) == B_OK) {
123 			show(network);
124 		}
125 		if (cookie == 0)
126 			puts("no associated networks found.");
127 	}
128 
129 	return 0;
130 }
131