/**************************************************************************
* simpletun.c *
* *
* A simplistic, simple-minded, naive tunnelling program using tun/tap *
* interfaces and TCP. DO NOT USE THIS PROGRAM FOR SERIOUS PURPOSES. *
* *
* You have been warned. *
* *
* (C) 2010 Davide Brini. *
* *
* DISCLAIMER AND WARNING: this is all work in progress. The code is *
* ugly, the algorithms are naive, error checking and input validation *
* are very basic, and of course there can be bugs. If that's not enough, *
* the program has not been thoroughly tested, so it might even fail at *
* the few simple things it should be supposed to do right. *
* Needless to say, I take no responsibility whatsoever for what the *
* program might do. The program has been written mostly for learning *
* purposes, and can be used in the hope that is useful, but everything *
* is to be taken "as is" and without any kind of warranty, implicit or *
* explicit. See the file LICENSE for further details. *
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <stdarg.h>
#include <pthread.h>
/* buffer for reading from tun/tap interface, must be >= 1500 */
#define BUFSIZE 200000
#define CLIENT 0
#define SERVER 1
#define PORT 55555
#define FILEOUT 1
int debug;
char *progname;
int recv_control_flag, type, value, tap2netInterval = 50;
int net_fd;
struct sockaddr_in dst_addr;
/**************************************************************************
* tun_alloc: allocates or reconnects to a tun/tap device. The caller *
* must reserve enough space in *dev. *
**************************************************************************/
int tun_alloc(char *dev, int flags) {
struct ifreq ifr;
int fd, err;
char *clonedev = "/dev/net/tun";
if( (fd = open(clonedev , O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
if (*dev) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
perror("ioctl(TUNSETIFF)");
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
/**************************************************************************
* cread: read routine that checks for errors and exits if an error is *
* returned. *
**************************************************************************/
int cread(int fd, char *buf, int n){
int nread;
if((nread=read(fd, buf, n)) < 0){
perror("Reading data");
exit(1);
}
return nread;
}
/**************************************************************************
* cwrite: write routine that checks for errors and exits if an error is *
* returned. *
**************************************************************************/
int cwrite(int fd, char *buf, int n){
int nwrite;
if((nwrite=write(fd, buf, n)) < 0){
perror("Writing data");
exit(1);
}
return nwrite;
}
/**************************************************************************
* read_n: ensures we read exactly n bytes, and puts them into "buf". *
* (unless EOF, of course) *
**************************************************************************/
int read_n(int fd, char *buf, int n) {
int nread, left = n;
while(left > 0) {
if ((nread = cread(fd, buf, left)) == 0){
return 0 ;
}else {
left -= nread;
buf += nread;
}
}
return n;
}
/**************************************************************************
* do_debug: prints debugging stuff (doh!) *
**************************************************************************/
void do_debug(char *msg, ...){
va_list argp;
if(debug) {
va_start(argp, msg);
vfprintf(stderr, msg, argp);
va_end(argp);
}
}
/**************************************************************************
* my_err: prints custom error messages on stderr. *
**************************************************************************/
void my_err(char *msg, ...) {
va_list argp;
va_start(argp, msg);
vfprintf(stderr, msg, argp);
va_end(argp);
}
/**************************************************************************
* usage: prints usage and exits. *
**************************************************************************/
void usage(void) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s -i <ifacename> [-s|-c <serverIP>] [-p <port>] [-u|-a] [-d]\n", progname);
fprintf(stderr, "%s -h\n", progname);
fprintf(stderr, "\n");
fprintf(stderr, "-i <ifacename>: Name of interface to use (mandatory)\n");
fprintf(stderr, "-s|-c <serverIP>: run in server mode (-s), or specify server address (-c <serverIP>) (mandatory)\n");
fprintf(stderr, "-p <port>: port to listen on (if run in server mode) or to connect to (in client mode), default 55555\n");
fprintf(stderr, "-u|-a: use TUN (-u, default) or TAP (-a)\n");
fprintf(stderr, "-d: outputs debug information while running\n");
fprintf(stderr, "-m: tun or tap ip address\n");
fprintf(stderr, "-h: prints this help text\n");
exit(1);
}
void *recv_control(void *para) {
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if (cfd < 0)
{
perror("socket error");
exit(1);
}
struct sockaddr_in serv;
struct sockaddr_in clientsock;
bzero(&serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(12366);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
bind(cfd, (struct sockaddr *)&serv, sizeof(serv));
int i;
int n;
socklen_t len;
uint32_t buf[100];
while (1)
{
memset(buf, 0x00, sizeof(buf));
len = sizeof(clientsock);
n = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&clientsock, &len);
for (int i = 0; i < n; i++) {
do_debug("%d ", buf[i]);
}
do_debug("\n");
if ((buf[0] == 0xffffffff) && (buf[1] == 1 || buf[1] == 2) &&
(buf[2] >= 1 && buf[2] <= 3)) {
recv_control_flag = 1;
type = buf[1];
value = buf[2];
int32_t control_message[3];
if (type == 2) {
control_message[0] = 0xAAAAAAAA;
if (value == 1) {
tap2netInterval = 50;
} else if (value == 2) {
tap2netInterval = 250;
}
int len = sendto(net_fd, control_message, 4, 0, (struct sockaddr *)&dst_addr, sizeof(dst_addr));
do_debug("send reset message len : %d\n", len);
sleep(7);
}
control_message[0] = 0xFFFFFFFF;
control_message[1] = type;
control_message[2] = value;
int len = sendto(net_fd, control_message, 12, 0, (struct sockaddr *)&dst_addr, sizeof(dst_addr));
do_debug("send handout message len : %d\n", len);
}
else {
printf("receive syntax error\n");
}
sleep(1);
}
close(cfd);
}
int main(int argc, char *argv[]) {
int tap_fd, option;
int flags = IFF_TUN;
char if_name[IFNAMSIZ] = "";
int maxfd;
uint32_t nread, nwrite, plength;
char buffer[BUFSIZE];
struct sockaddr_in local, remote;
char remote_ip[16] = ""; /* dotted quad IP string */
char local_ip[16] = "";
char if_ipaddress[20] ="";
unsigned short int port = PORT;
unsigned short int rm_port = PORT;
int sock_fd, optval = 1;
socklen_t remotelen;
int cliserv = -1; /* must be specified on cmd line */
unsigned long int tap2net = 0, net2tap = 0;
char *Mode[2] = {"tun", "tap"};
int mode = 0;
int connected_flag = 0;
recv_control_flag = 0;
progname = argv[0];
/* Check command line options */
while((option = getopt(argc, argv, "i:s:c:p:r:uahdm:")) > 0) {
switch(option) {
case 'd':
debug = 1;
break;
case 'h':
usage();
break;
case 'i':
strncpy(if_name,optarg, IFNAMSIZ-1);
break;
case 's':
cliserv = SERVER;
strncpy(remote_ip,optarg,15);
break;
case 'c':
cliserv = CLIENT;
strncpy(local_ip,optarg,15);
break;
case 'p':
port = atoi(optarg);
break;
case 'r':
rm_port = atoi(optarg);
break;
case 'u':
flags = IFF_TUN;
break;
case 'a':
flags = IFF_TAP;
mode = 1;
break;
case 'm':
strncpy(if_ipaddress,optarg,20);
break;
default:
my_err("Unknown option %c\n", option);
usage();
}
}
argv += optind;
argc -= optind;
if(argc > 0) {
my_err("Too many options!\n");
usage();
}
if(*if_name == '\0') {
my_err("Must specify interface name!\n");
usage();
} else if(cliserv < 0) {
my_err("Must specify client or server mode!\n");
usage();
} else if((cliserv == CLIENT)&&(*remote_ip == '\0')) {
my_err("Must specify server address!\n");
usage();
}
#if 0
char *gnb = "gnb.txt";
char *ue = "ue.txt";
FILE *gnb_fd, *ue_fd;
gnb_fd = fopen(gnb, "w");
ue_fd = fopen(ue, "w");
if (gnb_fd == NULL || ue_fd == NULL) {
fprintf(stderr, "can't open file\n");
return 1;
}
#endif
char cmd[256];
snprintf(cmd, sizeof(cmd), "sudo ip tuntap del %s mode tap",if_name);
system(cmd);
snprintf(cmd, sizeof(cmd), "sudo ip tuntap del %s mode tun",if_name);
system(cmd);
if(*if_ipaddress != '\0'){
// char *if_ipaddress = "6.6.6.1/24";
snprintf(cmd, sizeof(cmd), "sudo ip tuntap add %s mode %s",if_name, Mode[mode]);
system(cmd);
snprintf(cmd, sizeof(cmd), "sudo ip addr add %s dev %s", if_ipaddress,if_name);
system(cmd);
snprintf(cmd, sizeof(cmd), "sudo ip link set dev %s up", if_name);
system(cmd);
char *remote_tun = (char *)malloc(sizeof(if_ipaddress));
memcpy(remote_tun, if_ipaddress, sizeof(if_ipaddress));
char *lastDot = strrchr(remote_tun, '.');
// int lastOctet = atoi(lastDot + 1);
sprintf(lastDot + 1, "0/24");
do_debug("remote_tun ip: %s\n", remote_tun);
snprintf(cmd, sizeof(cmd), "sudo ip route add %s dev %s", remote_tun, if_name);
system(cmd);
}
// printf("pass\n");
/* initialize tun/tap interface */
if ( (tap_fd = tun_alloc(if_name, flags | IFF_NO_PI)) < 0 ) {
my_err("Error connecting to tun/tap interface %s!\n", if_name);
exit(1);
}
do_debug("Successfully connected to interface %s ip address %s\n", if_name, if_ipaddress);
/*while(1){
nread = cread(tap_fd, buffer, BUFSIZE);
tap2net++;
do_debug("TAP2NET %lu: Read %d bytes from the tap interface\n", tap2net, nread);
}*/
pthread_t pthread_recv = 0;
int recv_flag = pthread_create(&pthread_recv, NULL, &recv_control, NULL);
if (recv_flag != 0)
{
printf("create control_input failed\n");
}
if ( (sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket()");
exit(1);
}
// 第二步:绑定地址(IP + Port)
struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = inet_addr(local_ip);
myaddr.sin_port = htons(port);
if(bind(sock_fd, (struct sockaddr*)&myaddr, sizeof(myaddr)) == -1)
{
perror("bind");
return 1;
}
// 第三步:收发数据
// 发送数�?
// 指定接收方地址
dst_addr.sin_family = AF_INET;
dst_addr.sin_addr.s_addr = inet_addr(remote_ip);//命令行输入接收方IP
dst_addr.sin_port = htons(rm_port);//命令行输入端口号
net_fd = sock_fd;
/* use select() to handle two descriptors at once */
maxfd = (tap_fd > net_fd)?tap_fd:net_fd;
socklen_t len = sizeof(struct sockaddr_in);
struct sockaddr_in serveraddr;
while(1) {
int ret;
fd_set rd_set;
FD_ZERO(&rd_set);
FD_SET(tap_fd, &rd_set); FD_SET(net_fd, &rd_set);
ret = select(maxfd + 1, &rd_set, NULL, NULL, NULL);
if (ret < 0 && errno == EINTR){
continue;
}
if (ret < 0) {
perror("select()");
exit(1);
}
if(FD_ISSET(tap_fd, &rd_set)) {
/* data from tun/tap: just read it and write it to the network */
usleep(tap2netInterval);
nread = cread(tap_fd, buffer, BUFSIZE);
if ((buffer[0]&0xf0)>>4 == 6) continue;
tap2net++;
do_debug("TAP2NET %lu: Read %d bytes from the tap interface\n", tap2net, nread);
/* write length + packet */
//plength = htons(nread);
//nwrite = cwrite(net_fd, (char *)&plength, sizeof(plength));
//nwrite = cwrite(net_fd, buffer, nread);
sendto(net_fd, buffer, nread, 0, (struct sockaddr *)&dst_addr, sizeof(dst_addr));
do_debug("TAP2NET %lu: Written %d bytes to the network\n", tap2net, nread);
#if 1
do_debug("TAP2NET %lu: ", tap2net);
for (int i = 0; i < nread; i++){
// if (buffer[i] != 0)
// fprintf(gnb_fd, "%02hhx ", buffer[i]);
do_debug("%02hhx ", buffer[i]);
}
//fprintf(gnb_fd, "\n");
do_debug("\n");
#endif
}
if(FD_ISSET(net_fd, &rd_set)) {
/* data from the network: read it, and write it to the tun/tap interface.
* We need to read the length first, and then the packet */
/* Read length */
//nread = read_n(net_fd, (char *)&plength, sizeof(plength));
// nread = read_n(net_fd, buffer, BUFSIZE);//ntohs(plength));
remotelen = sizeof(dst_addr);
nread = recvfrom(net_fd, buffer, BUFSIZE, 0, (struct sockaddr *)&dst_addr, &remotelen);
do_debug("recv:%d\n", nread);
if(nread == 0) {
/* ctrl-c at the other end */
break;
}
for (int i = 0; i < nread; i++){
do_debug("%02hhx ", buffer[i]);
}
do_debug("\n");
net2tap++;
// if (nread > 200) printf("recv:%d\n", nread);
/* read packet */
// nread = read_n(net_fd, buffer, ntohs(plength));
// do_debug("NET2TAP %lu: Read %d bytes from the network\n", net2tap, nread);
/* now buffer[] contains a full packet or frame, write it into the tun/tap interface */
nwrite = write(tap_fd, buffer, nread);
do_debug("NET2TAP %lu: Written %d bytes to the tap interface\n", net2tap, nwrite);
if (connected_flag == 0) {
my_err("baseband successful\n");
connected_flag = 1;
}
#if 0
do_debug("NET2TAP %lu: ", net2tap);
for (int i = 0; i < nwrite; i++){
if (buffer[i] != 0)
fprintf(ue_fd, "%02hhx ", buffer[i]);
do_debug("%02hhx ", buffer[i]);
}
fprintf(ue_fd, "\n");
do_debug("\n");
#endif
}
}
return(0);
}
最新发布