aboutsummaryrefslogtreecommitdiffstats
path: root/library/Native_FunctionDefs.cc
blob: 78ca4d1fee0cd1e93937cc219d653f8e289b683e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Utility functions that I'm used to from C but for which I couldn't find TTCN-3 implementations
 *
 * (C) 2017 Harald Welte <laforge@gnumonks.org>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include <Charstring.hh>
#include <Octetstring.hh>

namespace Native__Functions {

OCTETSTRING f__inet6__addr(const CHARSTRING& in)
{
	char buf[INET6_ADDRSTRLEN];
	TTCN_Buffer ttcn_buffer(in);
	int ret;

	ret = inet_pton(AF_INET6, (const char *)ttcn_buffer.get_data(), buf);
	if(ret < 1)
		fprintf(stderr, "inet_pton failed: %d %s\n", ret, strerror(errno));

	return OCTETSTRING(16, (const unsigned char *)&buf[0]);
}

OCTETSTRING f__inet__addr(const CHARSTRING& in)
{
	TTCN_Buffer ttcn_buffer(in);
	in_addr_t ia;

	ia = inet_addr((const char *)ttcn_buffer.get_data());

	return OCTETSTRING(4, (const unsigned char *)&ia);
}

OCTETSTRING f__inet__haddr(const CHARSTRING& in)
{
	TTCN_Buffer ttcn_buffer(in);
	in_addr_t ia;

	ia = inet_addr((const char *)ttcn_buffer.get_data());
	ia = ntohl(ia);

	return OCTETSTRING(4, (const unsigned char *)&ia);
}

CHARSTRING f__inet__ntoa(const OCTETSTRING& in)
{
	TTCN_Buffer ttcn_buffer(in);
	const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data();
	const char *str = inet_ntoa(ia);

	return CHARSTRING(str);
}

CHARSTRING f__inet__hntoa(const OCTETSTRING& in)
{
	TTCN_Buffer ttcn_buffer(in);
	struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data();
	ia.s_addr = htonl(ia.s_addr);
	const char *str = inet_ntoa(ia);

	return CHARSTRING(str);
}

CHARSTRING f__str__tolower(const CHARSTRING& in)
{
	TTCN_Buffer ttcn_buffer(in);
	TTCN_Buffer buf_out;
	CHARSTRING out;
	unsigned int i;

	const char *in_str = (const char *)ttcn_buffer.get_data();
	for (i = 0; i < strlen(in_str); i++)
		buf_out.put_c((unsigned char) tolower(in_str[i]));
	buf_out.get_string(out);
	return out;
}

CHARSTRING f__str__toupper(const CHARSTRING& in)
{
	TTCN_Buffer ttcn_buffer(in);
	TTCN_Buffer buf_out;
	CHARSTRING out;
	unsigned int i;

	const char *in_str = (const char *)ttcn_buffer.get_data();
	for (i = 0; i < strlen(in_str); i++)
		buf_out.put_c((unsigned char) toupper(in_str[i]));
	buf_out.get_string(out);
	return out;
}



} // namespace