/* Virginia Tech Cognitive Radio Open Source Systems * Virginia Tech, 2009 * * TODO LICENSE INFORMATION GOES HERE */ /* TODO DESCRIPTION OF FILE. */ #include #include #include #include #include #include #include #include #include #include "vtcross/common.h" #include "vtcross/containers.h" #include "vtcross/debug.h" #include "vtcross/error.h" #include "vtcross/socketcomm.h" // TODO can someone write a description of how this function is operating? I'm // not sure I understand why it is making two separate blocking calls to recv void ReadMessage(int32_t socketFD, char* msgBuffer) { ssize_t msgLength = recv(socketFD, msgBuffer, 256, MSG_PEEK); size_t i; for(i = 0; i < 256; i++) { if(strcmp(&msgBuffer[i], "\0") == 0) break; } msgLength = recv(socketFD, msgBuffer, i + 1, 0); if (msgLength < 0) ERROR(1, "Error reading from socket"); } int32_t ClientSocket(const char* serverName, const char* portNumber) { int32_t socketFD; int32_t portNo; struct sockaddr_in serv_addr; struct hostent *server; server = gethostbyname(serverName); if(server == NULL) ERROR(1, "No server found by that hostname."); portNo = atoi(portNumber); socketFD = socket(AF_INET, SOCK_STREAM, 0); if(socketFD < 0) ERROR(1, "Error opening socket"); memset((void *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portNo); memcpy((char *) &serv_addr.sin_addr.s_addr, (char *) server->h_addr, \ server->h_length); if(connect(socketFD, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) ERROR(1, "Error connecting to remote socket."); return socketFD; } /* TODO see notes in socketcomm.h. This function, and the RemoteComponent * struct, need a code review to make sure it is ready to be used rather * than ClientSocket() */ void CreateClientSocket(struct RemoteComponent* serverInfo) { if(serverInfo == NULL) ERROR(1, "CreateClientSocket received null struct pointer.\n"); struct hostent *server = gethostbyname(serverInfo->serverName.c_str()); if(server == NULL) ERROR(1, "No server found by that hostname."); serverInfo->socketFD = socket(AF_INET, SOCK_STREAM, 0); if(serverInfo->socketFD < 0) ERROR(1, "Error opening socket"); struct sockaddr_in serv_addr; memset((void *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(serverInfo->serverPort); memcpy((char *) &serv_addr.sin_addr.s_addr, (char *) server->h_addr, \ server->h_length); if(connect(serverInfo->socketFD, (struct sockaddr *) &serv_addr, \ sizeof(serv_addr)) < 0) ERROR(1, "Error connecting to remote socket."); } /* TODO I'm fairly certain this function is unnecessary, see function below for more details... int32_t SendMessage(int32_t socketFD, char* message) { // TODO explain this. What, exactly, does the below line do and how does it // affect the rest of the function? strcat(message, "\0000"); ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0); if(numSentBytes < 0) { ERROR(1, "Error sending to server."); } else if(numSentBytes == 0) { LOG("socket_comm::SendMessage - Server closed the socket.\n"); } return numSentBytes; } */ // TODO this function is here to handle calls to send const char* messages. Note // that the std::string.c_str() function auto-appends a null character at the // end of the cstring, so the strcat function call in the previous function // isn't necessary here... I think... although I still don't really understand // what exactly that call is for. int32_t SendMessage(int32_t socketFD, const char* message) { ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0); if(numSentBytes < 0) { ERROR(1, "Error sending to server."); } else if(numSentBytes == 0) { LOG("socket_comm::SendMessage - Server closed the socket.\n"); } return numSentBytes; } // TODO This function is currently returning 1... always... is this necessary? // If we want a fail/success return type, then why aren't we ever returning a // failure? int32_t GetParameter(int32_t socketFD, struct Parameter pList[], \ struct CE_Info *ce_info) { char buffer[256]; memset(buffer, 0, 256); ReadMessage(socketFD, buffer); ce_info->numParameters = atoi(buffer); LOG("socket_comm::GetParameter - Number of parameters: %d\n", \ ce_info->numParameters); for(size_t i = 0; i < ce_info->numParameters; i++) { memset(buffer, 0, 256); ReadMessage(socketFD, buffer); LOG("socket_comm::GetParameter - Name: %s\n", buffer); pList[i].name = std::string(buffer); memset(buffer, 0, 256); ReadMessage(socketFD, buffer); LOG("socket_comm::GetParameter - Units: %s\n", buffer); pList[i].units = std::string(buffer); memset(buffer, 0, 256); ReadMessage(socketFD, buffer); LOG("socket_comm::GetParameter - Min: %s\n", buffer); pList[i].min = atof(buffer); memset(buffer, 0, 256); ReadMessage(socketFD, buffer); LOG("socket_comm::GetParameter - Max: %s\n", buffer); pList[i].max = atof(buffer); memset(buffer, 0, 256); ReadMessage(socketFD, buffer); LOG("socket_comm::GetParameter - Step: %s\n", buffer); pList[i].step = atof(buffer); memset(buffer, 0, 256); ReadMessage(socketFD, buffer); LOG("socket_comm::GetParameter - Value: %s\n", buffer); pList[i].value = atof(buffer); } return 1; } // TODO if we are just returing fail/success here, then why not return a bool // instead of an entire 32 bit integer? Seems wasteful. int32_t GetRequest(int32_t socketFD, struct Parameter pList[], struct CE_Info *ce_info) { char buffer[256]; memset(buffer, 0, 256); ReadMessage(socketFD, buffer); if(strcmp(buffer, "val") != 0) { LOG("socket_comm::GetRequest - Unexpected control data received.\n\n"); return 0; } LOG("socket_comm::GetRequest - Getting parameters.\n\n"); GetParameter(socketFD, pList, ce_info); return 1; } int32_t AcceptTCPConnection(int32_t serverSock) { struct sockaddr_in echoClientAddr; uint32_t clientLength = sizeof(echoClientAddr); int32_t clientSocket = accept(serverSock, NULL, NULL); if(clientSocket < 0) ERROR(1, "Could not establish connection with client socket.\n"); LOG("Handling client %s\n", inet_ntoa(echoClientAddr.sin_addr)); return clientSocket; } int32_t CreateTCPServerSocket(uint16_t port) { struct sockaddr_in echoServerAddr; int32_t localSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (localSocket < 0) ERROR(1, "socket() failed\n"); /* Construct the local address structure */ memset(&echoServerAddr, 0, sizeof(echoServerAddr)); echoServerAddr.sin_family = AF_INET; echoServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); echoServerAddr.sin_port = htons(port); /* Bind to the local address */ if(bind(localSocket, (struct sockaddr *) &echoServerAddr, \ sizeof(echoServerAddr)) < 0) ERROR(1, "bind() failed\n"); /* Mark the socket so it will listen for incoming connections */ if(listen(localSocket, 5) < 0) ERROR(1, "listen() failed\n"); return localSocket; }