www.pudn.com > stund_0.92_Jun06.rar > tlsServer.cxx
/* This program takes no arguments. It opens the STUN tcp port and rus a tls server on it. */ #include#include #include #include #include #include #include #include #include #include #include #ifdef WIN32 #include #include #include #endif #ifndef WIN32 #include #include #include #include #include #include #include #endif #ifdef WIN32 # include # include # include # include # include typedef unsigned int u_int32_t; #endif #include #include #include #include #include #ifdef __MACH__ typedef int socklen_t; #endif #include "stun.h" using namespace std; #define MAX_CONNECTIONS 64 #ifdef WIN32 typedef int socklen_t; //#define errno WSAGetLastError() typedef SOCKET Socket; #else typedef int Socket; static const Socket INVALID_SOCKET = -1; static const int SOCKET_ERROR = -1; #endif // TODO - !cj! - need to deal with closing connections void makeSocketNonBlocking(Socket fd) { #if WIN32 unsigned long noBlock = 1; int errNoBlock = ioctlsocket( fd, FIONBIO , &noBlock ); if ( errNoBlock != 0 ) { assert(0); } #else int flags = fcntl( fd, F_GETFL, 0); int errNoBlock = fcntl(fd, F_SETFL, flags | O_NONBLOCK ); if ( errNoBlock != 0 ) // !cj! I may have messed up this line { assert(0); } #endif } int main() { #ifdef WIN32 WORD wVersionRequested = MAKEWORD( 2, 2 ); WSADATA wsaData; int err; err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { // could not find a usable WinSock DLL //cerr << "Could not load winsock" << endl; assert(0); // is this is failing, try a different version that 2.2, 1.0 or later will likely work exit(1); } #endif // contexts for each connection SSL* ssl[MAX_CONNECTIONS]; // buffers for each connection BIO* bio[MAX_CONNECTIONS]; // file descriptors for each connection Socket fd[MAX_CONNECTIONS]; // ip address of other side of connection int peerip[MAX_CONNECTIONS]; // root cert list X509_STORE* certAuthorities; // my public cert X509* publicCert; // my private key EVP_PKEY* privateKey; // SSL Context SSL_CTX* ctx; Socket mFd; char* password = "password"; for ( int i=0; i 0 ) { FD_SET(fd[i], &read); size = ( int(fd[i]+1) > size) ? int(fd[i]+1) : size; } } // do a select unsigned long ms = 500; struct timeval tv; tv.tv_sec = (ms/1000); tv.tv_usec = (ms%1000)*1000; int e = select(size, &read, NULL, NULL, &tv); cerr << "." ; // process any new connections if ( FD_ISSET(mFd, &read)) { cerr << "Got a new connection" << endl; // find an unused connection int i=0; for ( ; i = MAX_CONNECTIONS ) { cerr << "Ran out of connections to use "< . * */ // Local Variables: // mode:c++ // c-file-style:"ellemtel" // c-file-offsets:((case-label . +)) // indent-tabs-mode:nil // End: