From 47eacf2c3a4de7064b25d7b0444248f1c854bb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E9=80=A2=E9=92=8A?= Date: Wed, 7 Aug 2019 20:36:54 +0800 Subject: [PATCH] kni tap mode 1 --- entry/src/tap_mode.cpp | 90 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/entry/src/tap_mode.cpp b/entry/src/tap_mode.cpp index 52d8f6b..51f910b 100644 --- a/entry/src/tap_mode.cpp +++ b/entry/src/tap_mode.cpp @@ -1,18 +1,94 @@ - +#include +#include +#include +#include +#include +#include "kni_utils.h" +#include "MESA_prof_load.h" struct tap_mode_handle{ int fd; void *logger; }; struct tap_mode_handle* tap_mode_init(void *logger){ + struct tap_mode_handle * tap_handle = (struct tap_mode_handle*)malloc(sizeof(struct tap_mode_handle)); -} + char tap_path[1024] = {0}; + char tap_name[IFNAMSIZ] = {0}; + struct ifreq ifr; + int err = 0; + MESA_load_profile_string_def(".kniconf/kni.conf","tap",(char*)"tap_path",tap_path,1024,"/dev/net/tap"); + MESA_load_profile_string_def(".kniconf/kni.conf","tap",(char*)"tap_name",tap_name,1024,"/dev/net/tap"); -int tap_mode_write(struct tap_mode_handle *handle, char *buff, uint16_t buff_len){ - -} - -int tap_mode_read(struct tap_mode_handle *handle, char *buff, uint16_t buff_len){ + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE; + if(*tap_name) + { + strncpy(ifr.ifr_name, tap_name, IFNAMSIZ); + } + + if((tap_handle ->fd = open(tap_path, O_RDWR)) < 0) + { + KNI_LOG_ERROR(logger, "tap_mode_init():open error,errno is:%d,%s",errno,strerror(errno)); + free(tap_handle); + return NULL; + } + err = ioctl(tap_handle ->fd, TUNSETIFF, (void *)&ifr); + if (err) + { + KNI_LOG_ERROR(logger ,"tap_mode_init():ioctl error,errno is:%d,%s",errno,strerror(errno)); + close(tap_handle ->fd); + free(tap_handle); + return NULL; + } + tap_handle -> logger = logger; + retrun tap_handle; +} + + + +/* +* > 0 : send data length +* = 0 : send null +* = -1 : error +*/ +int tap_mode_write(struct tap_mode_handle *handle, char *buff, uint16_t buff_len){ + int sendlen=0; + + sendlen = write(handle -> fd, buff, buff_len); + if(sendlen < 0) + { + KNI_LOG_ERROR(handle -> logger, "tap_mode_write() error %d, %s",errno,strerror(errno)); + return -1; + } + else + { + return sendlen; + } + + return 0; +} + +/* +* > 0 : read data length +* = 0 : read null +* = -1 : error +*/ +int tap_mode_read(struct tap_mode_handle *handle, char *buff, uint16_t buff_len){ + int recv_len=0; + + recv_len = read(handle -> fd, buff, buff_len); + if(recv_len <0) + { + KNI_LOG_ERROR(handle -> logger, "tap_mode_read() error %d, %s",errno,strerror(errno)); + return -1; + } + else + { + return recv_len; + } + + return 0; } \ No newline at end of file