views:

44

answers:

3

While trying the following the address in the second sockaddr changes:

/*Stuff*/
sockaddr add1, add2;
recvfrom(/*socket*/, /*buffer*/, /*count*/, /*flag*/, &add1, /*fromlen*/);

add2 = add1; //The sa_data - part changes O_o...

/*Stuff*/

Anyone knows why?...

EDIT: 1.I changed the sockaddr to sockaddr_storage which definetly has enough space for sockaddr_in!! 2. I memset the structure to zero on initialization 3. I wrote a copy ruitine for my copy/assignment wishes:

memcpy(&AddrTarget, &AddrSource, sizeof(sockaddr_storage));

But this does not help, too!... Im desperate:(...

A: 

It's possible that the whole struct isn't being copied. sa_data can be longer than 14 elements. But doing a copy misses the bits after the 14'th value.

http://www.delorie.com/gnu/docs/glibc/libc_305.html

Sibshops
Would it be a solution to use the SOCKADDR_STORAGE structure instead, and cast it , when needed?...
Incubbus
A: 

Assuming the common case of TCP - you probably want to use a sockaddr_in, which has space for the extra fields.

Tony
`recvfrom` is typically used with UDP.
dreamlax
+1  A: 

How is fromlen being set when you call recvfrom()? If fromlen > sizeof(add1), you are possibly writing over add2 by accident.

Beej's Guide suggests that you use local variables of type struct sockaddr_storage, which is guaranteed to be big enough to hold any of the struct sockaddr_foos in use.

Jack Kelly
Didn't know about this, very nice.
Matt Joiner
Yes... I did use this, but: It still does not work... what I am doing is:SOCKADDR_STORAGE x, y;recvfrom(/*recv addr into x*/);y= x; <<---- still changes the content...
Incubbus