summaryrefslogtreecommitdiff
path: root/jni/src/pppoe_status.c (plain)
blob: 40615d672ea9ddd22dc99f48e0ee605d815fe773
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
25static 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
50done:
51 close(s);
52 return ret;
53}
54
55#define PPP_IF_NAME "ppp0"
56
57int get_net_updown(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 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "No such device(%s)\n", phy_if_name);
65 return 0;
66 }
67
68 if(0 == ret) {
69 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "%s is DOWN\n", phy_if_name);
70 return 0;
71 }
72 else {
73 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "%s is UP\n", phy_if_name);
74 return 1;
75 }
76}
77
78int get_pppoe_status(const char *phy_if_name)
79{
80 int ret;
81
82 ret = if_is_up(phy_if_name);
83 if (ret < 0) {
84 if (ENODEV == -ret)
85 PRINTF("No such device(%s)\n", phy_if_name);
86
87 PRINTF("ppp_status: DISCONNECTED\n");
88 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,
89 "ppp_status: DISCONNECTED\n");
90
91 return PPP_STATUS_DISCONNECTED;
92 }
93
94 if (0 == ret) {
95 PRINTF("%s is DOWN\n", phy_if_name);
96
97 PRINTF("ppp_status: DISCONNECTED\n");
98 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,
99 "ppp_status: DISCONNECTED\n");
100 return PPP_STATUS_DISCONNECTED;
101 }
102
103 ret = if_is_up(PPP_IF_NAME);
104 if (ret < 0) {
105 if (ENODEV == -ret)
106 PRINTF("No such device(%s)\n", PPP_IF_NAME);
107
108 PRINTF("ppp_status: DISCONNECTED\n");
109 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,
110 "ppp_status: DISCONNECTED\n");
111 return PPP_STATUS_DISCONNECTED;
112 }
113
114 if (0 == ret) {
115 PRINTF("ppp_status: CONNECTING\n");
116
117 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,
118 "ppp_status: CONNECTING\n");
119 return PPP_STATUS_CONNECTING;
120 }
121 else {
122 PRINTF("ppp_status: CONNECTED\n");
123 __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,
124 "ppp_status: CONNECTED\n");
125 return PPP_STATUS_CONNECTED;
126 }
127}
128
129/*
130int
131main(int argc, char **argv)
132{
133 get_pppoe_status(( 1 == argc ) ? "eth0" : argv[1]);
134
135 return 0;
136}
137*/
138