blob: f6d5d1d9a40a6c2b77830559c9f80c2fb6e4de2c [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 Welteddeecbb2017-08-18 22:53:30 +020019
20#include <Charstring.hh>
21#include <Octetstring.hh>
22
23namespace Native__Functions {
24
Pau Espin Pedrolaea381f2018-01-29 15:18:17 +010025OCTETSTRING f__inet6__addr(const CHARSTRING& in)
26{
27 char buf[INET6_ADDRSTRLEN];
28 TTCN_Buffer ttcn_buffer(in);
29 int ret;
30
31 ret = inet_pton(AF_INET6, (const char *)ttcn_buffer.get_data(), buf);
32 if(ret < 1)
33 fprintf(stderr, "inet_pton failed: %d %s\n", ret, strerror(errno));
34
35 return OCTETSTRING(16, (const unsigned char *)&buf[0]);
36}
37
Harald Welteddeecbb2017-08-18 22:53:30 +020038OCTETSTRING f__inet__addr(const CHARSTRING& in)
39{
40 TTCN_Buffer ttcn_buffer(in);
41 in_addr_t ia;
42
43 ia = inet_addr((const char *)ttcn_buffer.get_data());
44
45 return OCTETSTRING(4, (const unsigned char *)&ia);
46}
47
Harald Welte303c19a2017-08-19 13:06:14 +020048OCTETSTRING f__inet__haddr(const CHARSTRING& in)
49{
50 TTCN_Buffer ttcn_buffer(in);
51 in_addr_t ia;
52
53 ia = inet_addr((const char *)ttcn_buffer.get_data());
54 ia = ntohl(ia);
55
56 return OCTETSTRING(4, (const unsigned char *)&ia);
57}
58
59CHARSTRING f__inet__ntoa(const OCTETSTRING& in)
60{
61 TTCN_Buffer ttcn_buffer(in);
62 const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data();
63 const char *str = inet_ntoa(ia);
64
65 return CHARSTRING(str);
66}
67
68CHARSTRING f__inet__hntoa(const OCTETSTRING& in)
69{
70 TTCN_Buffer ttcn_buffer(in);
71 struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data();
72 ia.s_addr = htonl(ia.s_addr);
73 const char *str = inet_ntoa(ia);
74
75 return CHARSTRING(str);
76}
77
78
Harald Welteddeecbb2017-08-18 22:53:30 +020079} // namespace