#include <tunoutscheduler.h>
Inheritance diagram for TunOutScheduler:
Public Member Functions | |
virtual | ~TunOutScheduler () |
Protected Member Functions | |
virtual int | initializeNetwork () |
Initialize the network. | |
virtual void | additionalFD () |
This function is called from main loop if data is accessable from "additional_fd". | |
Protected Attributes | |
char * | dev |
TunOutScheduler::~TunOutScheduler | ( | ) | [virtual] |
00029 { 00030 if (app_fd >= 0) { 00031 close(app_fd); 00032 } 00033 if (additional_fd >= 0) { 00034 close(additional_fd); 00035 } 00036 00037 delete dev; 00038 }
int TunOutScheduler::initializeNetwork | ( | ) | [protected, virtual] |
Initialize the network.
Implements RealtimeScheduler.
00042 { 00043 // Initialize TUN device for network communication 00044 // see /usr/src/linux/Documentation/network/tuntap.txt 00045 struct ifreq ifr; 00046 int err; 00047 dev = new char[IFNAMSIZ]; 00048 00049 // get app port (0 if external app is not used) 00050 int appPort = ev.config()->getAsInt("ExternalApp", "app-port"); 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 sock = socket( AF_INET, SOCK_STREAM, 0 ); 00061 if (sock < 0) { 00062 opp_error("Error creating socket"); 00063 return -1; 00064 } 00065 00066 int on = 1; 00067 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); 00068 00069 memset( &server, 0, sizeof (server)); 00070 server.sin_family = AF_INET; 00071 server.sin_addr.s_addr = htonl( INADDR_ANY ); 00072 server.sin_port = htons( appPort ); 00073 00074 if(bind( sock, (struct sockaddr*)&server, sizeof( server)) < 0) { 00075 opp_error("Error binding to app socket"); 00076 return -1; 00077 } 00078 if( listen( sock, 5 ) == -1 ) { 00079 opp_error("Error listening on app socket"); 00080 return -1; 00081 } 00082 // Set additional_fd so we will be called if data 00083 // (i.e. connection requests) is available at sock 00084 additional_fd = sock; 00085 } 00086 00087 if (netw_fd > 0) { 00088 opp_error("Already bound to TUN device!"); 00089 return -1; 00090 } 00091 if( (netw_fd = open("/dev/net/tun", O_RDWR)) < 0 ) { 00092 opp_warning("Error opening tun device"); 00093 return 0; 00094 } else { 00095 ev << "[TunOutScheduler::initializeNetwork()]\n" 00096 << "\t Successfully opened TUN device" 00097 << endl; 00098 } 00099 00100 memset(&ifr, 0, sizeof(ifr)); 00101 00102 /* Flags: IFF_TUN - TUN device (no Ethernet headers) 00103 * IFF_TAP - TAP device 00104 * 00105 * IFF_NO_PI - Do not provide packet information 00106 */ 00107 ifr.ifr_flags = IFF_TUN | IFF_NO_PI; 00108 strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ); 00109 00110 if((err = ioctl(netw_fd, TUNSETIFF, (void *) &ifr)) < 0 ) { 00111 close(netw_fd); 00112 opp_error("Error ioctl tun device"); 00113 return -1; 00114 } 00115 strncpy(dev, ifr.ifr_name, IFNAMSIZ); 00116 ev << "[TunOutScheduler::initializeNetwork()]\n" 00117 << " Bound to device " << dev << "\n" 00118 << " Remember to bring up TUN device with ifconfig before proceeding" 00119 << endl; 00120 return 0; 00121 }
void TunOutScheduler::additionalFD | ( | ) | [protected, virtual] |
This function is called from main loop if data is accessable from "additional_fd".
This FD can be set in initializeNetwork by concrete implementations.
Reimplemented from RealtimeScheduler.
00123 { 00124 int new_sock = accept( additional_fd, 0, 0 ); 00125 if (app_fd >= 0) { 00126 // We already have a connection to an application 00127 // "reject" connection 00128 close(new_sock); 00129 ev << "[TunOutScheduler::additionalFD()]\n" 00130 << " Rejecting new app connection (FD: " << new_sock << ")" 00131 << endl; 00132 } else { 00133 app_fd = new_sock; 00134 ev << "[TunOutScheduler::additionalFD()]\n" 00135 << " Accepting new app connection (FD: " << app_fd << ")" 00136 << endl; 00137 if (app_fd < 0) { 00138 opp_warning("Error connecting to remote app"); 00139 app_fd = -1; 00140 } 00141 } 00142 00143 }
char* TunOutScheduler::dev [protected] |