push c03b0bb7c67acf502e715114ece376179b90ac7c
[wine/hacks.git] / dlls / dnsapi / name.c
blob32620506fa268c40943cf301e1844e5f91e732f7
1 /*
2 * DNS support
4 * Copyright (C) 2006 Matthew Kehrer
5 * Copyright (C) 2006 Hans Leidekker
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
26 #include <stdarg.h>
27 #include <sys/types.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
34 # undef NOERROR
35 #endif
36 #ifdef HAVE_RESOLV_H
37 # include <resolv.h>
38 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winerror.h"
43 #include "winnls.h"
44 #include "windns.h"
46 #include "dnsapi.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi);
50 /******************************************************************************
51 * DnsNameCompare_A [DNSAPI.@]
54 BOOL WINAPI DnsNameCompare_A( PCSTR name1, PCSTR name2 )
56 BOOL ret;
57 PWSTR name1W, name2W;
59 TRACE( "(%s,%s)\n", debugstr_a(name1), debugstr_a(name2) );
61 name1W = dns_strdup_aw( name1 );
62 name2W = dns_strdup_aw( name2 );
64 ret = DnsNameCompare_W( name1W, name2W );
66 heap_free( name1W );
67 heap_free( name2W );
69 return ret;
72 /******************************************************************************
73 * DnsNameCompare_W [DNSAPI.@]
76 BOOL WINAPI DnsNameCompare_W( PCWSTR name1, PCWSTR name2 )
78 PCWSTR p, q;
80 TRACE( "(%s,%s)\n", debugstr_w(name1), debugstr_w(name2) );
82 if (!name1 && !name2) return TRUE;
83 if (!name1 || !name2) return FALSE;
85 p = name1 + lstrlenW( name1 ) - 1;
86 q = name2 + lstrlenW( name2 ) - 1;
88 while (*p == '.' && p >= name1) p--;
89 while (*q == '.' && q >= name2) q--;
91 if (p - name1 != q - name2) return FALSE;
93 while (name1 <= p)
95 if (toupperW( *name1 ) != toupperW( *name2 ))
96 return FALSE;
98 name1++;
99 name2++;
101 return TRUE;
104 /******************************************************************************
105 * DnsValidateName_A [DNSAPI.@]
108 DNS_STATUS WINAPI DnsValidateName_A( PCSTR name, DNS_NAME_FORMAT format )
110 PWSTR nameW;
111 DNS_STATUS ret;
113 TRACE( "(%s, %d)\n", debugstr_a(name), format );
115 nameW = dns_strdup_aw( name );
116 ret = DnsValidateName_W( nameW, format );
118 heap_free( nameW );
119 return ret;
122 /******************************************************************************
123 * DnsValidateName_UTF8 [DNSAPI.@]
126 DNS_STATUS WINAPI DnsValidateName_UTF8( PCSTR name, DNS_NAME_FORMAT format )
128 PWSTR nameW;
129 DNS_STATUS ret;
131 TRACE( "(%s, %d)\n", debugstr_a(name), format );
133 nameW = dns_strdup_uw( name );
134 ret = DnsValidateName_W( nameW, format );
136 heap_free( nameW );
137 return ret;
140 #define HAS_EXTENDED 0x0001
141 #define HAS_NUMERIC 0x0002
142 #define HAS_NON_NUMERIC 0x0004
143 #define HAS_DOT 0x0008
144 #define HAS_DOT_DOT 0x0010
145 #define HAS_SPACE 0x0020
146 #define HAS_INVALID 0x0040
147 #define HAS_ASTERISK 0x0080
148 #define HAS_UNDERSCORE 0x0100
149 #define HAS_LONG_LABEL 0x0200
151 /******************************************************************************
152 * DnsValidateName_W [DNSAPI.@]
155 DNS_STATUS WINAPI DnsValidateName_W( PCWSTR name, DNS_NAME_FORMAT format )
157 PCWSTR p;
158 unsigned int i, j, state = 0;
159 static const WCHAR invalid[] = {
160 '{','|','}','~','[','\\',']','^','\'',':',';','<','=','>',
161 '?','@','!','\"','#','$','%','^','`','(',')','+','/',',',0 };
163 TRACE( "(%s, %d)\n", debugstr_w(name), format );
165 if (!name) return ERROR_INVALID_NAME;
167 for (p = name, i = 0, j = 0; *p; p++, i++, j++)
169 if (*p == '.')
171 j = 0;
172 state |= HAS_DOT;
173 if (p[1] == '.') state |= HAS_DOT_DOT;
175 else if (*p < '0' || *p > '9') state |= HAS_NON_NUMERIC;
176 else state |= HAS_NUMERIC;
178 if (j > 62) state |= HAS_LONG_LABEL;
180 if (strchrW( invalid, *p )) state |= HAS_INVALID;
181 else if ((unsigned)*p > 127) state |= HAS_EXTENDED;
182 else if (*p == ' ') state |= HAS_SPACE;
183 else if (*p == '_') state |= HAS_UNDERSCORE;
184 else if (*p == '*') state |= HAS_ASTERISK;
187 if (i == 0 || i > 255 ||
188 (state & HAS_LONG_LABEL) ||
189 (state & HAS_DOT_DOT) ||
190 (name[0] == '.' && name[1])) return ERROR_INVALID_NAME;
192 switch (format)
194 case DnsNameDomain:
196 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
197 return DNS_ERROR_NUMERIC_NAME;
198 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
199 return DNS_ERROR_NON_RFC_NAME;
200 if ((state & HAS_SPACE) ||
201 (state & HAS_INVALID) ||
202 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
203 break;
205 case DnsNameDomainLabel:
207 if (state & HAS_DOT) return ERROR_INVALID_NAME;
208 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
209 return DNS_ERROR_NON_RFC_NAME;
210 if ((state & HAS_SPACE) ||
211 (state & HAS_INVALID) ||
212 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
213 break;
215 case DnsNameHostnameFull:
217 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
218 return DNS_ERROR_NUMERIC_NAME;
219 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
220 return DNS_ERROR_NON_RFC_NAME;
221 if ((state & HAS_SPACE) ||
222 (state & HAS_INVALID) ||
223 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
224 break;
226 case DnsNameHostnameLabel:
228 if (state & HAS_DOT) return ERROR_INVALID_NAME;
229 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
230 return DNS_ERROR_NUMERIC_NAME;
231 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
232 return DNS_ERROR_NON_RFC_NAME;
233 if ((state & HAS_SPACE) ||
234 (state & HAS_INVALID) ||
235 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
236 break;
238 case DnsNameWildcard:
240 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
241 return ERROR_INVALID_NAME;
242 if (name[0] != '*') return ERROR_INVALID_NAME;
243 if (name[1] && name[1] != '.')
244 return DNS_ERROR_INVALID_NAME_CHAR;
245 if ((state & HAS_EXTENDED) ||
246 (state & HAS_SPACE) ||
247 (state & HAS_INVALID)) return ERROR_INVALID_NAME;
248 break;
250 case DnsNameSrvRecord:
252 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
253 return ERROR_INVALID_NAME;
254 if (name[0] != '_') return ERROR_INVALID_NAME;
255 if ((state & HAS_UNDERSCORE) && !name[1])
256 return DNS_ERROR_NON_RFC_NAME;
257 if ((state & HAS_EXTENDED) ||
258 (state & HAS_SPACE) ||
259 (state & HAS_INVALID)) return ERROR_INVALID_NAME;
260 break;
262 default:
263 WARN( "unknown format: %d\n", format );
264 break;
266 return ERROR_SUCCESS;