25 #include "IPvXAddress.h"
26 #include <regmacros.h>
46 #if defined _WIN32 || defined __APPLE__
47 throw cRuntimeError(
"TunOutSchedulter::initializeNetwork():"
48 "TUN interface not supported on Windows/Max OS!");
57 struct sockaddr_in server;
61 sock = socket(AF_INET, SOCK_STREAM, 0);
63 if (sock == INVALID_SOCKET) {
64 opp_error(
"Error creating socket");
69 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (
char*)&on,
sizeof(on));
71 memset(&server, 0,
sizeof (server));
72 server.sin_family = AF_INET;
73 server.sin_addr.s_addr = htonl(INADDR_ANY);
74 server.sin_port = htons(appPort);
76 if (bind( sock, (
struct sockaddr*)&server,
sizeof( server)) < 0) {
77 opp_error(
"Error binding to app socket");
81 if (listen( sock, 5 ) == -1) {
82 opp_error(
"Error listening on app socket");
95 if (
netw_fd != INVALID_SOCKET) {
105 netw_fd = socket(AF_INET, SOCK_DGRAM, 0);
106 memset(&addr, 0,
sizeof(addr));
107 addr.sin_family = AF_INET;
109 cModule* overlay = simulation.getModuleByPath(
110 "SingleHostUnderlayNetwork.overlayTerminal[0].overlay");
112 if (overlay == NULL) {
113 throw cRuntimeError(
"UdpOutScheduler::initializeNetwork(): "
114 "Overlay module not found!");
117 addr.sin_port = htons(overlay->gate(
"appIn")->getNextGate()->
118 getOwnerModule()->par(
"localPort").longValue());
120 cModule* underlayConfigurator =
121 simulation.getModuleByPath(
"SingleHostUnderlayNetwork.underlayConfigurator");
123 if (underlayConfigurator == NULL) {
124 throw cRuntimeError(
"UdpOutScheduler::initializeNetwork(): "
125 "UnderlayConfigurator module not found!");
128 if (strlen(underlayConfigurator->par(
"nodeIP").stringValue())) {
129 addr.sin_addr.s_addr = htonl(IPAddress(underlayConfigurator->
130 par(
"nodeIP").stringValue()).getInt());
132 addr.sin_addr.s_addr = htonl(INADDR_ANY);
135 if (bind(
netw_fd, (sockaddr*)&addr,
sizeof(addr)) < 0) {
136 opp_error(
"Error binding to UDP socket");
150 dev =
new char[IFNAMSIZ];
153 opp_error(
"Already bound to application TUN device!");
157 if ((
apptun_fd = open(
"/dev/net/tun", O_RDWR)) < 0 ) {
158 opp_warning(
"Error opening application tun device");
161 ev <<
"[AppTunOutScheduler::initializeNetwork()]\n"
162 <<
"\t Successfully opened application TUN device"
166 memset(&ifr, 0,
sizeof(ifr));
173 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
174 strncpy(ifr.ifr_name,
"tun%d", IFNAMSIZ);
176 if((err = ioctl(
apptun_fd, TUNSETIFF, (
void *) &ifr)) < 0 ) {
178 opp_error(
"Error ioctl application tun device");
182 strncpy(
dev, ifr.ifr_name, IFNAMSIZ);
183 ev <<
"[AppTunOutScheduler::initializeNetwork()]\n"
184 <<
" Bound to device " <<
dev <<
"\n"
185 <<
" Remember to bring up application TUN device with "
186 <<
"ifconfig before proceeding"
196 sockaddr* from = (sockaddr*)
new sockaddr_in;
197 socklen_t addrlen =
sizeof(sockaddr_in);
201 if (new_sock == INVALID_SOCKET) {
202 opp_warning(
"Error connecting to remote app");
209 for (SOCKET fd = 0; fd <=
maxfd; fd++) {
212 if (FD_ISSET(fd, &
all_fds)) count++;
219 closesocket(new_sock);
223 ev <<
"[UdpOutScheduler::additionalFD()]\n"
224 <<
" Rejecting new app connection (FD: " << new_sock <<
")"
233 if (new_sock >
maxfd) {
243 ev <<
"[UdpOutScheduler::additionalFD()]\n"
244 <<
" Accepting new app connection (FD: " << new_sock <<
")"