vtun 建立隧道的套接字和传输截获包的套接字
认证即隧道的建立用TCP套接字,之后根据配置文件选择合适协议传输。
clinet隧道建立套接字和传输套接字替换
1、在clinet函数中
s = socket(AF_INET,SOCK_STREAM,0);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(s,(struct sockaddr *)&my_addr,sizeof(my_addr)) ;
2、在connect_t函数中
fcntl(s,F_SETFL,O_NONBLOCK);
connect(s,svr,sizeof(struct sockaddr);
select(s+1,NULL,&fdset,NULL,timeout?&tv:NULL);
fcntl(s,F_SETFL,sock_flags);
3、在auth_client中
readn_t(fd, buf, VTUN_MESG_SIZE, vtun.timeout);
print_p(fd,"HOST: %s\n",host->host);//print_p向server发送认证信息。
host->rmt_fd = s; //这里的host->rmt_fd是TCP socket.
4、认证成功后
如果传输数据用UDP协议,在udp_session中,用UDP socket替换TCP socket.
s=socket(AF_INET,SOCK_DGRAM,0)
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(s,(struct sockaddr *)&saddr,sizeof(saddr)
write_n(host->rmt_fd,(char *)&port,sizeof(short)
readn_t(host->rmt_fd,&port,sizeof(short),host->timeout)
connect(s,(struct sockaddr *)&saddr,sizeof(saddr))
host->rmt_fd = s; //这里的host->rmt_fd是UDP socket
后续数据传输是对该套接字操作。
如果传输数据用TCP协议,在tunnel函数中,继续使用该TCP socket.
setsockopt(host->rmt_fd,SOL_SOCKET,SO_KEEPALIVE,&opt,sizeof(opt) );
setsockopt(host->rmt_fd,IPPROTO_TCP,TCP_NODELAY,&opt,sizeof(opt) );
server隧道建立套接字和传输套接字替换
1、listerner函数中
s=socket(AF_INET,SOCK_STREAM,0);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(s,(struct sockaddr *)&my_addr,sizeof(my_addr));
listen(s, 10);
accept(s,(struct sockaddr *)&cl_addr,&opt);
2、auth_server函数中
readn_t(fd, buf, VTUN_MESG_SIZE, vtun.timeout)
print_p(fd,"OK CHAL: %s\n", cl2cs(chal_req));
3、认证成功后
如果传输数据用UDP协议,在udp_session中,用UDP socket替换TCP socket.
s=socket(AF_INET,SOCK_DGRAM,0)
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(s,(struct sockaddr *)&saddr,sizeof(saddr)
write_n(host->rmt_fd,(char *)&port,sizeof(short)
readn_t(host->rmt_fd,&port,sizeof(short),host->timeout)
connect(s,(struct sockaddr *)&saddr,sizeof(saddr))
host->rmt_fd = s; //这里的host->rmt_fd是UDP socket
后续数据传输是对该套接字操作。
如果传输数据用TCP协议,在tunnel函数中,继续使用该TCP socket.
setsockopt(host->rmt_fd,SOL_SOCKET,SO_KEEPALIVE,&opt,sizeof(opt) );
setsockopt(host->rmt_fd,IPPROTO_TCP,TCP_NODELAY,&opt,sizeof(opt) );