Harald Welte | 303c19a | 2017-08-19 13:06:14 +0200 | [diff] [blame] | 1 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 2 | /* Utility functions that I'm used to from C but for which I couldn't find TTCN-3 implementations |
| 3 | * |
| 4 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 5 | */ |
| 6 | |
| 7 | #include <sys/socket.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <arpa/inet.h> |
| 10 | |
| 11 | #include <Charstring.hh> |
| 12 | #include <Octetstring.hh> |
| 13 | |
| 14 | namespace Native__Functions { |
| 15 | |
| 16 | OCTETSTRING f__inet__addr(const CHARSTRING& in) |
| 17 | { |
| 18 | TTCN_Buffer ttcn_buffer(in); |
| 19 | in_addr_t ia; |
| 20 | |
| 21 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 22 | |
| 23 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 24 | } |
| 25 | |
Harald Welte | 303c19a | 2017-08-19 13:06:14 +0200 | [diff] [blame] | 26 | OCTETSTRING f__inet__haddr(const CHARSTRING& in) |
| 27 | { |
| 28 | TTCN_Buffer ttcn_buffer(in); |
| 29 | in_addr_t ia; |
| 30 | |
| 31 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 32 | ia = ntohl(ia); |
| 33 | |
| 34 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 35 | } |
| 36 | |
| 37 | CHARSTRING f__inet__ntoa(const OCTETSTRING& in) |
| 38 | { |
| 39 | TTCN_Buffer ttcn_buffer(in); |
| 40 | const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data(); |
| 41 | const char *str = inet_ntoa(ia); |
| 42 | |
| 43 | return CHARSTRING(str); |
| 44 | } |
| 45 | |
| 46 | CHARSTRING f__inet__hntoa(const OCTETSTRING& in) |
| 47 | { |
| 48 | TTCN_Buffer ttcn_buffer(in); |
| 49 | struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data(); |
| 50 | ia.s_addr = htonl(ia.s_addr); |
| 51 | const char *str = inet_ntoa(ia); |
| 52 | |
| 53 | return CHARSTRING(str); |
| 54 | } |
| 55 | |
| 56 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 57 | } // namespace |