//==================================================== file = rawClient.c ===== //= A message "client" program to demonstrate raw sockets programming = //============================================================================= //= Notes: = //= 1) This program compiles only for Winsock = //= 2) This program needs rawServer to be running on another host. = //= Program rawServer must be started first. = //= 3) This program assumes that the IP address of the host running = //= rawServer is defined in "#define IP_ADDR" = //= 4) For Windows 7 needs to run in an elevated command window (cmd) = //= 5) If run locally (host to host on one machine) then rawClient will = //= receive both the packet it sends (to rawServer) and the reply from = //= rawServer. See the code block labeled as "DEBUG". = //=---------------------------------------------------------------------------= //= Example execution: (rawServer and rawClient running on host 127.0.0.1) = //= Received from server: 4500003755ae000080ff00007f0000017f000001 | Test = //= message from CLIENT to SERVER = //= Received from server: 4500004255af000080ff00007f0000017f000001 | This = //= is a reply message from SERVER to CLIENT = //=---------------------------------------------------------------------------= //= Build: bcc32 rawClient.c ws2_32.lib or cl rawClient.c ws2_32.lib = //=---------------------------------------------------------------------------= //= Execute: rawClient = //=---------------------------------------------------------------------------= //= 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 udpClient.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 //----- Defines --------------------------------------------------------------- #define PORT_NUM 1050 // Port number used #define IP_ADDR "127.0.0.1" // IP address of server1 (*** HARDWIRED ***) //===== Main program ========================================================== int main() { WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions int client_s; // Client socket descriptor struct sockaddr_in server_addr; // Server Internet 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 client_s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); if (client_s < 0) { printf("*** ERROR - socket() failed \n"); exit(-1); } // Fill-in server socket address information server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use // Assign a message to buffer out_buf strcpy(out_buf, "Test message from CLIENT to SERVER"); // Now send the message to server. The "+ 1" is for the end-of-string // delimiter retcode = sendto(client_s, out_buf, (strlen(out_buf) + 1), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (retcode < 0) { printf("*** ERROR - sendto() failed \n"); exit(-1); } // Wait to receive a message addr_len = sizeof(server_addr); retcode = recvfrom(client_s, in_buf, sizeof(in_buf), 0, (struct sockaddr *)&server_addr, &addr_len); if (retcode < 0) { printf("*** ERROR - recvfrom() failed \n"); exit(-1); } // Output the received message (IP header and then payload) printf("Received from server: "); for (i=0; i<20; i++) printf("%02x", in_buf[i]); printf(" | "); for(i=20; i