C header files for ipv4 addresses

Hello, I'm trying for some help in choosing the data type for ipv4 variables, is there any predefined headers for this, or should I write my own implantation (class ipv4{bytes....}).

Thank you
 
Don't roll your own! For IPv4 addresses, there's in_addr_t, defined as a part of the BSD sockets API.
You'd typically use it as part of a struct sockaddr_in, which e.g. also includes the port.

In fact, try to write your code independent of the protocol used, that's how the sockets API was originally designed (with struct sockaddr as a "base type").

edit: I'd recommend using higher-level API calls like getaddrinfo(3)/getnameinfo(3) to work with socket addresses.
 
Don't roll your own! For IPv4 addresses, there's in_addr_t, defined as a part of the BSD sockets API.
You'd typically use it as part of a struct sockaddr_in, which e.g. also includes the port.

In fact, try to write your code independent of the protocol used, that's how the sockets API was originally designed (with struct sockaddr as a "base type").

edit: I'd recommend using higher-level API calls like getaddrinfo(3)/getnameinfo(3) to work with socket addresses.
What is actual type for subnet? in_addr_t is like a one ip address.
 
What kind of problem do you solve? It's rather untypical to need subnets in userspace networking applications 🤔

But the two typical approaches are to either store a second in_addr_t used as a bitmask, or a simple integer giving the prefix length (number of initial 1-bits in the bitmask).
 
Back
Top