Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with sockets and sendto()

Status
Not open for further replies.

lughbh

Programmer
Sep 12, 2003
2
CA
Hi,
I have a short function which is trying to send an
icmp message out a raw ipv6 socket.

when I run the following code I get "wrote -1"...
i.e. sendto() is returning an error. Can someone
please tell me what I'm doing wrong?
-----
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/icmp6.h>

int main() {
int sockfd,len,out;
struct sockaddr_in6 myDst;
struct icmp6_hdr *hdr;
char sendbuff[1500]=&quot;abcdefghijklmnop&quot;;

out=0;

sockfd=socket(AF_INET6,SOCK_RAW,IPPROTO_ICMPV6);
if( !inet_pton(AF_INET6,'fed0:10:100::1&quot;,&myDst) )
printf(&quot;bad address\n&quot;);

hdr=(struct icmp6_hdr *)sendbuff;
hdr->icmp6_type=ND_ROUTER_ADVERT; //don't care about type... just want to send!
hdr->icmp6_code=0;
hdr->icmp6_cksum=0;
hdr->icmp6_seq=htons(6); //dummy val
hdr->icmp6_id=htons(8);//dummy val

out=sendto(sockfd, (char *) sendbuff, 8, 0, (struct sockaddr *) &myDst, sizeof(myDst));
printf(&quot;wrote %d\n&quot;,out);

return 1;
}
-----
thank-you so much!!

 
Try printing the error message too, that would probably help you to diagnose the problem further.

//Daniel
 
I get
22: Invalid argument

I was suspecting whether I was passing a pointer
wrong... maybe you can point it out?

sendto (int s, const void *msg, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)

... myDst is not a pointer, so I have to pass it's
location to satisfy sendto(). maybe my flag value
shouldn't be 0, but there are examples in the book
where it is 0.
 
sendto() sets errno to EINVAL when it doesn't like one of the specified lengths. The man page on my HP/UX box indicates the minimum/maximum message length varies by protocol.

Try increasing the message you're sending from 8 bytes to 60 bytes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top