PortGenerator.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: PortGenerator.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #ifndef WINCE 00045 #include <sys/types.h> 00046 #endif 00047 #ifndef WIN32 00048 #include <sys/socket.h> 00049 #include <sys/select.h> 00050 #include <netinet/in.h> 00051 #include <netdb.h> 00052 #include <unistd.h> 00053 #endif 00054 00055 #ifdef WIN32 00056 #include <winsock2.h> 00057 #endif 00058 00059 00060 #include <stdlib.h> 00061 #include <string.h> 00062 00063 #include "PortGenerator.hpp" 00064 00065 00066 00067 PortGenerator::PortGenerator() { 00068 initialize(); 00069 } 00070 00071 00072 PortGenerator::~PortGenerator() { 00073 } 00074 00075 00076 00077 /***************************************************************/ 00078 void PortGenerator::initialize() { 00079 struct timeval tv; 00080 00081 gettimeofday(&tv,0); 00082 srand(tv.tv_usec); 00083 memset(UDPsocketOf,0,sizeof(int)*PORT_RANGE); 00084 memset(TCPsocketOf,0,sizeof(int)*PORT_RANGE); 00085 } 00086 00087 00088 /***************************************************************/ 00089 int PortGenerator::setSockAndPort(int port, int proto) { 00090 struct sockaddr_in sin; 00091 int s; 00092 bool isUDP = (proto==PROT_UDP); 00093 if(port<PORT_OFFS || port>=(PORT_OFFS+PORT_RANGE)) //ignore this 00094 return 0; 00095 00096 if ((s = socket(PF_INET, isUDP?SOCK_DGRAM:SOCK_STREAM, 0)) < 0) { 00097 dprintf_err("PortGenerator::getNextFreePort: socket"); 00098 exit(1); 00099 } 00100 00101 /* Build address data structure */ 00102 memset((char *)&sin,0,sizeof(sin)); 00103 00104 sin.sin_family = AF_INET; 00105 sin.sin_addr.s_addr = INADDR_ANY; 00106 sin.sin_port = htons(port); 00107 00108 //find a systemwide unassigned port 00109 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 00110 #ifdef WIN32 00111 ::closesocket(s); 00112 #else 00113 close(s); 00114 #endif 00115 return 0; 00116 } 00117 00118 isUDP?(UDPsocketOf[port-PORT_OFFS] = s):(TCPsocketOf[port-PORT_OFFS] = s); 00119 00120 return s; 00121 } 00122 00123 /***************************************************************/ 00124 int PortGenerator::getNextFreePortPair(int proto) { 00125 int port; 00126 int s1,s2; 00127 bool isUDP = (proto==PROT_UDP); 00128 int ret; 00129 00130 00131 do { 00132 //find unassigned port from this PortGenerator 00133 do { 00134 port = (rand()%(PORT_RANGE/2)) * 2; //find an even port 00135 } while ( (isUDP? (UDPsocketOf[port]!=0):(TCPsocketOf[port]!=0)) || 00136 (isUDP? (UDPsocketOf[port+1]!=0):(TCPsocketOf[port+1]!=0))); 00137 port+=PORT_OFFS; 00138 if ( (ret = s1 = setSockAndPort(port,proto)) != 0) { 00139 ret = s2 = setSockAndPort(port+1,proto); 00140 if (ret == 0) { //odd port failed 00141 closePort(port,proto); //close even port 00142 } 00143 } 00144 00145 } while (ret == 0); 00146 00147 dprintf_full("PortGenerator::getNextFreePort bound port %i and %i to sock %i and %i\n",port,port+1,s1,s2); 00148 00149 00150 return port; 00151 } 00152 00153 00154 /***************************************************************/ 00155 bool PortGenerator::closePort(int port, int proto) { 00156 bool isUDP = (proto==PROT_UDP); 00157 if(port<PORT_OFFS || port>=(PORT_OFFS+PORT_RANGE)) //ignore this 00158 return true; 00159 00160 if ((isUDP?UDPsocketOf[port-PORT_OFFS]:TCPsocketOf[port-PORT_OFFS]) != 0) { 00161 #ifdef WIN32 00162 ::closesocket(isUDP?UDPsocketOf[port-PORT_OFFS]:TCPsocketOf[port-PORT_OFFS]); 00163 #else 00164 close(isUDP?UDPsocketOf[port-PORT_OFFS]:TCPsocketOf[port-PORT_OFFS]); 00165 #endif 00166 isUDP?UDPsocketOf[port-PORT_OFFS]=0:TCPsocketOf[port-PORT_OFFS]=0; 00167 return true; 00168 } 00169 00170 return false; 00171 } 00172 00173 00174 /***************************************************************/ 00175 bool PortGenerator::closePortPair(int port, int proto) { 00176 return (closePort(port,proto) & closePort(port+1,proto)); 00177 } 00178 00179 00180 /***************************************************************/ 00181 int PortGenerator::getSockFromPort(int port, int proto) { 00182 00183 bool isUDP = (proto==PROT_UDP); 00184 if(port<PORT_OFFS || port>=(PORT_OFFS+PORT_RANGE)) //ignore this 00185 return 0; 00186 00187 return (isUDP?UDPsocketOf[port-PORT_OFFS]:TCPsocketOf[port-PORT_OFFS]); 00188 }