I'm trying to do basic operations like copy and equality comparison with a SOCKADDR C Union.
#ifdef AF_INET6
#define SOCKADDR union { \
struct sockaddr_in him4; \
struct sockaddr_in6 him6; \
}
#define SOCKADDR_LEN (ipv6_available() ? sizeof(SOCKADDR) : \
sizeof(struct sockaddr_in))
#else
#define SOCKADDR union { struct sockaddr_in him4; }
#define SOCKADDR_LEN sizeof(SOCKADDR)
#endif
Let's assume I have a SOCKADDR sa
on my stack, which is created with the UDP recvfrom
method below:
void *buf = (void *)jlong_to_ptr(address);
SOCKADDR sa;
socklen_t sa_len = SOCKADDR_LEN;
jint n = recvfrom(fd, buf, len, 0, (struct sockaddr *)&sa, &sa_len);
Great, so far so good!
If I want to duplicate my
sa
, can I just doSOCKADDR newSA = sa
?Is this true for all UNIONs, in other words, I can just assign to copy?
Why do I have to cast my union to a
(struct sockaddr*)
and how is this cast even possible since I don't seesockaddr
in the union declaration?How about equality, can I just do
sa1 == sa2
for logical equivalence?