blob: 8aa31b4451c1ce2589cbb7967bec9e7f4411771e [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 *
4 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
5 */
6
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
Pau Espin Pedrolaea381f2018-01-29 15:18:17 +010010#include <stdio.h>
11#include <string.h>
12#include <errno.h>
Harald Welteddeecbb2017-08-18 22:53:30 +020013
14#include <Charstring.hh>
15#include <Octetstring.hh>
16
17namespace Native__Functions {
18
Pau Espin Pedrolaea381f2018-01-29 15:18:17 +010019OCTETSTRING f__inet6__addr(const CHARSTRING& in)
20{
21 char buf[INET6_ADDRSTRLEN];
22 TTCN_Buffer ttcn_buffer(in);
23 int ret;
24
25 ret = inet_pton(AF_INET6, (const char *)ttcn_buffer.get_data(), buf);
26 if(ret < 1)
27 fprintf(stderr, "inet_pton failed: %d %s\n", ret, strerror(errno));
28
29 return OCTETSTRING(16, (const unsigned char *)&buf[0]);
30}
31
Harald Welteddeecbb2017-08-18 22:53:30 +020032OCTETSTRING f__inet__addr(const CHARSTRING& in)
33{
34 TTCN_Buffer ttcn_buffer(in);
35 in_addr_t ia;
36
37 ia = inet_addr((const char *)ttcn_buffer.get_data());
38
39 return OCTETSTRING(4, (const unsigned char *)&ia);
40}
41
Harald Welte303c19a2017-08-19 13:06:14 +020042OCTETSTRING f__inet__haddr(const CHARSTRING& in)
43{
44 TTCN_Buffer ttcn_buffer(in);
45 in_addr_t ia;
46
47 ia = inet_addr((const char *)ttcn_buffer.get_data());
48 ia = ntohl(ia);
49
50 return OCTETSTRING(4, (const unsigned char *)&ia);
51}
52
53CHARSTRING f__inet__ntoa(const OCTETSTRING& in)
54{
55 TTCN_Buffer ttcn_buffer(in);
56 const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data();
57 const char *str = inet_ntoa(ia);
58
59 return CHARSTRING(str);
60}
61
62CHARSTRING f__inet__hntoa(const OCTETSTRING& in)
63{
64 TTCN_Buffer ttcn_buffer(in);
65 struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data();
66 ia.s_addr = htonl(ia.s_addr);
67 const char *str = inet_ntoa(ia);
68
69 return CHARSTRING(str);
70}
71
72
Harald Welteddeecbb2017-08-18 22:53:30 +020073} // namespace