#include <tunoutscheduler.h>
Inheritance diagram for TunOutScheduler:
Public Member Functions | |
virtual | ~TunOutScheduler () |
Protected Member Functions | |
virtual int | initializeNetwork () |
Initialize the network. | |
Protected Attributes | |
char * | dev |
TunOutScheduler::~TunOutScheduler | ( | ) | [virtual] |
int TunOutScheduler::initializeNetwork | ( | ) | [protected, virtual] |
Initialize the network.
Implements RealtimeScheduler.
00012 { 00013 // Initialize TUN device for network communication 00014 // see /usr/src/linux/Documentation/network/tuntap.txt 00015 struct ifreq ifr; 00016 int err; 00017 dev = new char[IFNAMSIZ]; 00018 00019 // get app port (0 if external app is not used) 00020 int appPort = ev.config()->getAsInt("ExternalApp", "app-port"); 00021 00022 if (netw_fd > 0) { 00023 opp_error("Already bound to TUN device!"); 00024 return -1; 00025 } 00026 if( (netw_fd = open("/dev/net/tun", O_RDWR)) < 0 ) { 00027 opp_error("Error opening tun device"); 00028 return -1; 00029 } else { 00030 ev << "TunOutScheduler: Successfully opened TUN device.\n"; 00031 } 00032 00033 memset(&ifr, 0, sizeof(ifr)); 00034 00035 /* Flags: IFF_TUN - TUN device (no Ethernet headers) 00036 * IFF_TAP - TAP device 00037 * 00038 * IFF_NO_PI - Do not provide packet information 00039 */ 00040 ifr.ifr_flags = IFF_TUN | IFF_NO_PI; 00041 strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ); 00042 00043 if((err = ioctl(netw_fd, TUNSETIFF, (void *) &ifr)) < 0 ) { 00044 close(netw_fd); 00045 opp_error("Error ioctl tun device"); 00046 return -1; 00047 } 00048 strncpy(dev, ifr.ifr_name, IFNAMSIZ); 00049 ev << "TunOutScheduler: Bound to device " << dev << "\n"; 00050 if (appPort == 0) opp_warning("Bring up TUN device with ifconfig before proceeding"); 00051 00052 // Initialize TCP socket for App communication if desired 00053 if ( appPort > 0 ) { 00054 00055 struct sockaddr_in server; 00056 int sock; 00057 00058 // Waiting for a TCP connection 00059 // WARNING: Will only accept exactly ONE app connecting to the socket 00060 // WARNING: Blocks until connection to app is established 00061 sock = socket( AF_INET, SOCK_STREAM, 0 ); 00062 if (sock < 0) { 00063 opp_error("Error creating socket"); 00064 return -1; 00065 } 00066 memset( &server, 0, sizeof (server)); 00067 server.sin_family = AF_INET; 00068 server.sin_addr.s_addr = htonl( INADDR_ANY ); 00069 server.sin_port = htons( appPort ); 00070 00071 if(bind( sock, (struct sockaddr*)&server, sizeof( server)) < 0) { 00072 opp_error("Error binding to app socket"); 00073 return -1; 00074 } 00075 if( listen( sock, 5 ) == -1 ) { 00076 opp_error("Error listening on app socket"); 00077 return -1; 00078 } 00079 opp_warning("Immediately after acknowledging this message, bring up TUN" 00080 "device with ifconfig and connect the external app with tcp to " 00081 "port %i ", appPort); 00082 app_fd = accept( sock, 0, 0 ); 00083 if (app_fd < 0) { 00084 opp_error("Error connecting to remote app"); 00085 return -1; 00086 } 00087 } 00088 return 0; 00089 }
char* TunOutScheduler::dev [protected] |