aboutsummaryrefslogtreecommitdiffstats
path: root/library/Native_FunctionDefs.cc
blob: f6d5d1d9a40a6c2b77830559c9f80c2fb6e4de2c (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
/* 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 <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);
}


} // namespace