recvfrom returns with an error code of 14, EFAULT "Bad Address"

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Chris Ritch » Sat, 05 Jul 2003 01:20:03



I'll post the code at the bottom of the post.  Whenever I try to
retreive a udp packet sent via broadcast I get the errorcode
EFAULT(bad address). The same program sends out the packets without
any complaints but it fails when retreiving, I'm lost as to why it
would report this error. It returns the number of bytes correctly but
returns the char* buffer as NULL. Any help would be appriciated and I
wouldn't be supprised if I was just missing something simple with
broacasting as I am still learning some aspects of socket
programiing.Thanks in advance to all whom reply and here is the code:

int Communications::GetBroadcastSocket()
{
    int toReturn;
    int on = 1;     //  flag to turn on socket options

    memset(&broadcast, 0, sizeof(broadcast));
    broadcast.sin_family      = AF_INET;
    broadcast.sin_port        = PORTNUM + 1;
    broadcast.sin_addr.s_addr = inet_addr(BCAST_ADDR);

    if((toReturn = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1;
    if( SetNonBlocking(toReturn) < 0 ||
        setsockopt(toReturn, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0 ||
        setsockopt(toReturn, SOL_SOCKET, SO_BROADCAST, &on,
sizeof(on)) < 0 ||
        bind(toReturn, (sockaddr *) &broadcast, sizeof(broadcast)) <
0)
    {
        close(toReturn);
        return -1;
    }

    return toReturn;

Quote:}

 
 
 

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Barry Margoli » Sat, 05 Jul 2003 02:29:36




>I'll post the code at the bottom of the post.  Whenever I try to

It looks like you didn't post the correct code, I don't see any calls to
recvfrom() in the excerpt.

Quote:>retreive a udp packet sent via broadcast I get the errorcode
>EFAULT(bad address). The same program sends out the packets without

Sounds like you passed an uninitialized pointer as the buffer argument
argument.

Quote:>any complaints but it fails when retreiving, I'm lost as to why it
>would report this error. It returns the number of bytes correctly but

If it returns the number of bytes, then how can it also be reporting an
error code?  Errno is only meaningful if recvfrom() returns -1.

Quote:>returns the char* buffer as NULL. Any help would be appriciated and I

I don't understand this at all.  Buffer is an input parameter, not an
output parameter.  You have to pass in the pointer to the place where you
want the data written.

Quote:>wouldn't be supprised if I was just missing something simple with
>broacasting as I am still learning some aspects of socket
>programiing.Thanks in advance to all whom reply and here is the code:

Have you looked at the sample code in Unix Network Programming, Vol.1?

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Chris Ritch » Sat, 05 Jul 2003 06:40:31





> >I'll post the code at the bottom of the post.  Whenever I try to

> It looks like you didn't post the correct code, I don't see any calls to
> recvfrom() in the excerpt.

Depest appologies I was posting about that, I was posting how I set
the socket up in case I did something wrong. I know that I'm not
handling any errors in my code as it is still very early in
development. What it does it takes the packet off the socket and
places it in a linked list where another function can pull it off and
use the data. The code is at the bottom of the page again.

Quote:> >retreive a udp packet sent via broadcast I get the errorcode
> >EFAULT(bad address). The same program sends out the packets without

> Sounds like you passed an uninitialized pointer as the buffer argument
> argument.

This is the main problem I am haveing. Before I pass the point to
recvfrom I am initializing it to NULL. This exact same function was
working before when the message was sent unicast instead of broadcast,
but when the message is sent through broadcast the same code returns-1
with errno set to 14, EFAULT.

Quote:> >any complaints but it fails when retreiving, I'm lost as to why it
> >would report this error. It returns the number of bytes correctly but

> If it returns the number of bytes, then how can it also be reporting an
> error code?  Errno is only meaningful if recvfrom() returns -1.

Ah yes you are correct, I was looking at the number thinking it was
the size of the packet received and instead it was the size of the
packet that was sent. recv from is returning -1, I think in my
frustration (and annoyance with flies) over looked this.

Quote:> >returns the char* buffer as NULL. Any help would be appriciated and I

> I don't understand this at all.  Buffer is an input parameter, not an
> output parameter.  You have to pass in the pointer to the place where you
> want the data written.

Sorry wasn't feeling all that awake when I originally wrote this post.
What I ment was the char* buff that is passed by referance that was
being returned to null.

Quote:> >wouldn't be supprised if I was just missing something simple with
> >broacasting as I am still learning some aspects of socket
> >programiing.Thanks in advance to all whom reply and here is the code:

> Have you looked at the sample code in Unix Network Programming, Vol.1?

Yes I have but I was wondering there was something I needed to do
anything special with the the sockaddr_in other than
    sind_addr.s_addr = inet_addr(BCAST_ADDR)

Phew thanks for looking through my errors in my writting. Hopefully I
can give a clearer discription of the problem by correctly describing
what is going on :)

Here is half the function which uses recvfrom, the other half is
basically the same.

int Communications::QueueMessages(int socket)
{
    QueueNode *newNode = new QueueNode;
    newNode->message=NULL;

    cout << "QueueMessages: Entering... ";
    if(first == NULL)   //  Then last is NULL, and there are no
messages in queue
    {
        if( (newNode->messageSize = recvfrom(socket, newNode->message,
            MAXPACKETSIZE + 2, 0, (sockaddr*)&(newNode->client),
NULL)) < 0 )
        {
            cout << "Error message: " << strerror(errno) << endl;
            numMsg = 0;
            delete newNode;
            return numMsg;  //  nothing to retreive
        }
        newNode->next = NULL;
        first = last = newNode;
        numMsg = 1;
        newNode = new QueueNode;
    }
    ...

Where QueueNode is defined as

//  nodes for the queue in communications
struct QueueNode
{
    char* message;
    int messageSize;
    sockaddr_in client;
    QueueNode* next;

Quote:};

 
 
 

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Barry Margoli » Sat, 05 Jul 2003 07:01:56








>> >returns the char* buffer as NULL. Any help would be appriciated and I

>> I don't understand this at all.  Buffer is an input parameter, not an
>> output parameter.  You have to pass in the pointer to the place where you
>> want the data written.

>Sorry wasn't feeling all that awake when I originally wrote this post.
>What I ment was the char* buff that is passed by referance that was
>being returned to null.

Buffer isn't a reference parameter.  It's a pointer to an existing buffer
that you must provide.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Chris Ritch » Wed, 09 Jul 2003 02:05:32









> >> >returns the char* buffer as NULL. Any help would be appriciated and I

> >> I don't understand this at all.  Buffer is an input parameter, not an
> >> output parameter.  You have to pass in the pointer to the place where you
> >> want the data written.

> >Sorry wasn't feeling all that awake when I originally wrote this post.
> >What I ment was the char* buff that is passed by referance that was
> >being returned to null.

> Buffer isn't a reference parameter.  It's a pointer to an existing buffer
> that you must provide.

ok I did get it to work, the problem was that I set the char* to NULL,
onc eI commented out this line of code it worked.

I always thought that recv took a pointer and set it to a section of
memory where the char array was at, not needing allocate it before
hand, I am thinking thats what you mean. I'm not quite sure what you
mean when you say we need to provide a buffer, I would think you mean
we need to allocate memmory before hand but wouldn't that cause a
memory think when the pointer was set to the what recv was
"returning."

 
 
 

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Casper H.S. Di » Wed, 09 Jul 2003 18:44:52



>ok I did get it to work, the problem was that I set the char* to NULL,
>onc eI commented out this line of code it worked.

By shear luck; now you're corrupting memory somewhere.

Quote:>I always thought that recv took a pointer and set it to a section of
>memory where the char array was at, not needing allocate it before
>hand, I am thinking thats what you mean. I'm not quite sure what you
>mean when you say we need to provide a buffer, I would think you mean
>we need to allocate memmory before hand but wouldn't that cause a
>memory think when the pointer was set to the what recv was
>"returning."

But "recv" has no way to communicate to you where the allocated pointer
would point; so the calling sequence tells you that you must provide the
preallocated buffer.  Now you're using a trash value which happens to point to
some allocated memory.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

 
 
 

recvfrom returns with an error code of 14, EFAULT "Bad Address"

Post by Barry Margoli » Wed, 09 Jul 2003 03:24:03




>ok I did get it to work, the problem was that I set the char* to NULL,
>onc eI commented out this line of code it worked.

You need to call malloc() or set it to point to an automatic or static
array.  If you simply removed the NULL assignment, you've now got an
uninitialized variable, and you'll be scribbling over random memory.

Quote:>I always thought that recv took a pointer and set it to a section of
>memory where the char array was at, not needing allocate it before
>hand, I am thinking thats what you mean.

How could it do this?  C doesn't have reference parameters, so it can't set
a pointer that it receives.  C++ is able to do this, but recv() is an
ordinary C function, not a C++ function.  If it were intended to allocate
the memory for you, it would have to receive a char** parameter.

Quote:>                                      I'm not quite sure what you
>mean when you say we need to provide a buffer, I would think you mean
>we need to allocate memmory before hand but wouldn't that cause a
>memory think when the pointer was set to the what recv was

What's a "memory think"?  I think you meant "memory leak".

Quote:>"returning."

Recv() doesn't (and *can't*) set the pointer.  It's not returning anything,
it's just filling in the memory that you allocated for it.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.