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> |
Pau Espin Pedrol | aea381f | 2018-01-29 15:18:17 +0100 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <errno.h> |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 13 | |
| 14 | #include <Charstring.hh> |
| 15 | #include <Octetstring.hh> |
| 16 | |
| 17 | namespace Native__Functions { |
| 18 | |
Pau Espin Pedrol | aea381f | 2018-01-29 15:18:17 +0100 | [diff] [blame] | 19 | OCTETSTRING f__inet6__addr(const CHARSTRING& in) |
| 20 | { |
| 21 | char buf[INET6_ADDRSTRLEN]; |
| 22 | TTCN_Buffer ttcn_buffer(in); |
| 23 | int ret; |
| 24 | |
| 25 | ret = inet_pton(AF_INET6, (const char *)ttcn_buffer.get_data(), buf); |
| 26 | if(ret < 1) |
| 27 | fprintf(stderr, "inet_pton failed: %d %s\n", ret, strerror(errno)); |
| 28 | |
| 29 | return OCTETSTRING(16, (const unsigned char *)&buf[0]); |
| 30 | } |
| 31 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 32 | OCTETSTRING f__inet__addr(const CHARSTRING& in) |
| 33 | { |
| 34 | TTCN_Buffer ttcn_buffer(in); |
| 35 | in_addr_t ia; |
| 36 | |
| 37 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 38 | |
| 39 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 40 | } |
| 41 | |
Harald Welte | 303c19a | 2017-08-19 13:06:14 +0200 | [diff] [blame] | 42 | OCTETSTRING f__inet__haddr(const CHARSTRING& in) |
| 43 | { |
| 44 | TTCN_Buffer ttcn_buffer(in); |
| 45 | in_addr_t ia; |
| 46 | |
| 47 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 48 | ia = ntohl(ia); |
| 49 | |
| 50 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 51 | } |
| 52 | |
| 53 | CHARSTRING f__inet__ntoa(const OCTETSTRING& in) |
| 54 | { |
| 55 | TTCN_Buffer ttcn_buffer(in); |
| 56 | const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data(); |
| 57 | const char *str = inet_ntoa(ia); |
| 58 | |
| 59 | return CHARSTRING(str); |
| 60 | } |
| 61 | |
| 62 | CHARSTRING f__inet__hntoa(const OCTETSTRING& in) |
| 63 | { |
| 64 | TTCN_Buffer ttcn_buffer(in); |
| 65 | struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data(); |
| 66 | ia.s_addr = htonl(ia.s_addr); |
| 67 | const char *str = inet_ntoa(ia); |
| 68 | |
| 69 | return CHARSTRING(str); |
| 70 | } |
| 71 | |
| 72 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 73 | } // namespace |