blob: 7139a624062dbabade2a9c9b3e1cef884f94e24f [file] [log] [blame]
Harald Welte303c19a2017-08-19 13:06:14 +02001
Harald Welteddeecbb2017-08-18 22:53:30 +02002/* Utility functions that I'm used to from C but for which I couldn't find TTCN-3 implementations
3 *
Harald Welte34b5a952019-05-27 11:54:11 +02004 * (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 Welteddeecbb2017-08-18 22:53:30 +020011 */
12
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <arpa/inet.h>
Pau Espin Pedrolaea381f2018-01-29 15:18:17 +010016#include <stdio.h>
17#include <string.h>
18#include <errno.h>
Harald Welte625faad2020-02-19 19:48:10 +010019#include <ctype.h>
Harald Welteddeecbb2017-08-18 22:53:30 +020020
21#include <Charstring.hh>
22#include <Octetstring.hh>
23
24namespace Native__Functions {
25
Pau Espin Pedrolaea381f2018-01-29 15:18:17 +010026OCTETSTRING 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 Welteddeecbb2017-08-18 22:53:30 +020039OCTETSTRING 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 Welte303c19a2017-08-19 13:06:14 +020049OCTETSTRING 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
60CHARSTRING 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
69CHARSTRING 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 Yanitskiy04ef7d82022-02-11 19:11:46 +060079CHARSTRING 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 Welte625faad2020-02-19 19:48:10 +010092CHARSTRING 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
106CHARSTRING 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 Welte303c19a2017-08-19 13:06:14 +0200121
Harald Welteddeecbb2017-08-18 22:53:30 +0200122} // namespace