URING++
Loading...
Searching...
No Matches
ip_address.h
1#pragma once
2
3#include <arpa/inet.h>
4#include <cstdint>
5#include <netdb.h>
6#include <netinet/in.h>
7#include <string>
8#include <sys/socket.h>
9
10namespace uringpp {
11
12static inline void *get_in_addr(struct sockaddr *sa) {
13 if (sa->sa_family == AF_INET) {
14 return &(((struct sockaddr_in *)sa)->sin_addr);
15 }
16
17 return &(((struct sockaddr_in6 *)sa)->sin6_addr);
18}
19
20static inline uint16_t get_in_port(struct sockaddr *sa) {
21 if (sa->sa_family == AF_INET) {
22 return (((struct sockaddr_in *)sa)->sin_port);
23 }
24
25 return (((struct sockaddr_in6 *)sa)->sin6_port);
26}
27
29public:
30 struct sockaddr_storage ss_;
31 socklen_t len_ = sizeof(ss_);
32 uint16_t port() {
33 return get_in_port(reinterpret_cast<struct sockaddr *>(&ss_));
34 }
35 std::string ip() {
36 char s[INET6_ADDRSTRLEN];
37 ::inet_ntop(ss_.ss_family,
38 get_in_addr(reinterpret_cast<struct sockaddr *>(&ss_)), s,
39 sizeof(s));
40 return s;
41 }
42};
43
44} // namespace uringpp
Definition: ip_address.h:28