void Client(char *Address, char *Port, int AddressFamily, int TransportProtocol) { char ErrBuf[1024]; char DataBuffer[1024]; int ClientSocket; // keeps the socket ID for this connection struct addrinfo Hints; // temporary struct to keep settings needed to open the new socket struct addrinfo *AddrInfo; // keeps the addrinfo chain; required to open a new socket int WrittenBytes; // Number of bytes written on the socket // Prepare to open a new server socket memset(&Hints, 0, sizeof(struct addrinfo)); Hints.ai_family= AddressFamily; Hints.ai_socktype= TransportProtocol; // Open a TCP or UDP connection if (sock_initaddress (Address, Port, &Hints, &AddrInfo, ErrBuf, sizeof(ErrBuf)) == sockFAILURE) { printf("Error resolving given address/port: %s\n\n", ErrBuf); return; } printf("Trying to connect to server on address %s, port %s, using protocol %s\n", Address ? Address : "all local addresses", Port, (AddrInfo->ai_family == AF_INET) ? "IPv4" : "IPv6"); if ( (ClientSocket= sock_open(AddrInfo, 0, 0, ErrBuf, sizeof(ErrBuf))) == sockFAILURE) { // AddrInfo is no longer required sock_freeaddrinfo(AddrInfo); printf("Cannot opening the socket: %s\n\n", ErrBuf); return; } // AddrInfo is no longer required sock_freeaddrinfo(AddrInfo); printf("Connection established.\n"); printf("Type the string you want to send to the server: "); // Warning: possible buffer overflow here fgets(DataBuffer, sizeof(DataBuffer), stdin); // fgets reads also the newline character, so we have to reset it DataBuffer[strlen(DataBuffer) - 1]= 0; printf("\n\n"); WrittenBytes= sock_send(ClientSocket, DataBuffer, strlen(DataBuffer), ErrBuf, sizeof(ErrBuf)); if (WrittenBytes == sockFAILURE) { printf("Error sending data: %s\n\n", ErrBuf); return; } printf("String '%s' sent. Press ENTER to terminate the program\n", DataBuffer); fgets(DataBuffer, sizeof(DataBuffer), stdin); }
void *doControlConnection(void *parameters) { int AddressFamily = AF_INET; //use IPv4 int TransportProtocol = SOCK_STREAM; //use TCP char ErrBuf[1024]; char DataBuffer[1024]; int ChildSocket; // keeps the socket ID for connections from clients struct addrinfo Hints; // temporary struct to keep settings needed to open the new socket struct addrinfo *AddrInfo; // keeps the addrinfo chain; required to open a new socket struct sockaddr_storage From; // temp variable that keeps the parameters of the incoming connection int ReadBytes, WrittenBytes; int ServerSocket; // Prepare to open a new server socket memset(&Hints, 0, sizeof(struct addrinfo)); Hints.ai_family= AddressFamily; Hints.ai_socktype= TransportProtocol; // Open a TCP/UDP connection Hints.ai_flags = AI_PASSIVE; // This is a server: ready to bind() a socket if (sock_initaddress (NULL, nf_params.tcp_port, &Hints, &AddrInfo, ErrBuf, sizeof(ErrBuf)) == sockFAILURE) { fprintf(logFile,"%s Error resolving given port (%s): %s\n",module_name, nf_params.tcp_port,ErrBuf); exit(EXIT_FAILURE); } if ( (ServerSocket= sock_open(AddrInfo, 1, 10, ErrBuf, sizeof(ErrBuf))) == sockFAILURE) { // AddrInfo is no longer required sock_freeaddrinfo(AddrInfo); fprintf(logFile,"%s Cannot opening the socket: %s\n",module_name, ErrBuf); exit(EXIT_FAILURE); } // AddrInfo is no longer required sock_freeaddrinfo(AddrInfo); while(1) { if ( (ChildSocket= sock_accept(ServerSocket, &From, ErrBuf, sizeof(ErrBuf))) == sockFAILURE) { fprintf(logFile,"%s Error when accepting a new connection: %s\n",module_name, ErrBuf); exit(EXIT_FAILURE); } ReadBytes= sock_recv(ChildSocket, DataBuffer, sizeof(DataBuffer), SOCK_RECEIVEALL_NO, 0/*no timeout*/, ErrBuf, sizeof(ErrBuf)); if (ReadBytes == sockFAILURE) { fprintf(logFile,"%s Error reading data: %s\n",module_name, ErrBuf); exit(EXIT_FAILURE); } // Terminate buffer, just for printing purposes // Warning: this can originate a buffer overflow DataBuffer[ReadBytes]= 0; fprintf(logFile,"%sData received (%d bytes):\n",module_name, ReadBytes); fprintf(logFile,"%s %s\n",module_name,DataBuffer); char answer[1024]; sprintf(answer,"Greeting from network function\"%s\"",nf_params.nf_name); fprintf(logFile,"%s Answer to be sent: %s\n",module_name,answer); WrittenBytes= sock_send(ChildSocket, answer, strlen(answer), ErrBuf, sizeof(ErrBuf)); if (WrittenBytes == sockFAILURE) { fprintf(logFile,"%s Error sending data: %s",module_name, ErrBuf); exit(EXIT_FAILURE); } sock_close(ChildSocket,ErrBuf,sizeof(ErrBuf)); } }
void Server(char *Address, char *Port, int AddressFamily, int TransportProtocol) { char ErrBuf[1024]; char DataBuffer[1024]; int ServerSocket, ChildSocket; // keeps the socket ID for this connection struct addrinfo Hints; // temporary struct to keep settings needed to open the new socket struct addrinfo *AddrInfo; // keeps the addrinfo chain; required to open a new socket struct sockaddr_storage From; // temp variable that keeps the parameters of the incoming connection int ReadBytes; // Number of bytes read from the socket char RemoteAddress[1024]; // temp variable to store the address of the connecting host char RemotePort[1024]; // temp variable to store the port used by the connecting host // Prepare to open a new server socket memset(&Hints, 0, sizeof(struct addrinfo)); Hints.ai_family= AddressFamily; Hints.ai_socktype= TransportProtocol; // Open a TCP/UDP connection Hints.ai_flags = AI_PASSIVE; // This is a server: ready to bind() a socket if (sock_initaddress (Address, Port, &Hints, &AddrInfo, ErrBuf, sizeof(ErrBuf)) == sockFAILURE) { printf("Error resolving given address/port: %s\n\n", ErrBuf); return; } printf("Server waiting on address %s, port %s, using protocol %s\n", Address ? Address : "all local addresses", Port, (AddrInfo->ai_family == AF_INET) ? "IPv4" : "IPv6"); if ( (ServerSocket= sock_open(AddrInfo, 1, 10, ErrBuf, sizeof(ErrBuf))) == sockFAILURE) { // AddrInfo is no longer required sock_freeaddrinfo(AddrInfo); printf("Cannot opening the socket: %s\n\n", ErrBuf); return; } // AddrInfo is no longer required sock_freeaddrinfo(AddrInfo); if (TransportProtocol == SOCK_STREAM) { if ( (ChildSocket= sock_accept(ServerSocket, &From, ErrBuf, sizeof(ErrBuf))) == sockFAILURE) { printf("Error when accepting a new connection: %s\n\n", ErrBuf); return; } if (sock_getascii_addrport(&From, RemoteAddress, sizeof(RemoteAddress), RemotePort, sizeof(RemotePort), NI_NUMERICHOST | NI_NUMERICSERV, ErrBuf, sizeof(ErrBuf)) == sockFAILURE) { printf("Error getting information related to the connecting host: %s\n\n", ErrBuf); return; } printf("Accepting a new connection from host %s, using remote port %s.\n\n", RemoteAddress, RemotePort); ReadBytes= sock_recv(ChildSocket, DataBuffer, sizeof(DataBuffer), SOCK_RECEIVEALL_NO, 30, ErrBuf, sizeof(ErrBuf)); if (ReadBytes == sockFAILURE) { printf("Error reading data: %s\n\n", ErrBuf); return; } } else { ReadBytes= sock_recvdgram(ServerSocket, DataBuffer, sizeof(DataBuffer), SOCK_RECEIVEALL_NO, 30, ErrBuf, sizeof(ErrBuf)); if (ReadBytes == sockFAILURE) { printf("Error reading data: %s\n\n", ErrBuf); return; } } if (ReadBytes == sockWARNING) { printf("We waited for enough time and no data has been received so far.\nAborting the connection.\n\n"); return; } // Terminate buffer, just for printing purposes // Warning: this can originate a buffer overflow DataBuffer[ReadBytes]= 0; printf("Received the following string: '%s'\n\n", DataBuffer); }