sockets - Assigning IP address in IP Header in C -
I am implementing a chat room application using raw sockets for my college project in C. I have problem in specifying IP address I
is received from the packet socket as I have shown I am parsing it now how to specify IP address.
I want to do it as shown (using RCVIP as a four), if possible. So before suggesting to use struct in_addr , help me with this because I have tried other methods which are causing me other illusions. Now the ParseIPHeader function. thanks :) Hope you IP addresses String because you try to print one with the This means that an IPv4 address uses only 32 bits, while the text's representation can be greater than 3 + 1 + 3 + 1 + 3 + 1 + 3 = 15 characters (Plus terminator, therefore 16). Read on more IP fundamentals.
main () {char * rcv_ip; Four * packet; Int len; Struct iphdr ip_header; Rcv_ip = ParseIPHeader (packet, lane); Printf ("\ n% s", rcv_ip); // Printing some junk printfrog ("\ n% s", inet_addr (rcv_ip)); // give mistake print ("\ n% s", inet_ntoa (* ((struct in_addr *) ip_header-> daddr)); // Printing ip ip_header CreateIPHeader (rcv_ip + 5, rcv_ip); }
char * parse header (packet, lane) {struct iphdr * ip_header; Struct ethhdr * eth_header; Char * ret_ip; Ip_header = (Straight IPHR *) (Packet + Size (Stret Etherthur)); Ret_ip = malloc (10); Memset (ret_ip, 0, 10); memcpy (ret_ip, & amp; ip_header-> daddr), sizeof (ip_header-> daddr)); Memcpy (ret_ip + 5, & amp; ip_header- & gt; saddr), 4); Return ret_ip; } Struct iphdr * CreateIPHeader (char * src_ip, char * dst_ip) {struct iphdr * ip_header; Ip_header = malloc (formation (structure IPHDR)); //main part. how to do this?? Ip_header-> Saddr = inet_addr (src_ip); //please correct it. Ip_header-> Daddr = inet_addr (dst_ip); Return (ip_header); }
printf () and
% s format specifier. this is wrong; The IP addresses are binary in the header.
Comments
Post a Comment