blob: 78ca4d1fee0cd1e93937cc219d653f8e289b683e [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
Harald Welte625faad2020-02-19 19:48:10 +010079CHARSTRING f__str__tolower(const CHARSTRING& in)
80{
81 TTCN_Buffer ttcn_buffer(in);
82 TTCN_Buffer buf_out;
83 CHARSTRING out;
84 unsigned int i;
85
86 const char *in_str = (const char *)ttcn_buffer.get_data();
87 for (i = 0; i < strlen(in_str); i++)
88 buf_out.put_c((unsigned char) tolower(in_str[i]));
89 buf_out.get_string(out);
90 return out;
91}
92
93CHARSTRING f__str__toupper(const CHARSTRING& in)
94{
95 TTCN_Buffer ttcn_buffer(in);
96 TTCN_Buffer buf_out;
97 CHARSTRING out;
98 unsigned int i;
99
100 const char *in_str = (const char *)ttcn_buffer.get_data();
101 for (i = 0; i < strlen(in_str); i++)
102 buf_out.put_c((unsigned char) toupper(in_str[i]));
103 buf_out.get_string(out);
104 return out;
105}
106
107
Harald Welte303c19a2017-08-19 13:06:14 +0200108
Harald Welteddeecbb2017-08-18 22:53:30 +0200109} // namespace