#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: Successfully opened TUN device.\n"; 00096 } 00097 00098 memset(&ifr, 0, sizeof(ifr)); 00099 00100 /* Flags: IFF_TUN - TUN device (no Ethernet headers) 00101 * IFF_TAP - TAP device 00102 * 00103 * IFF_NO_PI - Do not provide packet information 00104 */ 00105 ifr.ifr_flags = IFF_TUN | IFF_NO_PI; 00106 strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ); 00107 00108 if((err = ioctl(netw_fd, TUNSETIFF, (void *) &ifr)) < 0 ) { 00109 close(netw_fd); 00110 opp_error("Error ioctl tun device"); 00111 return -1; 00112 } 00113 strncpy(dev, ifr.ifr_name, IFNAMSIZ); 00114 ev << "TunOutScheduler: Bound to device " << dev << "\n"; 00115 ev << "Remember to bring up TUN device with ifconfig before proceeding\n"; 00116 00117 00118 return 0; 00119 }
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.
00121 { 00122 int new_sock = accept( additional_fd, 0, 0 ); 00123 if (app_fd >= 0) { 00124 // We already have a connection to an application 00125 // "reject" connection 00126 close(new_sock); 00127 ev << "Rejecting new app connection (FD: " << new_sock << ")\n"; 00128 } else { 00129 app_fd = new_sock; 00130 ev << "Accepting new app connection (FD: " << app_fd << ")\n"; 00131 if (app_fd < 0) { 00132 opp_warning("Error connecting to remote app"); 00133 app_fd = -1; 00134 } 00135 } 00136 00137 }
char* TunOutScheduler::dev [protected] |