4 * Copyright (C) 2006 Matthew Kehrer
5 * Copyright (C) 2006 Hans Leidekker
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
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
27 #include <sys/types.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
48 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi
);
50 /******************************************************************************
51 * DnsNameCompare_A [DNSAPI.@]
54 BOOL WINAPI
DnsNameCompare_A( PCSTR name1
, PCSTR name2
)
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
);
72 /******************************************************************************
73 * DnsNameCompare_W [DNSAPI.@]
76 BOOL WINAPI
DnsNameCompare_W( PCWSTR name1
, PCWSTR name2
)
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
;
95 if (toupperW( *name1
) != toupperW( *name2
))
104 /******************************************************************************
105 * DnsValidateName_A [DNSAPI.@]
108 DNS_STATUS WINAPI
DnsValidateName_A( PCSTR name
, DNS_NAME_FORMAT format
)
113 TRACE( "(%s, %d)\n", debugstr_a(name
), format
);
115 nameW
= dns_strdup_aw( name
);
116 ret
= DnsValidateName_W( nameW
, format
);
122 /******************************************************************************
123 * DnsValidateName_UTF8 [DNSAPI.@]
126 DNS_STATUS WINAPI
DnsValidateName_UTF8( PCSTR name
, DNS_NAME_FORMAT format
)
131 TRACE( "(%s, %d)\n", debugstr_a(name
), format
);
133 nameW
= dns_strdup_uw( name
);
134 ret
= DnsValidateName_W( nameW
, format
);
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
)
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
++)
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
;
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
;
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
;
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
;
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
;
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
;
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
;
263 WARN( "unknown format: %d\n", format
);
266 return ERROR_SUCCESS
;