blob: 5dda08ce95662619b75aa461b08c028f1d662f2c [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>
10
11#include <Charstring.hh>
12#include <Octetstring.hh>
13
14namespace Native__Functions {
15
16OCTETSTRING f__inet__addr(const CHARSTRING& in)
17{
18 TTCN_Buffer ttcn_buffer(in);
19 in_addr_t ia;
20
21 ia = inet_addr((const char *)ttcn_buffer.get_data());
22
23 return OCTETSTRING(4, (const unsigned char *)&ia);
24}
25
Harald Welte303c19a2017-08-19 13:06:14 +020026OCTETSTRING f__inet__haddr(const CHARSTRING& in)
27{
28 TTCN_Buffer ttcn_buffer(in);
29 in_addr_t ia;
30
31 ia = inet_addr((const char *)ttcn_buffer.get_data());
32 ia = ntohl(ia);
33
34 return OCTETSTRING(4, (const unsigned char *)&ia);
35}
36
37CHARSTRING f__inet__ntoa(const OCTETSTRING& in)
38{
39 TTCN_Buffer ttcn_buffer(in);
40 const struct in_addr ia = *(const struct in_addr *)ttcn_buffer.get_data();
41 const char *str = inet_ntoa(ia);
42
43 return CHARSTRING(str);
44}
45
46CHARSTRING f__inet__hntoa(const OCTETSTRING& in)
47{
48 TTCN_Buffer ttcn_buffer(in);
49 struct in_addr ia = *(const in_addr *)ttcn_buffer.get_data();
50 ia.s_addr = htonl(ia.s_addr);
51 const char *str = inet_ntoa(ia);
52
53 return CHARSTRING(str);
54}
55
56
Harald Welteddeecbb2017-08-18 22:53:30 +020057} // namespace