sendmsg : Invalid Argument
Hi,
I m new to Linux and socket programming in IPv6,please check out the code. I know the error which is w.r.t Memory allocation. The code snippet is:
int main()
{
sockfd = socket(PF_INET6,SOCK_RAW,IPPROTO_RAW);
struct sockaddr_in6 sin;
********
// Inserted the IPv6 address along with the other options in the above structure
setsockopt(sockfd,IPPROTO_IPV6,IPV6_PKTINFO,&one,s izeof(int))
struct in6_pktinfo ip;
memset(&ip,0,sizeof(ip));
memcpy((char *)ip.ipi6_addr.s6_addr16,(char *)sin.sin6_addr.s6_addr16,sizeof(struct
in6_addr));
ip.ipi6_ifindex = 0;//htonl(if_nametoindex("eth0"));
struct iovec io[1];
struct msghdr hdr;
memset(&io,0,sizeof(struct iovec));
memset(&hdr,0,sizeof(hdr) );
#if 1
struct cmsghdr *cmsg;
int cmsglen = CMSG_SPACE(sizeof(ip));
cmsg = malloc(cmsglen);
if(cmsg == NULL)
{
printf("CMSG ALLOC Failed");
return -ENOMEM;
}
memset(cmsg,0,cmsglen);
hdr.msg_control = cmsg;
hdr.msg_controllen =CMSG_SPACE( cmsglen);
#endif
io[0].iov_base =(char *) msg;
io[0].iov_len = sizeof(msg);
hdr.msg_name = (void *)&sin;
hdr.msg_namelen = sizeof(struct sockaddr_in6);
hdr.msg_iov = io;
hdr.msg_iovlen = 1;
hdr.msg_flags =0;
int kl;
while(1)
{
if ((kl = sendmsg(sockfd,&hdr,0)) < 0)
{
// close(sockfd);
printf("Client : Error in sendto() : %s \n",strerror(errno));
}
else
{
printf(" %d ",kl,strerror(errno));
printf(".");
}
fflush(stdout);
sleep(1);
}
return 0;
}
This is the code which when run is returning invalid argument error ,
So any help in this regard will be appreciated
regards,
Suresh K
|