1/* User-level DNS functions                                    -*- mfl -*-
2   Copyright (C) 2007-2021 Sergey Poznyakoff
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 3, or (at your option)
7   any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17module 'dns'.
18
19const
20do
21  DNS_TYPE_A 1
22  DNS_TYPE_NS 2
23  DNS_TYPE_PTR 12
24  DNS_TYPE_MX 15
25  DNS_TYPE_TXT 16
26done
27
28func hostname (string ipstr)
29  returns string
30do
31  catch *
32  do
33    return ipstr
34  done
35  return primitive_hostname(ipstr)
36done
37
38func resolve (string str; string domain)
39  returns string
40do
41  catch *
42  do
43    return "0"
44  done
45  if defined(domain)
46    return primitive_resolve (str, domain)
47  else
48    return primitive_resolve (str)
49  fi
50done
51
52func hasmx (string str)
53  returns string
54do
55  catch *
56  do
57    return 0
58  done
59  return primitive_hasmx(str)
60done
61
62func ismx (string domain, string ipstr)
63  returns number
64do
65  catch *
66  do
67    return 0
68  done
69  return primitive_ismx (domain, ipstr)
70done
71
72func hasns (string str)
73  returns number
74do
75  try
76  do
77    return primitive_hasns (str)
78  done
79  catch *
80  do
81    return 0
82  done
83done
84
85func dns_getname (string ipstr)
86  returns string
87do
88  string result ''
89  set n dns_query(DNS_TYPE_PTR, ipstr, 1)
90  loop for set i 0,
91       while i < dns_reply_count(n),
92       set i i + 1
93  do
94    if result
95      set result result . ' '
96    fi
97    set result result . dns_reply_string(n, i)
98  done
99  dns_reply_release(n)
100  return result
101done
102
103func dns_getaddr (string domain)
104  returns string
105do
106  string result ''
107  set n dns_query(DNS_TYPE_A, domain, 1)
108  loop for set i 0,
109       while i < dns_reply_count(n),
110       set i i + 1
111  do
112    if result
113      set result result . ' '
114    fi
115    set result result . dns_reply_string(n, i)
116  done
117  dns_reply_release(n)
118  return result
119done
120
121func getns (string domain; number resolve_names, number sort_names)
122  returns string
123do
124  string result ''
125  set n dns_query(DNS_TYPE_NS, domain, sort_names, resolve_names)
126  loop for set i 0,
127       while i < dns_reply_count(n),
128       set i i + 1
129  do
130    if result
131      set result result . ' '
132    fi
133    set result result . dns_reply_string(n, i)
134  done
135  dns_reply_release(n)
136  return result
137done
138
139func getmx (string domain; number resolve_names)
140  returns string
141do
142  string result ''
143  set n dns_query(DNS_TYPE_MX, domain, 0, resolve_names)
144  loop for set i 0,
145       while i < dns_reply_count(n),
146       set i i + 1
147  do
148    if result
149      set result result . ' '
150    fi
151    set result result . dns_reply_string(n, i)
152  done
153  dns_reply_release(n)
154  return result
155done
156
157
158