John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 1 | #include "aos/util/inet_addr.h" |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 2 | |
| 3 | #include <stdlib.h> |
| 4 | #ifndef __VXWORKS__ |
| 5 | #include <string.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 7 | #include "aos/byteorder.h" |
Brian Silverman | b3d5054 | 2014-04-23 14:28:55 -0500 | [diff] [blame] | 8 | #else |
| 9 | |
| 10 | template<typename T> |
| 11 | T hton(T); |
| 12 | |
| 13 | template<uint32_t> |
| 14 | uint32_t hton(uint32_t v) { return v; } |
| 15 | |
| 16 | #endif |
Brian Silverman | f02c398 | 2014-04-21 22:04:41 -0700 | [diff] [blame] | 17 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 18 | namespace aos { |
| 19 | namespace util { |
| 20 | |
| 21 | const 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 | |
| 35 | void SetLastSegment(in_addr *address, ::aos::NetworkAddress last_segment) { |
Brian Silverman | f02c398 | 2014-04-21 22:04:41 -0700 | [diff] [blame] | 36 | address->s_addr &= ~(hton<uint32_t>(0xFF)); |
| 37 | address->s_addr |= hton<uint32_t>(static_cast<uint8_t>(last_segment)); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | } // namespace util |
| 41 | } // namespace aos |