/** * @file PolicyServer.cpp * @authir Gadi Srebnik * * Main Policy Server file (Windows winsock2 version). Get host and port from configuration * file and uses crossdomain.xml file to send to end user. * Has 2 versions of host binding: localhost and ip from config file. * 2 files should be in exe directory: * conf.txt * crossdomain.xml * Use freely and dont forget to share. */ #include "stdafx.h" #include #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int port; char host[80]; // get port number from configuration files ifstream inConf("conf.txt"); if(!inConf) { cout << "Cannot open conf.txt file.\n"; return -1; } char item[200]; char * pch; // set port inConf.getline(item, 80); pch = strtok (item,"="); pch = strtok (NULL, "="); port = atoi(pch); // set host inConf.getline(item, 80); pch = strtok (item,"="); pch = strtok (NULL, "="); strcpy(host, pch); //inConf >> item; inConf.close(); // get crossdomain.xml content ifstream inCrossDomain("crossdomain.xml"); if(!inCrossDomain) { cout << "Cannot open crossdomain.xml file.\n"; return -1; } char data[1024], *str; int len; inCrossDomain.read(data, 1024); int begin = inCrossDomain.tellg(); inCrossDomain.seekg (0, ios::end); int end = inCrossDomain.tellg(); len = end - begin; inCrossDomain.close(); str = new char[len + 1]; strncpy(str, data, len); // Initialize WinSock2.2 DLL // low word = major, highword = minor WSADATA wsaData = {0}; WORD wVer = MAKEWORD(2,2); int nRet = WSAStartup( wVer, &wsaData ); if( nRet == SOCKET_ERROR ) { // WSAGetLastError() cout << "Failed to init Winsock library" << endl; return -1; } cout << "Starting PolicyServer by Gadi Srebnik" << endl; cout << "Listening for connections" << endl; // name a socket WORD WSAEvent = 0; WORD WSAErr = 0; // open a socket // // for the server we do not want to specify a network address // we should always use INADDR_ANY to allow the protocal stack // to assign a local IP address SOCKET hSock = {0}; hSock = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ); if( hSock == INVALID_SOCKET ) { cout << "Invalid socket, failed to create socket" << endl; return -1; } // name socket sockaddr_in saListen = {0}; // localhost version struct hostent *hp; hp = gethostbyname("localhost"); saListen.sin_family = hp->h_addrtype; saListen.sin_port = htons(port); // host from config file version //saListen.sin_family = PF_INET; //saListen.sin_addr.s_addr = inet_addr(host); //htonl( INADDR_ANY ); // bind socket's name nRet = bind( hSock, (sockaddr*)&saListen, sizeof(sockaddr) ); if( nRet == SOCKET_ERROR ) { cout << "Failed to bind socket" << endl; //shutdown( hSock ); closesocket( hSock ); return -1; } while( true ) { // listen nRet = listen( hSock, 5 ); // connection backlog queue set to 10 if( nRet == SOCKET_ERROR ) { int nErr = WSAGetLastError(); if( nErr == WSAECONNREFUSED ) { cout << "Failed to listen, connection refused" << endl; } else { cout << "Call to listen failed" << endl; } closesocket( hSock ); return -1; } // connect sockaddr_in saClient = {0}; int nSALen = sizeof( sockaddr ); SOCKET hClient = accept( hSock, (sockaddr*)&saClient, &nSALen ); if( hClient == INVALID_SOCKET ) { cout << "Invalid client socket, connection failed" << endl; closesocket( hSock ); return -1; } char bRead[256]; int n; //bzero(bRead, 256); n = recv(hClient, bRead, 255, 0); if (n < 0) { cout << "ERROR reading from socket"; } if(strstr(bRead, "")) { //printf("Here is the message: %s",bRead); //read(i, buf, BUFLEN); // process data str[len - 1] = '\0'; int nData = send( hClient, str, len, 0 ); if( nData == SOCKET_ERROR ) { cout << "Error sending data" << endl; break; } // close client connection closesocket( hClient ); hClient = 0; } } return 0; }