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 | * |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 4 | * (C) 2017 Harald Welte <laforge@gnumonks.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Released under the terms of GNU General Public License, Version 2 or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0-or-later |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <sys/socket.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <arpa/inet.h> |
Pau Espin Pedrol | aea381f | 2018-01-29 15:18:17 +0100 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <errno.h> |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 19 | |
| 20 | #include <Charstring.hh> |
| 21 | #include <Octetstring.hh> |
| 22 | |
| 23 | namespace Native__Functions { |
| 24 | |
Pau Espin Pedrol | aea381f | 2018-01-29 15:18:17 +0100 | [diff] [blame] | 25 | OCTETSTRING f__inet6__addr(const CHARSTRING& in) |
| 26 | { |
| 27 | char buf[INET6_ADDRSTRLEN]; |
| 28 | TTCN_Buffer ttcn_buffer(in); |
| 29 | int ret; |
| 30 | |
| 31 | ret = inet_pton(AF_INET6, (const char *)ttcn_buffer.get_data(), buf); |
| 32 | if(ret < 1) |
| 33 | fprintf(stderr, "inet_pton failed: %d %s\n", ret, strerror(errno)); |
| 34 | |
| 35 | return OCTETSTRING(16, (const unsigned char *)&buf[0]); |
| 36 | } |
| 37 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 38 | OCTETSTRING f__inet__addr(const CHARSTRING& in) |
| 39 | { |
| 40 | TTCN_Buffer ttcn_buffer(in); |
| 41 | in_addr_t ia; |
| 42 | |
| 43 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 44 | |
| 45 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 46 | } |
| 47 | |
Harald Welte | 303c19a | 2017-08-19 13:06:14 +0200 | [diff] [blame] | 48 | OCTETSTRING f__inet__haddr(const CHARSTRING& in) |
| 49 | { |
| 50 | TTCN_Buffer ttcn_buffer(in); |
| 51 | in_addr_t ia; |
| 52 | |
| 53 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 54 | ia = ntohl(ia); |
| 55 | |
| 56 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 57 | } |
| 58 | |
| 59 | CHARSTRING f__inet__ntoa(const OCTETSTRING& in) |
| 60 | { |
| 61 | TTCN_Buffer ttcn_buffer(in); |
| 62 | const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data(); |
| 63 | const char *str = inet_ntoa(ia); |
| 64 | |
| 65 | return CHARSTRING(str); |
| 66 | } |
| 67 | |
| 68 | CHARSTRING f__inet__hntoa(const OCTETSTRING& in) |
| 69 | { |
| 70 | TTCN_Buffer ttcn_buffer(in); |
| 71 | struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data(); |
| 72 | ia.s_addr = htonl(ia.s_addr); |
| 73 | const char *str = inet_ntoa(ia); |
| 74 | |
| 75 | return CHARSTRING(str); |
| 76 | } |
| 77 | |
| 78 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 79 | } // namespace |