#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
struct ip6_hdr;
struct udphdr;
int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_in6 serverAddr6;
socklen_t addr_size;
struct hostent *hp;
struct msghdr m;
struct in_addr **addr_list;
/*Create UDP socket*/
clientSocket = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
/*Configure settings in address struct*/
hp = gethostbyname2("2001:0db8:0:f101::2", PF_INET6);
memset((char *)&serverAddr6, 0, sizeof(serverAddr6));
memcpy((char *)&serverAddr6.sin6_addr, hp->h_addr, hp->h_length);
serverAddr6.sin6_family = hp->h_addrtype;
// serverAddr6.sin6_port = htons(7891);
serverAddr6.sin6_port = 0;//htons(IPPROTO_RAW);
/*Initialize size variable to be used later on*/
const size_t IPV6_HEADER_LEN = 40;
const size_t UDP_HEADER_LEN = 8;
addr_size = sizeof serverAddr6;
size_t data_len = sizeof(serverAddr6) - IPV6_HEADER_LEN;
/*Populate ip header*/
struct ip6_hdr *iphdr = (struct ip6_hdr *)buffer;
iphdr->ip6_flow = htonl ((6 << 28) | (0 << 20) | 0); // IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
iphdr->ip6_plen = data_len;
iphdr->ip6_nxt = IPPROTO_UDP;
iphdr->ip6_hops = 64;
//supposing src_addr and dst_addr pointers are never NULL
memcpy(&iphdr->ip6_src, (const in6_addr*)&serverAddr6.sin6_addr, sizeof(in6_addr));
memcpy(&iphdr->ip6_dst, (const in6_addr*)&serverAddr6.sin6_addr, sizeof(in6_addr));
/*Populate UDP header*/
udphdr * udp_header = reinterpret_cast<udphdr *>(buffer + IPV6_HEADER_LEN);
udp_header->source = htons(0);
udp_header->dest = htons(7891);
int len = sizeof(serverAddr6);
uint32_t data_length = (len - (IPV6_HEADER_LEN + UDP_HEADER_LEN));
udp_header->len = htons(data_length + sizeof(udphdr));
udp_header->check = htons(0);
for (int i=0; i<10; ++i)
{
sprintf(buffer, "%d", i);
nBytes = strlen(buffer) + 1;
/*Send message to server*/
if(sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr6,addr_size) == -1 )
{
printf("sendto(): %s\n", strerror(errno));
exit(0);
}
/*Receive message from server*/
// nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
// nBytes = recvmsg(clientSocket,&m,0);
if((nBytes = recvmsg(clientSocket,&m, 0)) == -1)
{
printf("recvmsg(): %s\n", strerror(errno));
// exit(0);
}
printf("Received from server: %s\n",buffer);
}
return 0;
}
I am pretty new to socket programming. I have the following code. It returns sendto(): Invalid argument Upon investigation, I have identified that the line containing: serverAddr6.sin6_port = htons(7891); *commented out in the code snippet With the help of Google, I have found a work around solution in http://osdir.com/ml/linux.ipv6.usagi.users/2003-03/msg00004.html :
From the kernel source code, it seems that when you use raw ipv6 socket, you have to set
dest.sin6_port = htons(IPPROTO_RAW);
and in http://osdir.com/ml/linux.ipv6.usagi.users/2003-03/msg00005.html :
dest.sin6_port=0;
On the other hand, I cannot go with this proposed solution as I have to specify the port to be something else other than 0.
Any suggestions or explanations?
I am using Python 3, and also encountering OSError: [Errno 22] Invalid argument
when sending IPv6 packages. However, when I set the port number of the parameter for sendto
to 0
, the packets are sent successfully and the port specified in the packet generated by the script ( buffer
in your program) is still used.
You should use different port numbers for generating the packet and calling sendto
. Generate with the desired port number and call with port number 0
.
The following is my code
s = socket.socket(AF_INET6, SOCK_RAW, IPPROTO_RAW)
s.setsockopt(IPPROTO_IPV6, IP_HDRINCL, 1)
s.sendto(GenerateIPv6Packet('::1', 53, '::1', 53), ('::1', 0))
# generate the packet with port 53 and call sendto with port 0
Sorry for answering in a different programming language.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.