Source: stackoverflow.com --- Tuesday, January 31, 2017
In my client socket code, I need to get IP layer header for the receiving packet for UDP data grams. So I decided to change the socket to RAW using below system call: int optval=1; WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2),&wsaData) == SOCKET_ERROR) { int err = WSAGetLastError(); my_err("WSAStartup error %d\n",err); return 0; } if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == INVALID_SOCKET) { perror("socket()"); return 0; } if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof optval) != 0) { perror("setsockopt(IPPROTO_IP,IP_HDRINCL)"); return 0; } An then I send first data using: //content is a structure containing data and its length uint16_t all_length = content->len; size_t IP_HL = sizeof (IPHEADER); //get_interface_ip returns interface ip address uint32_t src_addr = get_interface_ip(); IPHEADER *iph = NULL; size_t all_length = IP_HL + sizeof (UDPHEADER) + total_len; UDPHEADER *udph = NULL; BUFFER buf; make_buf(&buf, 0); iph = (IPHEADER *) BPTR(&buf); iph->ip_hl = 5; iph->ip_v = 4; iph->ip_tos = 0; iph->ip_len = all_length; //get_uniq_id creates ip id field iph->ip_id = htonl (get_uniq_id()); //Id of this packet iph->ip_off = 64; //DONT fragment iph->ip_ttl = 255; iph->ip_p = IPPROTO_UDP; iph->ip_sum = 0; //Set to 0 before calculating checksum iph->ip_src = src_addr; iph->ip_dst = remote.sin_addr.s_addr; iph->ip_sum = csum((uint16_t *)iph, iph->ip_len); udph = (UDPHEADER *) (iph + 1); memcpy((udph ...
from Windows http://ift.tt/2jRxIXr
No comments:
Post a Comment