I have a sockaddr_storage
object and I need to fill it with values provided by the user. Note that user can provide either of AF_INET
or AF_INET6
as domain for filling up the struct.
void fill(sockaddr_storage &addrStruct, int domain,
const char addr[], const int port)
{
std::memset(&addrStruct, 0, sizeof(addrStruct));
switch(domain) {
case AF_INET: addrStruct.sin_family = AF_INET;
addrStruct.sin_port= htons(port);
inet_pton(AF_INET, addr, addrStruct.sin_addr);
case AF_INET6: ....
....
....
default: ....
}
}
Pretty sure this doesn't works since addrStruct
is of type struct sockaddr_storage
and these members are present in struct sockaddr_in
. I also tried static_cast<sockaddr_in>(addrStruct).sin_port
and similar but that again doesn't works. So how should I be filling up this struct so that it holds valid values while respecting alignment of casted structs.