kni tap mode 1

This commit is contained in:
石逢钊
2019-08-07 20:36:54 +08:00
parent d4525b0c5c
commit 47eacf2c3a

View File

@@ -1,18 +1,94 @@
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/if_tun.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}