blob: a1a347c754b2e7d5811e6daca741549f1bb6f4d6 [file] [log] [blame]
Harald Welte9a5dd542018-10-28 10:33:23 +01001module DNS_Helpers {
2
Harald Welte34b5a952019-05-27 11:54:11 +02003/* DNS Helper functions in TTCN-3
4 * (C) 2018 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
11 */
12
Vadim Yanitskiy89f198c2021-11-19 03:31:08 +030013private function f_strchr(charstring s, charstring c) return integer {
Harald Welte9a5dd542018-10-28 10:33:23 +010014 var integer i;
15 for (i := 0; i < lengthof(s); i := i+1) {
16 if (s[i] == c) {
17 return i;
18 }
19 }
20 return -1;
21}
22
23private function f_dns_enc_label(charstring str) return octetstring {
24 var octetstring ret;
25
26 ret[0] := int2oct(lengthof(str), 1);
27 return ret & char2oct(str);
28}
29
30function f_enc_dns_hostname(charstring str) return octetstring {
31 var octetstring ret := ''O;
32 while (lengthof(str) > 0) {
33 var integer dot_idx;
34 var octetstring lbl;
35
36 dot_idx := f_strchr(str, ".");
37 if (dot_idx >= 0) {
38 /* there is another dot */
39 lbl := f_dns_enc_label(substr(str, 0, dot_idx));
40 str := substr(str, dot_idx+1, lengthof(str)-dot_idx-1);
41 } else {
42 /* no more dot */
43 lbl := f_dns_enc_label(str);
44 str := "";
45 }
46 ret := ret & lbl;
47 }
48 return ret;
49}
50
51
52
53
54function f_dec_dns_hostname(octetstring inp) return charstring {
55 var charstring ret := "";
56 while (lengthof(inp) > 0) {
57 var integer label_len;
58 var charstring lbl;
59
60 label_len := oct2int(substr(inp, 0, 1));
61 lbl := oct2char(substr(inp, 1, label_len));
62 inp := substr(inp, 1+label_len, lengthof(inp)-1-label_len);
63
64 ret := ret & lbl;
65 if (lengthof(inp) > 0) {
66 ret := ret & ".";
67 }
68 }
69 return ret;
70}
71
72
Oliver Smithd5696eb2020-01-29 14:58:21 +010073function f_enc_IPv4(charstring str) return octetstring {
74 var octetstring ret := ''O;
75
76 while (lengthof(str) > 0) {
77 var integer dot_idx;
78 var charstring num;
79
80 dot_idx := f_strchr(str, ".");
81 if (dot_idx >= 0) {
82 /* there is another dot */
83 num := substr(str, 0, dot_idx);
84 str := substr(str, dot_idx+1, lengthof(str)-dot_idx-1);
85 } else {
86 /* no more dot */
87 num := str;
88 str := "";
89 }
90 ret := ret & int2oct(str2int(num), 1);
91 }
92
93 return ret;
94}
95
Harald Welte9a5dd542018-10-28 10:33:23 +010096}