blob: f54292133c7b6c981fa82788f9e9b59fd867b7d9
1 | #include <stdlib.h> |
2 | #include <stdio.h> |
3 | #include <stdarg.h> |
4 | #include <string.h> |
5 | |
6 | #include <sys/types.h> |
7 | #include <errno.h> |
8 | |
9 | #include <unistd.h> |
10 | |
11 | #include <sys/socket.h> |
12 | #include <netinet/in.h> |
13 | #include <linux/if.h> |
14 | #include <linux/sockios.h> |
15 | |
16 | #include <sys/un.h> |
17 | #include <android/log.h> |
18 | #include "pppoe_status.h" |
19 | |
20 | |
21 | #define LOCAL_TAG "PPPOE_STATUS" |
22 | //#define PRINTF printf |
23 | #define PRINTF |
24 | |
25 | static int if_is_up(const char *if_name) |
26 | { |
27 | struct ifreq ifr; |
28 | int s, ret; |
29 | |
30 | strlcpy(ifr.ifr_name, if_name, IFNAMSIZ); |
31 | |
32 | s = socket(AF_INET, SOCK_DGRAM, 0); |
33 | if (s < 0) { |
34 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"if_is_up(%s): failed to create socket(%s)\n", if_name, strerror(errno)); |
35 | |
36 | return -1; |
37 | } |
38 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); |
39 | if (ret < 0) { |
40 | ret = -errno; |
41 | //perror(ifr.ifr_name); |
42 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"if_is_up(%s): failed to ioctl(%s)\n", if_name, strerror(errno)); |
43 | goto done; |
44 | } |
45 | |
46 | ret = (ifr.ifr_flags &= IFF_UP) ? 1 : 0; |
47 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
48 | "if_is_up(%s) is %s\n", if_name, ret ? "UP" : "DOWN"); |
49 | |
50 | done: |
51 | close(s); |
52 | return ret; |
53 | } |
54 | |
55 | #define PPP_IF_NAME "ppp0" |
56 | |
57 | int get_pppoe_status( const char *phy_if_name) |
58 | { |
59 | int ret; |
60 | |
61 | ret = if_is_up(phy_if_name); |
62 | if (ret < 0){ |
63 | if ( ENODEV == -ret ) |
64 | PRINTF( "No such device(%s)\n", phy_if_name ); |
65 | |
66 | PRINTF("ppp_status: DISCONNECTED\n"); |
67 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
68 | "ppp_status: DISCONNECTED\n"); |
69 | |
70 | return PPP_STATUS_DISCONNECTED; |
71 | } |
72 | |
73 | if (0 == ret ) { |
74 | PRINTF( "%s is DOWN\n", phy_if_name ); |
75 | |
76 | PRINTF("ppp_status: DISCONNECTED\n"); |
77 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
78 | "ppp_status: DISCONNECTED\n"); |
79 | return PPP_STATUS_DISCONNECTED; |
80 | } |
81 | |
82 | ret = if_is_up(PPP_IF_NAME); |
83 | if (ret < 0){ |
84 | if ( ENODEV == -ret ) |
85 | PRINTF( "No such device(%s)\n", PPP_IF_NAME ); |
86 | |
87 | PRINTF("ppp_status: DISCONNECTED\n"); |
88 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
89 | "ppp_status: DISCONNECTED\n"); |
90 | return PPP_STATUS_DISCONNECTED; |
91 | } |
92 | |
93 | if (0 == ret ) { |
94 | PRINTF("ppp_status: CONNECTING\n"); |
95 | |
96 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
97 | "ppp_status: CONNECTING\n"); |
98 | return PPP_STATUS_CONNECTING; |
99 | } |
100 | else { |
101 | PRINTF("ppp_status: CONNECTED\n"); |
102 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
103 | "ppp_status: CONNECTED\n"); |
104 | return PPP_STATUS_CONNECTED; |
105 | } |
106 | } |
107 | |
108 | /* |
109 | int |
110 | main(int argc, char **argv) |
111 | { |
112 | get_pppoe_status(( 1 == argc ) ? "eth0" : argv[1]); |
113 | |
114 | return 0; |
115 | } |
116 | */ |
117 |