c - Extracting IP and port from multiple sockaddr_storage's into a char* -
i have fixed length array, every entry type struct contact
typedef struct contact { int fd; union { struct sockaddr_in v4addr; struct sockaddr_in6 v6addr; struct sockaddr_storage stor; }; char buf[frame_buf_len]; int len; char name[32]; } contact_t;
and need extract ip , port every entry char*. result should this
192.168.0.1 1234\n192.168.0.2 1235\n192.168.0.3 1236\n //and on..
i have no clue how information , allocate correct size final char*.
use (for example) struct sockaddr_storage stor
's member ss_family
determine address family , depending on chose v4addr
or v6addr
used inet_ntop()
.
the port number comes in network bytes order, shall pass ntohs()
before being used.
the members of v4addr
, v6addr
used drawn <netinet/in.h>
:
/* structure describing internet socket address. */ struct sockaddr_in { [...] in_port_t sin_port; /* port number. */ struct in_addr sin_addr; /* internet address. */ [...] }; /* ditto, ipv6. */ struct sockaddr_in6 { [...] in_port_t sin6_port; /* transport layer port # */ [...] struct in6_addr sin6_addr; /* ipv6 address */ [...] };
to create buffers size unknown @ compile time, use dynamic memory allocation in general.
for successivly allocating memory block of increasing size, when looping through arrray , adding address:port tuples use realloc()
in particular.
Comments
Post a Comment