blob: edb7dbb71fe3b93d4bf72a5d7664f271f3a2a8ed [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/inet_addr.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
3#include <stdlib.h>
4#ifndef __VXWORKS__
5#include <string.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07006
John Park33858a32018-09-28 23:05:48 -07007#include "aos/byteorder.h"
Brian Silvermanb3d50542014-04-23 14:28:55 -05008#else
9
10template<typename T>
11T hton(T);
12
13template<uint32_t>
14uint32_t hton(uint32_t v) { return v; }
15
16#endif
Brian Silvermanf02c3982014-04-21 22:04:41 -070017
Brian Silverman66f079a2013-08-26 16:24:30 -070018namespace aos {
19namespace util {
20
21const char *MakeIPAddress(const in_addr &base_address,
22 ::aos::NetworkAddress last_segment) {
23 in_addr address = base_address;
24 SetLastSegment(&address, last_segment);
25
26#ifdef __VXWORKS__
27 char *r = static_cast<char *>(malloc(INET_ADDR_LEN));
28 inet_ntoa_b(address, r);
29 return r;
30#else
31 return strdup(inet_ntoa(address));
32#endif
33}
34
35void SetLastSegment(in_addr *address, ::aos::NetworkAddress last_segment) {
Brian Silvermanf02c3982014-04-21 22:04:41 -070036 address->s_addr &= ~(hton<uint32_t>(0xFF));
37 address->s_addr |= hton<uint32_t>(static_cast<uint8_t>(last_segment));
Brian Silverman66f079a2013-08-26 16:24:30 -070038}
39
40} // namespace util
41} // namespace aos