35 #include <arpa/inet.h>
37 #include <sys/socket.h>
38 #include <sys/types.h>
69 memset(addr, 0,
sizeof(
struct sockaddr_in));
75 #if defined(__linux__)
77 struct hostent hent, *he = 0;
81 if (gethostbyname_r(host, &hent, hbuf,
sizeof(hbuf), &he, &err) != 0
83 HT_ERRORF(
"gethostbyname '%s': error: %d", host, err);
86 #elif defined(__APPLE__) || defined(__sun__) || defined(__FreeBSD__)
89 struct hostent *he = gethostbyname(host);
92 String errmsg = (
String)
"gethostbyname(\"" + host +
"\")";
93 herror(errmsg.c_str());
98 # error TODO please implement me!
100 memcpy(&addr->sin_addr.s_addr, he->h_addr_list[0],
sizeof(uint32_t));
101 if (addr->sin_addr.s_addr == 0) {
102 uint8_t *ip = (uint8_t *)&addr->sin_addr.s_addr;
107 addr->sin_family = AF_INET;
108 addr->sin_port = htons(port);
114 const char *ptr = ipin, *end = ipin + strlen(ipin);
117 int num_components=0;
121 component = strtol(ptr, &last, base);
125 if (*last !=
'.' || last > end || component > 255 || component < 0 || num_components > 4)
129 if (num_components != 4 || component > 255 || component < 0)
137 uint8_t *ip = (uint8_t *)&addr.sin_addr.s_addr;
138 const char *ipstr = ipin, *end = ipin + strlen(ipin);
140 int64_t n = strtoll(ipstr, &last, base);
142 addr.sin_family = AF_INET;
143 addr.sin_port = htons(port);
145 if (last == end && n > 0 && n < UINT32_MAX) {
146 addr.sin_addr.s_addr = htonl(n);
151 if (last > end || *last !=
'.')
155 *ip++ = strtol(ipstr, &last, base);
157 if (last >= end || *last !=
'.')
161 *ip++ = strtol(ipstr, &last, base);
163 if (last >= end || *last !=
'.')
167 *ip++ = strtol(ipstr, &last, base);
172 if (addr.sin_addr.s_addr == 0) {
173 uint8_t *ip = (uint8_t *)&addr.sin_addr.s_addr;
182 const char *colon = strchr(endpoint,
':');
186 return Endpoint(host, atoi(colon + 1));
188 return Endpoint(endpoint, default_port);
197 return initialize(addr,
"localhost", atoi(addr_str));
201 memset(addr, 0 ,
sizeof(sockaddr_in));
202 addr->sin_family = AF_INET;
203 addr->sin_addr.s_addr = htonl(haddr);
204 addr->sin_port = htons(port);
210 const uint8_t *ip = (uint8_t *)&addr.sin_addr.s_addr;
212 (
int)ip[2],(int)ip[3], sep, (
int)ntohs(addr.sin_port));
217 const uint8_t *ip = (uint8_t *)&addr.sin_addr.s_addr;
219 (
int)ip[2], (int)ip[3]);
224 ntohs(addr.sin_port));
266 const uint8_t *end = *bufp + encoding_length;
267 size_t tmp_remain = encoding_length;
270 *remainp -= encoding_length;
305 *(*bufp)++ =
sizeof(sockaddr_in);
306 *(*bufp)++ = sin_family;
325 std::ostream &
operator<<(std::ostream &out,
const sockaddr_in &a) {
String format_ipaddress()
Returns a string with a dotted notation ("127.0.0.1") without! the port.
std::string String
A String is simply a typedef to std::string.
static Endpoint parse_endpoint(const char *endpoint, int defport=0)
Parse an endpoint string in (host:port) format.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
String hex(int sep= ':')
Returns a compact String representation ("0x2387646:80") including the port.
uint8_t encoding_version() const
Returns encoding version.
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
#define HT_EXPECT(_e_, _code_)
uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Decode a 8-bit integer (a byte/character)
int encoded_length_vi32(uint32_t val)
Length of a variable length encoded 32-bit integer (up to 5 bytes)
Encapsulate an internet address.
uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Decode a 16-bit integer in little-endian order.
static bool is_ipv4(const char *ip)
Tests whether the input string in n.n.n.n format (base 10)
Logging routines and macros.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
InetAddr()
Constructor creates an empty internet address.
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
void encode_i16(uint8_t **bufp, uint16_t val)
Encode a 16-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
String format(int sep= ':') const
Returns a string with a dotted notation ("127.0.0.1:8080") including the port.
void encode_vi32(uint8_t **bufp, uint32_t val)
Encode a integer (up to 32-bit) in variable length encoding.
static bool initialize(sockaddr_in *addr, const char *host, uint16_t port)
Initialize a sockaddr_in structure from host:port.
#define HT_THROWF(_code_, _fmt_,...)
size_t encoded_length_internal() const
Returns internal serialized length.
size_t encoded_length() const
Returns serialized object length.
Internet address wrapper classes and utility functions.
void legacy_decode(const uint8_t **bufp, size_t *remainp)
Deserializes object from legacy serialization format.
void encode_internal(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
#define HT_ERRORF(msg,...)
static bool parse_ipv4(const char *ip, uint16_t port, sockaddr_in &addr, int base=0)
Parses an ipv4 address string in dotted notiation ("n.n.n.n") or as a number and initializes a sockad...
String extensions and helpers: sets, maps, append operators etc.
#define HT_THROW(_code_, _msg_)
uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Decode a variable length encoded integer up to 32-bit.
void encode_i8(uint8_t **bufp, uint8_t val)
Encodes a byte into the given buffer.
High-level entry point to a service; wraps a host:port pair.