24 explicit sockaddr_ptr(
const sockaddr* sa) : msa(*sa) {}
25 const sockaddr_in& operator*()
const {
return msi; }
26 const sockaddr_in* operator->()
const {
return &msi; }
29 union { sockaddr msa; sockaddr_in msi; };
36 temp_socket() : SimpleSocket( AF_INET, SOCK_DGRAM, 0) {}
40 void assign_ifreq(
struct ifreq& ifr, std::string_view interface)
42 size_t len = interface.length();
44 if(
len >= IF_NAMESIZE)
47 ifr.ifr_addr.sa_family = AF_INET;
48 std::memcpy( ifr.ifr_name, interface.data(),
len);
49 ifr.ifr_name[
len] = 0;
55 hostent* host = gethostbyname( hostname.c_str());
59 throw SocketException(
"Failed to resolve address (gethostbyname)",
false);
63 auto addr = host->h_addr;
64 int len = sprintf(ip,
"%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
65 return std::string(ip,
len);
72 serv = getservbyname( service.c_str(),
nullptr);
74 serv = getservbyname( service.c_str(), protocol.c_str());
77 return ntohs(
static_cast<uint16_t
>(serv->s_port));
100 std::vector<std::string> ret;
102 char data[32 *
sizeof(
struct ifreq)];
106 conf.ifc_len =
sizeof(data);
107 conf.ifc_buf = (caddr_t) data;
110 if( ioctl( sock.nativeHandle(), SIOCGIFCONF, &conf) < 0)
113 ifr = (
struct ifreq*)data;
114 while( (
char*)ifr < data+conf.ifc_len)
116 switch(ifr->ifr_addr.sa_family)
119 ret.emplace_back( ifr->ifr_name);
124 printf(
"%s : %s\n", ifr->ifr_name, inet_ntop(ifr->ifr_addr.sa_family, &((
struct sockaddr_in6*)&ifr->ifr_addr)->sin6_addr, addrbuf,
sizeof(addrbuf)));
138 assign_ifreq( ifr, interface);
141 if( ioctl( sock.nativeHandle(), SIOCGIFADDR, &ifr) < 0)
144 return inet_ntoa( sockaddr_ptr( &ifr.ifr_addr)->sin_addr);
150 assign_ifreq( ifr, interface);
152 std::string address_z(address);
153 inet_aton( address_z.c_str(), &(
reinterpret_cast<sockaddr_in*
>(&ifr.ifr_addr)->sin_addr));
156 if( ioctl( sock.nativeHandle(), SIOCSIFADDR, &ifr) < 0)
163 assign_ifreq( ifr, interface);
166 if( ioctl( sock.nativeHandle(), SIOCGIFBRDADDR, &ifr) < 0)
169 return inet_ntoa( sockaddr_ptr( &ifr.ifr_broadaddr)->sin_addr);
175 assign_ifreq( ifr, interface);
177 std::string address_z(address);
178 inet_aton( address_z.c_str(), &(
reinterpret_cast<sockaddr_in*
>(&ifr.ifr_broadaddr)->sin_addr));
181 if( ioctl( sock.nativeHandle(), SIOCSIFBRDADDR, &ifr) < 0)
188 assign_ifreq( ifr, interface);
191 if( ioctl( sock.nativeHandle(), SIOCGIFNETMASK, &ifr) < 0)
194 return inet_ntoa( sockaddr_ptr( &ifr.ifr_netmask)->sin_addr);
200 assign_ifreq( ifr, interface);
202 std::string address_z(address);
203 inet_aton( address_z.c_str(), &(
reinterpret_cast<sockaddr_in*
>(&ifr.ifr_netmask)->sin_addr));
206 if( ioctl( sock.nativeHandle(), SIOCSIFNETMASK, &ifr) < 0)
213 assign_ifreq( ifr, interface);
216 if( ioctl( sock.nativeHandle(), SIOCGIFDSTADDR, &ifr) < 0)
219 return inet_ntoa( sockaddr_ptr( &ifr.ifr_dstaddr)->sin_addr);
225 assign_ifreq( ifr, interface);
227 std::string address_z(address);
228 inet_aton( address_z.c_str(), &(
reinterpret_cast<sockaddr_in*
>(&ifr.ifr_dstaddr)->sin_addr));
231 if( ioctl( sock.nativeHandle(), SIOCSIFDSTADDR, &ifr) < 0)
238 assign_ifreq( ifr, interface);
241 if( ioctl( sock.nativeHandle(), SIOCGIFMTU, &ifr) < 0)
250 assign_ifreq( ifr, interface);
255 if( ioctl( sock.nativeHandle(), SIOCSIFMTU, &ifr) < 0)
262 assign_ifreq( ifr, interface);
265 if( ioctl( sock.nativeHandle(), SIOCGIFHWADDR, &ifr) < 0)
268 switch( ifr.ifr_hwaddr.sa_family) {
281 auto addr =
reinterpret_cast<unsigned char*
>( ifr.ifr_addr.sa_data);
282 int len = sprintf(mac,
"%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
283 return std::string(mac,
len);
void setDestinationAddress(std::string_view interface, std::string_view address)
Set the IPv4 destination address of the given network interface.
std::string getDestinationAddress(std::string_view interface)
Return the IPv4 destination address of the given network interface.
std::string getHardwareAddress(std::string_view interface)
Return the MAC address of the given network interfaces.
std::string getInterfaceAddress(std::string_view interface)
Return the IPv4 address of the given network interface.
int getMTU(std::string_view interface)
Return the MTU (Maximum Transmission Unit) of the given network interface in bytes.
void setInterfaceAddress(std::string_view interface, std::string_view address)
Set the IPv4 address of the given network interface.
uint16_t resolveService(const std::string &service, const std::string &protocol="tcp")
std::vector< std::string > getNetworkInterfaces()
Return a list of available network interfaces.
std::string getBroadcastAddress(std::string_view interface)
Return the IPv4 broadcast address of the given network interface.
void setMTU(std::string_view interface, int mtu)
Set the MTU (Maximum Transmission Unit) of the given network interface in bytes.
std::string getNetmask(std::string_view interface)
Return the IPv4 netmask of the given network interface.
std::string resolveHostname(const std::string &hostname)
void setBroadcastAddress(std::string_view interface, std::string_view address)
Set the IPv4 broadcast address of the given network interface.
void setNetmask(std::string_view interface, std::string_view address)
Set the IPv4 netmask of the given network interface.
Signals a problem with the execution of a socket call.
A socket representing a basic communication endpoint.