//==================================================== file = rawServer.c ===== //= A message "server" program to demonstrate raw sockets programming = //============================================================================= //= Notes: = //= 1) This program compiles only for Winsock = //= 2) This program serves a message to program rawClient running on = //= another host. = //= 3) For Windows 7 needs to run in an elevated command window (cmd) = //=---------------------------------------------------------------------------= //= Example execution: (rawServer and rawClient running on host 127.0.0.1) = //= Waiting for recvfrom() to complete... = //= IP address of client = 127.0.0.1 = //= Received from client: 4500003755ae000080ff00007f0000017f000001 | Test = //= message from CLIENT to SERVER = //=---------------------------------------------------------------------------= //= Build: bcc32 rawClient.c ws2_32.lib or cl rawClient.c ws2_32.lib = //=---------------------------------------------------------------------------= //= Execute: rawServer = //=---------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=---------------------------------------------------------------------------= //= History: KJC (03/26/17) - Genesis (from udpServer.c and rawsend.c) = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for memcpy() and strcpy() #include // Needed for exit() #include // Needed for all Winsock stuff //===== Main program ========================================================== int main() { WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions int server_s; // Server socket descriptor struct sockaddr_in server_addr; // Server Internet address struct sockaddr_in client_addr; // Client Internet address struct in_addr client_ip_addr; // Client IP address int addr_len; // Internet address length unsigned char out_buf[4096]; // Output buffer for data unsigned char in_buf[4096]; // Input buffer for data int retcode; // Return code int i; // Loop index // This stuff initializes winsock WSAStartup(wVersionRequested, &wsaData); // Create a raw socket server_s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); if (server_s < 0) { printf("*** ERROR - socket() failed \n"); exit(-1); } // Fill-in my socket's address information server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address retcode = bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (retcode < 0) { printf("*** ERROR - bind() failed \n"); exit(-1); } // Wait to receive a message from client printf("Waiting for recvfrom() to complete... \n"); addr_len = sizeof(client_addr); retcode = recvfrom(server_s, in_buf, sizeof(in_buf), 0, (struct sockaddr *)&client_addr, &addr_len); if (retcode < 0) { printf("*** ERROR - recvfrom() failed \n"); exit(-1); } // Copy the four-byte client IP address into an IP address structure memcpy(&client_ip_addr, &client_addr.sin_addr.s_addr, 4); // Print an informational message of IP address of the client printf("IP address of client = %s \n", inet_ntoa(client_ip_addr)); // Output the received message (IP header and then payload) printf("Received from client: "); for (i=0; i<20; i++) printf("%02x", in_buf[i]); printf(" | "); for(i=20; i