wifi: Add support for autoscan request
[framework/connectivity/connman.git] / tools / addr-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <arpa/inet.h>
28
29 static unsigned char netmask2prefixlen(const char *netmask)
30 {
31         unsigned char bits = 0;
32         in_addr_t mask = inet_network(netmask);
33         in_addr_t host = ~mask;
34
35         /* a valid netmask must be 2^n - 1 */
36         if ((host & (host + 1)) != 0)
37                 return -1;
38
39         for (; mask; mask <<= 1)
40                 ++bits;
41
42         return bits;
43 }
44
45 int main(int argc, char *argv[])
46 {
47         const char *masks[] = { "255.255.255.255", "255.255.255.0",
48                                                 "255.255.0.0", "255.0.0.0" };
49         struct in_addr addr;
50         int i, len;
51
52         for (i = 0; i < 4; i++) {
53                 printf("subnet %-16s  prefixlen %u\n", masks[i],
54                                                 netmask2prefixlen(masks[i]));
55         }
56
57         printf("\n");
58
59         for (i = 0; i < 4; i++) {
60                 len = (i + 1) * 8;
61                 addr.s_addr = htonl(~(0xfffffffflu >> len));
62                 printf("prefixlen %-2u  netmask %s\n", len, inet_ntoa(addr));
63         }
64
65         return 0;
66 }