47 #if defined _WIN32 || defined __APPLE__
48 throw cRuntimeError(
"TunOutSchedulter::initializeNetwork():"
49 "TUN interface not supported on Windows/Max OS!");
56 dev =
new char[IFNAMSIZ];
63 struct sockaddr_in server;
68 sock = socket( AF_INET, SOCK_STREAM, 0 );
69 if (sock == INVALID_SOCKET) {
70 opp_error(
"Error creating socket");
75 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on));
77 memset( &server, 0,
sizeof (server));
78 server.sin_family = AF_INET;
79 server.sin_addr.s_addr = htonl( INADDR_ANY );
80 server.sin_port = htons( appPort );
82 if (bind( sock, (
struct sockaddr*)&server,
sizeof( server)) < 0) {
83 opp_error(
"Error binding to app socket");
86 if (listen( sock, 5 ) == -1 ) {
87 opp_error(
"Error listening on app socket");
97 if (
netw_fd != INVALID_SOCKET) {
98 opp_error(
"Already bound to TUN device!");
102 if ((
netw_fd = open(
"/dev/net/tun", O_RDWR)) < 0 ) {
103 opp_warning(
"Error opening tun device");
106 ev <<
"[TunOutScheduler::initializeNetwork()]\n"
107 <<
"\t Successfully opened TUN device"
111 memset(&ifr, 0,
sizeof(ifr));
118 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
119 strncpy(ifr.ifr_name,
"tun%d", IFNAMSIZ);
121 if((err = ioctl(
netw_fd, TUNSETIFF, (
void *) &ifr)) < 0 ) {
123 opp_error(
"Error ioctl tun device");
127 strncpy(
dev, ifr.ifr_name, IFNAMSIZ);
128 ev <<
"[TunOutScheduler::initializeNetwork()]\n"
129 <<
" Bound to device " <<
dev <<
"\n"
130 <<
" Remember to bring up TUN device with ifconfig before proceeding"
140 sockaddr* from = (sockaddr*)
new sockaddr_in;
141 socklen_t addrlen =
sizeof(sockaddr_in);
144 if (new_sock == INVALID_SOCKET) {
145 opp_warning(
"Error connecting to remote app");
150 for (SOCKET fd = 0; fd <
maxfd; fd++) {
153 if( FD_ISSET(fd, &
all_fds)) count++;
159 ev <<
"[UdpOutScheduler::additionalFD()]\n"
160 <<
" Rejecting new app connection (FD: " << new_sock <<
")"
174 ev <<
"[UdpOutScheduler::additionalFD()]\n"
175 <<
" Accepting new app connection (FD: " << new_sock <<
")"