1 /* $Id: getconnstatus.c,v 1.6 2013/03/23 10:46:54 nanard Exp $ */
2 /* MiniUPnP project
3  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4  * (c) 2011-2013 Thomas Bernard
5  * This software is subject to the conditions detailed
6  * in the LICENCE file provided within the distribution */
7 
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <netinet/in.h>
11 #include "getconnstatus.h"
12 #include "getifaddr.h"
13 
14 #define STATUS_UNCONFIGURED (0)
15 #define STATUS_CONNECTING (1)
16 #define STATUS_CONNECTED (2)
17 #define STATUS_PENDINGDISCONNECT (3)
18 #define STATUS_DISCONNECTING (4)
19 #define STATUS_DISCONNECTED (5)
20 
21 /**
22  * get the connection status
23  * return values :
24  *  0 - Unconfigured
25  *  1 - Connecting
26  *  2 - Connected
27  *  3 - PendingDisconnect
28  *  4 - Disconnecting
29  *  5 - Disconnected */
30 int
get_wan_connection_status(const char * ifname)31 get_wan_connection_status(const char * ifname)
32 {
33 	char addr[INET_ADDRSTRLEN];
34 	int r;
35 
36 	/* we need a better implementation here.
37 	 * I'm afraid it should be device specific */
38 	r = getifaddr(ifname, addr, INET_ADDRSTRLEN, NULL, NULL);
39 	return (r < 0) ? STATUS_DISCONNECTED : STATUS_CONNECTED;
40 }
41 
42 /**
43  * return the same value as get_wan_connection_status()
44  * as a C string */
45 const char *
get_wan_connection_status_str(const char * ifname)46 get_wan_connection_status_str(const char * ifname)
47 {
48 	int status;
49 	const char * str = NULL;
50 
51 	status = get_wan_connection_status(ifname);
52 	switch(status) {
53 	case 0:
54 		str = "Unconfigured";
55 		break;
56 	case 1:
57 		str = "Connecting";
58 		break;
59 	case 2:
60 		str = "Connected";
61 		break;
62 	case 3:
63 		str = "PendingDisconnect";
64 		break;
65 	case 4:
66 		str = "Disconnecting";
67 		break;
68 	case 5:
69 		str = "Disconnected";
70 		break;
71 	}
72 	return str;
73 }
74 
75