LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-10-2010, 08:55 PM   #1
gr1980
LQ Newbie
 
Registered: Sep 2010
Posts: 5

Rep: Reputation: 0
sendto() returning -1 - Invalid argument


Hello,

I am trying to write a program that sends a UDP packet over the network. When I am using the sendto function it is returning with -1 and errno "Invalid argument". I am using the cast to make sure arg 5 is being passed in as a pointer to a sockaddr struct.

I have looked at lots of examples and my code seems to follow what the examples have so I am not sure what is wring here. I am a newbie so any pointers would be great!

STATUS send_request(const packet_t * pkt)
{

int fd = -1; /* socket filedescriptor */
int result = 0; /* sendto status */
struct sockaddr_in sa; /* structure for handling internet addressing */

/* create UDP socket PF_PACKET for post 2.0 kernel */
fd = socket(PF_PACKET, SOCK_DGRAM, IPPROTO_UDP);
if (-1 == fd)
{
/* error case */
printf ("%s:%d socket failed %s", __FUNCTION__, __LINE__, strerror(errno) );
return FAILED;
}

/* setup sockaddr_in struct */
memset((char *) &sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(SEND_PORT);
/* if (0 == inet_aton(SVR_IP, &sa.sin_addr))
{
// error case
printf ("%s:%d inet_aton failed", __FUNCTION__, __LINE__ );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}
*/

/* send packet */
printf ("%s:%d sendto fd:%d sizeof(packet_t):%d sizeof(pkt):%d", __FUNCTION__, __LINE__,fd, sizeof(packet_t), sizeof(pkt));

result = sendto(fd, pkt, sizeof(packet_t), 0, (struct sockaddr *)&sa, sizeof(sa));
if(-1 == result)
{
/* error case */
printf ("%s:%d sendto failed %d : %s", __FUNCTION__, __LINE__,result, strerror(errno) );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}
else if (sizeof(packet_t) == result)
{
/* success case */
printf ("%s:%d sendto successful", __FUNCTION__, __LINE__ );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
return FAILED;
}
}
else
{
/* catch all */
printf ("%s:%d sendto %d : %s", __FUNCTION__, __LINE__,result, strerror(errno) );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}

return SUCCESS;
}
 
Old 09-11-2010, 12:32 AM   #2
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Hi -

As far as I can tell, the only reason you'd be getting an error in "sendto()" is because you failed to assign an IP address. Here's your code, slightly reformatted, and using code blocks:
Code:
STATUS 
send_request(const char * data, int len)
{

  int fd = -1; /* socket filedescriptor */
  int result = 0; /* sendto status */
  struct sockaddr_in sa; /* structure for handling internet addressing */

  /* create UDP socket PF_PACKET for post 2.0 kernel */
  fd = socket(PF_PACKET, SOCK_DGRAM, IPPROTO_UDP);
  if (-1 == fd)
  {
    printf ("%s:%d socket failed %s", __FUNCTION__, __LINE__, strerror(errno) );
    return FAILED;
  }

  /* setup sockaddr_in struct */
  memset((char *) &sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(SEND_PORT);
  /* ??? sa.sin_addr.s_addr ??? */

  /* send packet */
  printf ("%s:%d sendto fd:%d sizeof(packet_t):%d sizeof(pkt):%d",
    __FUNCTION__, __LINE__,fd, sizeof(packet_t), sizeof(pkt));
  result = sendto(fd, data, len, 0, (struct sockaddr *)&sa, sizeof(sa));
  if(-1 == result)
  {
    ...
 
Old 09-11-2010, 10:13 AM   #3
gr1980
LQ Newbie
 
Registered: Sep 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you for your response. I tried adding the IP but still see the same problem. Here are the code change:

Code:
if (0 == inet_aton("255.255.255.255", &sa.sin_addr))
{
  // error case 
  printf (logbuf, sizeof(logbuf), "%s:%d inet_aton failed", __FUNCTION__, __LINE__ );
  if (-1 == (close(fd)))
  {
    printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
  }
  return FAILED;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sendto failed -1 : Invalid argument gr1980 Linux - Newbie 1 09-11-2010 02:01 AM
Please Help me : Sendto: Invalid argument PF_PACKET SOCK_DGRAM htons(ETH_P_IP) tuxtuxtux Linux - Networking 3 03-24-2010 06:08 AM
sendto: Invalid argument netbsd quantt Programming 3 02-14-2009 02:29 AM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM
sendto: invalid argument Yury Programming 12 11-04-2006 06:06 AM


All times are GMT -5. The time now is 11:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration