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_EXTENDED
) || (state
& HAS_UNDERSCORE
))
197 return DNS_ERROR_NON_RFC_NAME
;
198 if ((state
& HAS_SPACE
) ||
199 (state
& HAS_INVALID
) ||
200 (state
& HAS_ASTERISK
)) return DNS_ERROR_INVALID_NAME_CHAR
;
203 case DnsNameDomainLabel
:
205 if (state
& HAS_DOT
) return ERROR_INVALID_NAME
;
206 if ((state
& HAS_EXTENDED
) || (state
& HAS_UNDERSCORE
))
207 return DNS_ERROR_NON_RFC_NAME
;
208 if ((state
& HAS_SPACE
) ||
209 (state
& HAS_INVALID
) ||
210 (state
& HAS_ASTERISK
)) return DNS_ERROR_INVALID_NAME_CHAR
;
213 case DnsNameHostnameFull
:
215 if ((state
& HAS_EXTENDED
) || (state
& HAS_UNDERSCORE
))
216 return DNS_ERROR_NON_RFC_NAME
;
217 if ((state
& HAS_SPACE
) ||
218 (state
& HAS_INVALID
) ||
219 (state
& HAS_ASTERISK
)) return DNS_ERROR_INVALID_NAME_CHAR
;
222 case DnsNameHostnameLabel
:
224 if (state
& HAS_DOT
) return ERROR_INVALID_NAME
;
225 if ((state
& HAS_EXTENDED
) || (state
& HAS_UNDERSCORE
))
226 return DNS_ERROR_NON_RFC_NAME
;
227 if ((state
& HAS_SPACE
) ||
228 (state
& HAS_INVALID
) ||
229 (state
& HAS_ASTERISK
)) return DNS_ERROR_INVALID_NAME_CHAR
;
232 case DnsNameWildcard
:
234 if (!(state
& HAS_NON_NUMERIC
) && (state
& HAS_NUMERIC
))
235 return ERROR_INVALID_NAME
;
236 if (name
[0] != '*') return ERROR_INVALID_NAME
;
237 if (name
[1] && name
[1] != '.')
238 return DNS_ERROR_INVALID_NAME_CHAR
;
239 if ((state
& HAS_EXTENDED
) ||
240 (state
& HAS_SPACE
) ||
241 (state
& HAS_INVALID
)) return ERROR_INVALID_NAME
;
244 case DnsNameSrvRecord
:
246 if (!(state
& HAS_NON_NUMERIC
) && (state
& HAS_NUMERIC
))
247 return ERROR_INVALID_NAME
;
248 if (name
[0] != '_') return ERROR_INVALID_NAME
;
249 if ((state
& HAS_UNDERSCORE
) && !name
[1])
250 return DNS_ERROR_NON_RFC_NAME
;
251 if ((state
& HAS_EXTENDED
) ||
252 (state
& HAS_SPACE
) ||
253 (state
& HAS_INVALID
)) return ERROR_INVALID_NAME
;
257 WARN( "unknown format: %d\n", format
);
260 return ERROR_SUCCESS
;