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 | 625faad | 2020-02-19 19:48:10 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 20 | |
| 21 | #include <Charstring.hh> |
| 22 | #include <Octetstring.hh> |
| 23 | |
| 24 | namespace Native__Functions { |
| 25 | |
Pau Espin Pedrol | aea381f | 2018-01-29 15:18:17 +0100 | [diff] [blame] | 26 | OCTETSTRING f__inet6__addr(const CHARSTRING& in) |
| 27 | { |
| 28 | char buf[INET6_ADDRSTRLEN]; |
| 29 | TTCN_Buffer ttcn_buffer(in); |
| 30 | int ret; |
| 31 | |
| 32 | ret = inet_pton(AF_INET6, (const char *)ttcn_buffer.get_data(), buf); |
| 33 | if(ret < 1) |
| 34 | fprintf(stderr, "inet_pton failed: %d %s\n", ret, strerror(errno)); |
| 35 | |
| 36 | return OCTETSTRING(16, (const unsigned char *)&buf[0]); |
| 37 | } |
| 38 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 39 | OCTETSTRING f__inet__addr(const CHARSTRING& in) |
| 40 | { |
| 41 | TTCN_Buffer ttcn_buffer(in); |
| 42 | in_addr_t ia; |
| 43 | |
| 44 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 45 | |
| 46 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 47 | } |
| 48 | |
Harald Welte | 303c19a | 2017-08-19 13:06:14 +0200 | [diff] [blame] | 49 | OCTETSTRING f__inet__haddr(const CHARSTRING& in) |
| 50 | { |
| 51 | TTCN_Buffer ttcn_buffer(in); |
| 52 | in_addr_t ia; |
| 53 | |
| 54 | ia = inet_addr((const char *)ttcn_buffer.get_data()); |
| 55 | ia = ntohl(ia); |
| 56 | |
| 57 | return OCTETSTRING(4, (const unsigned char *)&ia); |
| 58 | } |
| 59 | |
| 60 | CHARSTRING f__inet__ntoa(const OCTETSTRING& in) |
| 61 | { |
| 62 | TTCN_Buffer ttcn_buffer(in); |
| 63 | const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data(); |
| 64 | const char *str = inet_ntoa(ia); |
| 65 | |
| 66 | return CHARSTRING(str); |
| 67 | } |
| 68 | |
| 69 | CHARSTRING f__inet__hntoa(const OCTETSTRING& in) |
| 70 | { |
| 71 | TTCN_Buffer ttcn_buffer(in); |
| 72 | struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data(); |
| 73 | ia.s_addr = htonl(ia.s_addr); |
| 74 | const char *str = inet_ntoa(ia); |
| 75 | |
| 76 | return CHARSTRING(str); |
| 77 | } |
| 78 | |
Vadim Yanitskiy | 04ef7d8 | 2022-02-11 19:11:46 +0600 | [diff] [blame] | 79 | CHARSTRING f__inet6__ntoa(const OCTETSTRING& in) |
| 80 | { |
| 81 | char buf[INET6_ADDRSTRLEN] = { 0 }; |
| 82 | TTCN_Buffer ttcn_buffer(in); |
| 83 | |
| 84 | const void *src = (const void *)ttcn_buffer.get_data(); |
| 85 | const char *str = inet_ntop(AF_INET6, src, buf, sizeof(buf)); |
| 86 | if (str == NULL) |
| 87 | fprintf(stderr, "inet_ntop failed: %s\n", strerror(errno)); |
| 88 | |
| 89 | return CHARSTRING((const char *)buf); |
| 90 | } |
| 91 | |
Harald Welte | 625faad | 2020-02-19 19:48:10 +0100 | [diff] [blame] | 92 | CHARSTRING f__str__tolower(const CHARSTRING& in) |
| 93 | { |
| 94 | TTCN_Buffer ttcn_buffer(in); |
| 95 | TTCN_Buffer buf_out; |
| 96 | CHARSTRING out; |
| 97 | unsigned int i; |
| 98 | |
| 99 | const char *in_str = (const char *)ttcn_buffer.get_data(); |
| 100 | for (i = 0; i < strlen(in_str); i++) |
| 101 | buf_out.put_c((unsigned char) tolower(in_str[i])); |
| 102 | buf_out.get_string(out); |
| 103 | return out; |
| 104 | } |
| 105 | |
| 106 | CHARSTRING f__str__toupper(const CHARSTRING& in) |
| 107 | { |
| 108 | TTCN_Buffer ttcn_buffer(in); |
| 109 | TTCN_Buffer buf_out; |
| 110 | CHARSTRING out; |
| 111 | unsigned int i; |
| 112 | |
| 113 | const char *in_str = (const char *)ttcn_buffer.get_data(); |
| 114 | for (i = 0; i < strlen(in_str); i++) |
| 115 | buf_out.put_c((unsigned char) toupper(in_str[i])); |
| 116 | buf_out.get_string(out); |
| 117 | return out; |
| 118 | } |
| 119 | |
| 120 | |
Harald Welte | 303c19a | 2017-08-19 13:06:14 +0200 | [diff] [blame] | 121 | |
Harald Welte | ddeecbb | 2017-08-18 22:53:30 +0200 | [diff] [blame] | 122 | } // namespace |