TunOutScheduler Class Reference

#include <tunoutscheduler.h>

Inheritance diagram for TunOutScheduler:

RealtimeScheduler List of all members.

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

Constructor & Destructor Documentation

TunOutScheduler::~TunOutScheduler (  )  [virtual]

00006 {
00007     if (app_fd >= 0) {
00008         close(app_fd);
00009     }
00010     if (additional_fd >= 0) {
00011         close(additional_fd);
00012     }
00013 
00014     delete dev;
00015 }


Member Function Documentation

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.

00094                                    {
00095     int new_sock = accept( additional_fd, 0, 0 );
00096     if (app_fd >= 0) {
00097         // We already have a connection to an application
00098         // "reject" connection
00099         close(new_sock);
00100         ev << "Rejecting new app connection (FD: " << new_sock << ")\n";
00101     } else {
00102         app_fd = new_sock;
00103         ev << "Accepting new app connection (FD: " << app_fd << ")\n";
00104         if (app_fd < 0) {
00105             opp_warning("Error connecting to remote app");
00106             app_fd = -1;
00107         }
00108     }
00109 
00110 }

int TunOutScheduler::initializeNetwork (  )  [protected, virtual]

Initialize the network.

Implements RealtimeScheduler.

00019 {
00020     // Initialize TUN device for network communication
00021     // see /usr/src/linux/Documentation/network/tuntap.txt
00022     struct ifreq ifr;
00023     int err;
00024     dev = new char[IFNAMSIZ];
00025 
00026     // get app port (0 if external app is not used)
00027     int appPort = ev.config()->getAsInt("ExternalApp", "app-port");
00028 
00029     // Initialize TCP socket for App communication if desired
00030     if ( appPort > 0 ) {
00031 
00032         struct sockaddr_in server;
00033         int sock;
00034 
00035         // Waiting for a TCP connection
00036         // WARNING: Will only accept exactly ONE app connecting to the socket
00037         sock = socket( AF_INET, SOCK_STREAM, 0 );
00038         if (sock < 0) {
00039             opp_error("Error creating socket");
00040             return -1;
00041         }
00042         memset( &server, 0, sizeof (server));
00043         server.sin_family = AF_INET;
00044         server.sin_addr.s_addr = htonl( INADDR_ANY );
00045         server.sin_port = htons( appPort );
00046 
00047         if(bind( sock, (struct sockaddr*)&server, sizeof( server)) < 0) {
00048             opp_error("Error binding to app socket");
00049             return -1;
00050         }
00051         if( listen( sock, 5 ) == -1 ) {
00052             opp_error("Error listening on app socket");
00053             return -1;
00054         }
00055         // Set additional_fd so we will be called if data
00056         // (i.e. connection requests) is available at sock
00057         additional_fd = sock;
00058     }
00059 
00060     if (netw_fd > 0) {
00061         opp_error("Already bound to TUN device!");
00062         return -1;
00063     }
00064     if( (netw_fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
00065         opp_warning("Error opening tun device");
00066         return 0;
00067     } else {
00068         ev << "TunOutScheduler: Successfully opened TUN device.\n";
00069     }
00070 
00071     memset(&ifr, 0, sizeof(ifr));
00072 
00073     /* Flags: IFF_TUN   - TUN device (no Ethernet headers)
00074      *        IFF_TAP   - TAP device  
00075      * 
00076      *        IFF_NO_PI - Do not provide packet information  
00077      */
00078     ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
00079     strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ);
00080 
00081     if((err = ioctl(netw_fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
00082         close(netw_fd);
00083         opp_error("Error ioctl tun device");
00084         return -1;
00085     }
00086     strncpy(dev, ifr.ifr_name, IFNAMSIZ);
00087     ev << "TunOutScheduler: Bound to device " << dev << "\n";
00088     ev << "Remember to bring up TUN device with ifconfig before proceeding\n";
00089 
00090 
00091     return 0;
00092 }


Member Data Documentation

char* TunOutScheduler::dev [protected]


The documentation for this class was generated from the following files:
Generated on Fri May 11 14:52:41 2007 for ITM OverSim by  doxygen 1.4.7