author | kejun.gao <kejun.gao@amlogic.com> | 2011-11-22 09:15:47 (GMT) |
---|---|---|
committer | kejun.gao <kejun.gao@amlogic.com> | 2011-11-22 09:15:47 (GMT) |
commit | 94369f9d84d969e464b10a8ed18f76600f78600d (patch) | |
tree | b9ae4934a74b51c2c5d240ade653500cdfc1fedc | |
parent | 001782b58655e3a75b00f4db57250db319d525ff (diff) | |
download | pppoe-94369f9d84d969e464b10a8ed18f76600f78600d.zip pppoe-94369f9d84d969e464b10a8ed18f76600f78600d.tar.gz pppoe-94369f9d84d969e464b10a8ed18f76600f78600d.tar.bz2 |
use netwrapper API to connect to PPPoE wrapper
-rwxr-xr-x | Android.mk | 15 | ||||
-rwxr-xr-x | jni/pppoe_jni.cpp | 22 | ||||
-rwxr-xr-x | jni/src/pppoe_cli.c | 11 | ||||
-rwxr-xr-x | jni/src/pppoe_ctrl.c | 188 | ||||
-rwxr-xr-x | jni/src/pppoe_ctrl.h | 32 | ||||
-rwxr-xr-x | jni/src/pppoe_status.h | 3 | ||||
-rwxr-xr-x | jni/src/pppoe_wrapper.c | 115 |
7 files changed, 42 insertions, 344 deletions
@@ -35,12 +35,11 @@ LOCAL_CFLAGS := -DVERSION=$(PPPOE_VERSION) include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) -LOCAL_SRC_FILES:= jni/src/pppoe_ctrl.c\ - jni/src/pppoe_status.c \ +LOCAL_SRC_FILES:= jni/src/pppoe_status.c \ jni/pppoe_jni.cpp LOCAL_SHARED_LIBRARIES := libandroid_runtime libnativehelper -LOCAL_SHARED_LIBRARIES += libc libcutils +LOCAL_SHARED_LIBRARIES += libc libcutils libnetutils LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) $(LOCAL_PATH)/jni/src LOCAL_MODULE_TAGS := optional LOCAL_MODULE := libpppoejni @@ -49,21 +48,17 @@ include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) -LOCAL_SRC_FILES:= jni/src/pppoe_cli.c\ - jni/src/pppoe_ctrl.c +LOCAL_SRC_FILES:= jni/src/pppoe_cli.c LOCAL_MODULE_TAGS := optional LOCAL_MODULE := pcli -LOCAL_CFLAGS += -O2 -LOCAL_LDLIBS += -llog -LOCAL_SHARED_LIBRARIES := libcutils +LOCAL_SHARED_LIBRARIES := libcutils libnetutils include $(BUILD_EXECUTABLE) - include $(CLEAR_VARS) LOCAL_SRC_FILES:= jni/src/pppoe_wrapper.c LOCAL_SHARED_LIBRARIES := \ - libcutils libcrypto + libcutils libcrypto libnetutils LOCAL_C_INCLUDES := \ $(LOCAL_PATH)/include diff --git a/jni/pppoe_jni.cpp b/jni/pppoe_jni.cpp index 0ed28e5..50d51cb 100755 --- a/jni/pppoe_jni.cpp +++ b/jni/pppoe_jni.cpp @@ -2,7 +2,9 @@ #define LOCAL_TAG "PPPOE_JNI" -//$as/dalvik/libnativehelper/include/nativehelper/jni.h +/* +dalvik/libnativehelper/include/nativehelper/jni.h +*/ #include <jni.h> #include <JNIHelp.h> @@ -19,7 +21,7 @@ #include <netinet/in.h> #include <sys/un.h> -#include "pppoe_ctrl.h" +#include <netwrapper.h> #include "pppoe_status.h" #include <android/log.h> @@ -115,7 +117,7 @@ static jboolean com_amlogic_PppoeOperation_connect { char *p_user; char *p_passwd; - struct pppoe_ctrl * ctrl; + struct netwrapper_ctrl * ctrl; char *p; FILE *f; @@ -157,15 +159,15 @@ static jboolean com_amlogic_PppoeOperation_connect __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"ppp.connect\n"); - ctrl = pppoe_ctrl_open("/dev/socket/pppd"); + ctrl = netwrapper_ctrl_open("/etc/ppp/pppoe", PPPOE_WRAPPER_SERVER_PATH); if (ctrl == NULL) { __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "Failed to connect to pppd\n"); return -1; } - pppoe_ctrl_request(ctrl, pppoe_connect_cmd, strlen(pppoe_connect_cmd)); + netwrapper_ctrl_request(ctrl, pppoe_connect_cmd, strlen(pppoe_connect_cmd)); - pppoe_ctrl_close(ctrl); + netwrapper_ctrl_close(ctrl); env->ReleaseStringUTFChars(jstr_account, p_user); env->ReleaseStringUTFChars(jstr_passwd, p_passwd); @@ -178,19 +180,19 @@ static jboolean com_amlogic_PppoeOperation_connect jboolean com_amlogic_PppoeOperation_disconnect (JNIEnv *env, jobject obj) { - struct pppoe_ctrl * ctrl; + struct netwrapper_ctrl * ctrl; __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"ppp.disconnect\n"); - ctrl = pppoe_ctrl_open("/dev/socket/pppd"); + ctrl = netwrapper_ctrl_open("/etc/ppp/pppoe", PPPOE_WRAPPER_SERVER_PATH); if (ctrl == NULL) { __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "Failed to connect to pppd\n"); return -1; } - pppoe_ctrl_request(ctrl, pppoe_disconnect_cmd, strlen(pppoe_disconnect_cmd)); + netwrapper_ctrl_request(ctrl, pppoe_disconnect_cmd, strlen(pppoe_disconnect_cmd)); - pppoe_ctrl_close(ctrl); + netwrapper_ctrl_close(ctrl); return 1; } diff --git a/jni/src/pppoe_cli.c b/jni/src/pppoe_cli.c index 604e244..939a1ec 100755 --- a/jni/src/pppoe_cli.c +++ b/jni/src/pppoe_cli.c @@ -12,8 +12,9 @@ #include <netinet/in.h> #include <sys/un.h> -#include "pppoe_ctrl.h" +#include <netwrapper.h> +#include "pppoe_status.h" static char pppd_connect_cmd[512]; static char pppoe_plugin_cmd[] = {"'pppoe -p /system/etc/ppp/pppoe.pid -I eth0 -T 80 -U -m 1412'" }; @@ -31,7 +32,7 @@ static int usage() int main(int argc, char *argv[]) { int i; - struct pppoe_ctrl * ctrl; + struct netwrapper_ctrl * ctrl; if (argc < 2 || (0 != strcmp( "connect", argv[1]) && 0 != strcmp( "disconnect", argv[1]) )) { @@ -39,7 +40,7 @@ int main(int argc, char *argv[]) return -2; } - ctrl = pppoe_ctrl_open("/dev/socket/pppd"); + ctrl = netwrapper_ctrl_open("/etc/ppp/pppcli", PPPOE_WRAPPER_SERVER_PATH); if (ctrl == NULL) { printf("Failed to connect to pppd\n"); return -1; @@ -55,8 +56,8 @@ int main(int argc, char *argv[]) } - pppoe_ctrl_request(ctrl, pppd_connect_cmd, strlen(pppd_connect_cmd)); + netwrapper_ctrl_request(ctrl, pppd_connect_cmd, strlen(pppd_connect_cmd)); - pppoe_ctrl_close(ctrl); + netwrapper_ctrl_close(ctrl); exit(0); } diff --git a/jni/src/pppoe_ctrl.c b/jni/src/pppoe_ctrl.c deleted file mode 100755 index fcac231..0000000 --- a/jni/src/pppoe_ctrl.c +++ b/dev/null @@ -1,188 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <stdarg.h> -#include <string.h> - -#include <sys/types.h> -#include <errno.h> - -#include <unistd.h> - -#include <sys/socket.h> -#include <netinet/in.h> - -#include <sys/un.h> -#include <sys/stat.h> -#include <android/log.h> -#include "pppoe_ctrl.h" - - -#define LOCAL_TAG "PPPOE_CTRL" -#define TMPDIR "/etc/ppp" - -struct pppoe_ctrl * pppoe_ctrl_open(const char *ctrl_path) -{ - struct pppoe_ctrl *ctrl; - int ret; - size_t res; - int tries = 0; - - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"%s(ctrl_path = %s)\n", __FUNCTION__, ctrl_path); - ctrl = malloc(sizeof(*ctrl)); - if (ctrl == NULL) - return NULL; - memset(ctrl, 0, sizeof(*ctrl)); - - ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0); - if (ctrl->s < 0) { - free(ctrl); - return NULL; - } - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"%s(ctrl->s = %d)\n", __FUNCTION__, ctrl->s); - - ctrl->local.sun_family = AF_UNIX; - -try_again: - ret = snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path), - "%s/ppp_cli-%d", TMPDIR, getpid()); - - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"%s: ctrl->local.sun_path: %s\n", __FUNCTION__, ctrl->local.sun_path); - if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) { - close(ctrl->s); - free(ctrl); - return NULL; - } - tries++; - if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, - sizeof(ctrl->local)) < 0) { -#if 1 - if (errno == EADDRINUSE && tries < 2) { - /* - * getpid() returns unique identifier for this instance - * of pppoe_ctrl, so the existing socket file must have - * been left by unclean termination of an earlier run. - * Remove the file and try again. - */ - unlink(ctrl->local.sun_path); - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"%s: bind failed(%s)\n", __FUNCTION__, strerror(errno)); - goto try_again; - } - - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"%s: bind failed(%s)\n", __FUNCTION__, strerror(errno)); - close(ctrl->s); - free(ctrl); - return NULL; -#endif - } - - ctrl->dest.sun_family = AF_UNIX; - res = strlcpy(ctrl->dest.sun_path, ctrl_path, - sizeof(ctrl->dest.sun_path)); - if (res >= sizeof(ctrl->dest.sun_path)) { - close(ctrl->s); - free(ctrl); - return NULL; - } - - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"%s: ctrl->dest.sun_path: %s\n", __FUNCTION__, ctrl->dest.sun_path); - if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, - sizeof(ctrl->dest)) < 0) { - close(ctrl->s); - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"%s: connection failed\n", __FUNCTION__); - unlink(ctrl->local.sun_path); - free(ctrl); - return NULL; - } - - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"connection to ppp_wrapper OK\n"); - return ctrl; -} - - -void pppoe_ctrl_close(struct pppoe_ctrl *ctrl) -{ - if (ctrl == NULL) - return; - unlink(ctrl->local.sun_path); - if (ctrl->s >= 0) - close(ctrl->s); - free(ctrl); -} - -#define REQUEST_BUF_LEN 1024 -#define ACK_BUF_LEN 128 -#define RESEND_CNT_MAX 10 - -static char request_buf[REQUEST_BUF_LEN]; -static char ack_buf[ACK_BUF_LEN]; -static int req_no = 0; - - -int pppoe_ctrl_request(struct pppoe_ctrl *ctrl, const char *cmd, size_t cmd_len) -{ - int nwritten = -1; - int acked = 0; - fd_set rfds; - struct timeval tv; - int res; - int pid_and_reqno_len =0; - int resend_cnt = 0; - - do { - request_buf[0] = 0; - nwritten = sprintf(request_buf, "%d\t", getpid()); - nwritten += sprintf(request_buf + nwritten, "%d\t", req_no++); - pid_and_reqno_len = nwritten; - nwritten += sprintf(request_buf + nwritten, "%s", cmd); - - if (send(ctrl->s, request_buf, nwritten, 0) < 0) { - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"send command[%s] failed(%s)\n", cmd, strerror(errno)); - goto exit_func; - } - - resend_cnt++; - - tv.tv_sec = 10; - tv.tv_usec = 0; - FD_ZERO(&rfds); - FD_SET(ctrl->s, &rfds); - res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); - if ( res < 0 ) { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"failed to select(%s)\n", strerror(errno)); - goto exit_func; - } else if ( 0 == res ){ - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"Timeout to recv ack, resend request\n"); - continue; - }else if (FD_ISSET(ctrl->s, &rfds)) { - res = recv(ctrl->s, ack_buf, ACK_BUF_LEN-1, 0); - if (res < 0) { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"failed to recv ack(%s)\n", strerror(errno)); - goto exit_func; - } - - if (0 == strncmp(ack_buf, request_buf,pid_and_reqno_len)) { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"recved VALID ack\n"); - acked = 1; - } - else { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"recved INVALID ack: pid_and_reqno_len(%d)\n", pid_and_reqno_len); - ack_buf[pid_and_reqno_len] = 0; - request_buf[pid_and_reqno_len] = 0; - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"ack_buf[%s]\n", ack_buf); - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"request_buf[%s]\n", request_buf); - } - } - }while(!acked && resend_cnt < RESEND_CNT_MAX); - -exit_func: - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG,"send command[%s] %s\n", cmd, acked ? "OK" : "failed"); - return acked ? 0 : -1; -} - - -int pppoe_ctrl_get_fd(struct pppoe_ctrl *ctrl) -{ - return ctrl->s; -} - - diff --git a/jni/src/pppoe_ctrl.h b/jni/src/pppoe_ctrl.h deleted file mode 100755 index f7c9901..0000000 --- a/jni/src/pppoe_ctrl.h +++ b/dev/null @@ -1,32 +0,0 @@ -#ifndef PPPOE_CTRL_H
-#define PPPOE_CTRL_H
-
-
-#define PPPOE_PIDFILE "/system/etc/ppp/pppoe.pid"
-
-
-struct pppoe_ctrl {
- int s; - struct sockaddr_un local; - struct sockaddr_un dest; -}; -
-
-#ifdef __cplusplus
-extern "C" { -#endif -struct pppoe_ctrl * pppoe_ctrl_open(const char *ctrl_path);
-
-void pppoe_ctrl_close(struct pppoe_ctrl *ctrl);
-
-int pppoe_ctrl_request(struct pppoe_ctrl *ctrl, const char *cmd, size_t cmd_len);
-
-int pppoe_ctrl_get_fd(struct pppoe_ctrl *ctrl);
-
-
-#ifdef __cplusplus
-} -#endif -
-
-#endif
diff --git a/jni/src/pppoe_status.h b/jni/src/pppoe_status.h index 7e48ef8..c85846a 100755 --- a/jni/src/pppoe_status.h +++ b/jni/src/pppoe_status.h @@ -5,6 +5,9 @@ #define PPP_STATUS_DISCONNECTED 0x20 #define PPP_STATUS_CONNECTING 0x40 +#define PPPOE_PIDFILE "/system/etc/ppp/pppoe.pid" +#define PPPOE_WRAPPER_SERVER_PATH "/dev/socket/pppoe_wrapper" + #ifdef __cplusplus extern "C" { #endif diff --git a/jni/src/pppoe_wrapper.c b/jni/src/pppoe_wrapper.c index 30af15d..907e267 100755 --- a/jni/src/pppoe_wrapper.c +++ b/jni/src/pppoe_wrapper.c @@ -24,16 +24,12 @@ #include <sys/un.h> #include <android/log.h> +#include <netwrapper.h> -#include "pppoe_ctrl.h" +#include "pppoe_status.h" #define LOCAL_TAG "PPPOE_WRAPPER" -#define PPP_CMD_LEN_MAX 512 - -static char ppp_cmd[PPP_CMD_LEN_MAX]; - - static pid_t read_pid(const char *pidfile) { FILE *fp; @@ -59,7 +55,7 @@ static pid_t read_pid(const char *pidfile) return pid; } -static int ppp_stop() +static int pppoe_disconnect_handler(char *request) { pid_t pid; int ret; @@ -108,102 +104,23 @@ static int ppp_stop() } -int main(int argc, char * argv[]) -{ - int socket_fd; - struct sockaddr_un cli_addr, serv_addr; - int i, len, clilen = 0; - int ppp_cmd_len; - - socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0); - if (socket_fd < 0) { - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, - "failed to create socket(%s)\n", strerror(errno)); - exit(-1); - } - - memset(&serv_addr,0,sizeof(serv_addr)); - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, - "create AF_UNIX socket:%d OK\n",socket_fd); - unlink("/dev/socket/pppd"); - serv_addr.sun_family = AF_UNIX; - strncpy(serv_addr.sun_path, "/dev/socket/pppd", sizeof(serv_addr.sun_path) - 1); +static int pppoe_connect_handler(char *request) +{ + return system(request); +} - if (bind(socket_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr_un)) < 0) { - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, - "failed to bind socket(%s)\n", strerror(errno)); - exit(-1); - } - { - struct timeval tv; - int res; - fd_set rfds; - char *cmd; - - for (;;) { - tv.tv_sec = 10; - tv.tv_usec = 0; - FD_ZERO(&rfds); - FD_SET(socket_fd, &rfds); - res = select(socket_fd + 1, &rfds, NULL, NULL, &tv); - if (res > 0 && FD_ISSET(socket_fd, &rfds)) { - clilen = sizeof (struct sockaddr_un); - res = recvfrom(socket_fd, ppp_cmd, PPP_CMD_LEN_MAX-1, 0, - (struct sockaddr *)&cli_addr,&clilen); - if (res < 0) { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, - "FAILED TO RECVFROM\n"); - - return res; - } - - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, - "client: [%s][%d]\n", - cli_addr.sun_path, clilen); - - ppp_cmd[res] = '\0'; - ppp_cmd_len = res; - - cmd = strchr(ppp_cmd, '\t'); - if (!cmd) { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, - "recv invalid cmd(No TAB found): [%s]\n",ppp_cmd); - continue; - } - cmd++; - - cmd = strchr(cmd, '\t'); - if (!cmd) { - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, - "recv invalid cmd(Second TAB NOT found): [%s]\n",ppp_cmd); - continue; - } - cmd++; - - if (sendto(socket_fd, ppp_cmd, cmd - ppp_cmd, 0, - (struct sockaddr *)&cli_addr, clilen) < 0) { - __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"failed to send ACK(%s)\n", strerror(errno)); - continue; - } - - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, - "recv cmd: [%s]\n",cmd); - - - if ( 0 == strcmp(cmd, "ppp-stop") ) { - ppp_stop(); - } - else { - system(cmd); - } - } - } - } +int main(int argc, char* argv[]) +{ + netwrapper_register_handler("ppp-stop", pppoe_disconnect_handler); + netwrapper_register_handler("pppd pty", pppoe_connect_handler); + netwrapper_main(PPPOE_WRAPPER_SERVER_PATH); - __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, "EXIT\n"); - close(socket_fd); return 0; } + + + + |