libtld 2.0.16
A library to determine the Top-Level Domain name of any Internet URI.
tld.h
Go to the documentation of this file.
1/* TLD library -- TLD, domain name, and sub-domain extraction
2 * Copyright (c) 2011-2025 Made to Order Software Corp. All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef LIB_TLD_H
24#define LIB_TLD_H
34#if ( defined(LIBTLD_DLL) || defined(_WINDLL) ) && ( defined(_WINDOWS) || defined(WINDOWS) )
35#ifdef tld_EXPORTS
36#define LIBTLD_EXPORT __declspec(dllexport)
37#else
38#define LIBTLD_EXPORT __declspec(dllimport)
39#endif
40#else
41#define LIBTLD_EXPORT
42#endif
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48#define LIBTLD_VERSION_MAJOR 2
49#define LIBTLD_VERSION_MINOR 0
50#define LIBTLD_VERSION_PATCH 16
51#define LIBTLD_VERSION "2.0.16"
52
53#define LIBTLD_MAXIMUM_LABEL_LENGTH 63 /* this is an inclusive length, it can be between 1 and 63 */
54#define LIBTLD_MAXIMUM_URI_LENGTH 253 /* inclusive of the dots */
55
71
73{
80 TLD_STATUS_EXAMPLE,
82
83 /* The following status is returned when the 2nd level domain names
84 * were not used for some top domain names that now required a 2nd level.
85 * For example, the UK had parliament.uk as an exception. Since the
86 * UK now allows .uk to be used directly, those exceptions are gone.
87 *
88 * In other words, that entry is NOT a TLD. It is an actual website
89 * URI.
90 */
92};
93
95{
97 TLD_RESULT_INVALID, /* TLD not acceptable (reserved, deprecated, etc.) */
98 TLD_RESULT_NULL, /* URI is a NULL pointer or empty string */
99 TLD_RESULT_NO_TLD, /* no '.' in the URI */
100 TLD_RESULT_BAD_URI, /* URI includes two '.' one after another or some other problem */
103
105{
108 char f_country[48]; /* current longest is "South Georgia and the South Sandwich Islands" */
109 const char * f_tld; /* pointer within your URI string */
111 int f_tld_index;
112};
113
115{
116 const char * f_name;
117 int f_name_length;
118 const char * f_value;
119 int f_value_length;
120};
121
123{
124 int f_depth;
125 int f_offset[10];
126 char f_domain[64]; /* TODO: max. size needs to be verified */
127};
128
129#define VALID_URI_ASCII_ONLY 0x0001
130#define VALID_URI_NO_SPACES 0x0002
131
132/* defined in tld_file.h */
133struct tld_file;
134
135extern LIBTLD_EXPORT const char *tld_version();
136
137
138extern LIBTLD_EXPORT void tld_clear_info(struct tld_info * info);
139extern LIBTLD_EXPORT enum tld_result tld(const char *uri, struct tld_info * info);
140extern LIBTLD_EXPORT enum tld_result tld_load_tlds(const char *filename, int fallback);
141extern LIBTLD_EXPORT const struct tld_file * tld_get_tlds();
142extern LIBTLD_EXPORT void tld_free_tlds();
143extern LIBTLD_EXPORT enum tld_result tld_next_tld(struct tld_enumeration_state * state, struct tld_info * info);
144extern LIBTLD_EXPORT enum tld_result tld_check_uri(const char * uri, struct tld_info * info, const char *protocols, int flags);
145extern LIBTLD_EXPORT char * tld_domain_to_lowercase(const char *domain);
146extern LIBTLD_EXPORT int tld_tag_count(struct tld_info * info);
147extern LIBTLD_EXPORT enum tld_result tld_get_tag(struct tld_info * info, int tag_idx, struct tld_tag_definition * tag);
148extern LIBTLD_EXPORT const char * tld_status_to_string(enum tld_status status);
149extern LIBTLD_EXPORT enum tld_category tld_word_to_category(const char *word, int n);
150
151
152
154{
155 const char * f_group;
156 const char * f_original_email;
157 const char * f_fullname;
158 const char * f_username;
159 const char * f_domain;
160 const char * f_email_only;
162};
163
173
174struct tld_email_list;
175
178extern LIBTLD_EXPORT enum tld_result tld_email_parse(struct tld_email_list *list, const char *emails, int flags);
181extern LIBTLD_EXPORT int tld_email_next(struct tld_email_list *list, struct tld_email *e);
182
183
184#ifdef __cplusplus
185}
186#endif
187
188#ifdef __cplusplus
189/* For C++ users */
190#include <string>
191#include <vector>
192#include <stdexcept>
193
194
195struct invalid_domain : public std::runtime_error
196{
197 invalid_domain(const char *what_str = "this tld object is not currently valid") : runtime_error(what_str) {}
198};
199
200
202{
203public:
204 tld_object(const char *domain_name = NULL);
205 tld_object(const std::string& domain_name);
206 void set_domain(const char *domain_name);
207 void set_domain(const std::string& domain_name);
208 tld_result result() const;
209 tld_status status() const;
210 bool is_valid() const;
211 std::string domain() const;
212 std::string sub_domains() const;
213 std::string full_domain() const;
214 std::string domain_only() const;
215 std::string tld_only() const;
216 tld_category category() const;
217 std::string country() const;
218private:
219 std::string f_domain = std::string();
220 tld_info f_info = tld_info();
222};
223
224
226{
227public:
229 {
230 tld_result parse(const std::string& email);
231 tld_result parse_group(const std::string& group);
232
233 std::string f_group = std::string();
234 std::string f_original_email = std::string();
235 std::string f_fullname = std::string();
236 std::string f_username = std::string();
237 std::string f_domain = std::string();
238 std::string f_email_only = std::string();
239 std::string f_canonicalized_email = std::string();
240 };
241 typedef std::vector<tld_email_t> tld_email_list_t;
242
244 tld_result parse(const std::string& emails, int flags);
245 static std::string quote_string(const std::string& name, char quote);
246 int count() const;
247 void rewind() const;
248 bool next(tld_email_t& e) const;
249 bool next(tld_email *e) const;
250
251 static tld_email_field_type email_field_type(const std::string& name);
252
253private:
254 void parse_all_emails();
255
256 std::string f_input = std::string();
257 int f_flags = 0;
259 std::string f_last_group = std::string();
260 mutable int f_pos = 0;
262};
263#endif
264/*#ifdef __cplusplus*/
265
266#endif
267/*#ifndef LIB_TLD_H*/
268/* vim: ts=4 sw=4 et
269 */
Class used to ease the use o the tld() function in C++.
Definition tld.h:202
Exception thrown when querying for data of an invalid domain.
Definition tld.h:196
invalid_domain(const char *what_str="this tld object is not currently valid")
Initialize the invalid_domain exception.
Definition tld.h:197
Parts of one email.
Definition tld.h:229
The C++ side of the email list implementation.
Definition tld.h:226
std::vector< tld_email_t > tld_email_list_t
A vector of email details.
Definition tld.h:241
Parts of one email.
Definition tld.h:154
const char * f_group
The group this emails was defined in.
Definition tld.h:155
const char * f_canonicalized_email
The email including the display name.
Definition tld.h:161
const char * f_original_email
The email as read from the source.
Definition tld.h:156
const char * f_email_only
The complete email address without display name.
Definition tld.h:160
const char * f_username
The user being named in this email address.
Definition tld.h:158
const char * f_domain
The domain part of the email address.
Definition tld.h:159
const char * f_fullname
The user full or display name.
Definition tld.h:157
Set of information returned by the tld() function.
Definition tld.h:105
enum tld_category f_category
The category of the TLD.
Definition tld.h:106
enum tld_status f_status
The status of the TLD.
Definition tld.h:107
int f_offset
The offset to the TLD in the URI string you supplied.
Definition tld.h:110
char f_country[48]
The country where this TLD is used.
Definition tld.h:108
const char * f_tld
Pointer to the TLD in the URI string you supplied.
Definition tld.h:109
LIBTLD_EXPORT char * tld_domain_to_lowercase(const char *domain)
Transform a domain with a TLD to lowercase before processing.
tld_email_field_type
Definition tld.h:165
@ TLD_EMAIL_FIELD_TYPE_UNKNOWN
The input does not represent valid emails.
Definition tld.h:167
@ TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST
The input represents a mandatory list of mailboxes.
Definition tld.h:170
@ TLD_EMAIL_FIELD_TYPE_MAILBOX
The input represents a mailbox.
Definition tld.h:169
@ TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT
The input represents an optional list of email addresses.
Definition tld.h:171
@ TLD_EMAIL_FIELD_TYPE_INVALID
The input of email_field_type() was not valid.
Definition tld.h:166
@ TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST
The input represents a mailbox list.
Definition tld.h:168
LIBTLD_EXPORT void tld_email_free(struct tld_email_list *list)
Free the list of emails.
LIBTLD_EXPORT enum tld_result tld(const char *uri, struct tld_info *info)
Get information about the TLD for the specified URI.
Definition tld.cpp:1113
LIBTLD_EXPORT const char * tld_status_to_string(enum tld_status status)
Transform the status to a string.
Definition tld_strings.c:49
#define LIBTLD_EXPORT
The export API used by MS-Windows DLLs.
Definition tld.h:41
tld_category
Definition tld.h:57
@ TLD_CATEGORY_PROFESSIONALS
Professional TLDs.
Definition tld.h:59
@ TLD_CATEGORY_LANGUAGE
Language specific TLDs.
Definition tld.h:60
@ TLD_CATEGORY_BRAND
The TLD is owned and represents a brand.
Definition tld.h:67
@ TLD_CATEGORY_COUNTRY
A country extension.
Definition tld.h:64
@ TLD_CATEGORY_GROUP
Groups specific TLDs.
Definition tld.h:61
@ TLD_CATEGORY_TECHNICAL
Technical extensions are considered internal.
Definition tld.h:63
@ TLD_CATEGORY_LOCATION
Another region specific TLDs.
Definition tld.h:65
@ TLD_CATEGORY_INTERNATIONAL
International TLDs.
Definition tld.h:58
@ TLD_CATEGORY_UNDEFINED
The TLD was not found.
Definition tld.h:69
@ TLD_CATEGORY_ENTREPRENEURIAL
A private extension.
Definition tld.h:66
@ TLD_CATEGORY_REGION
Region specific TLDs.
Definition tld.h:62
@ TLD_CATEGORY_CONTACT
The attached TLD has contact information.
Definition tld.h:68
LIBTLD_EXPORT void tld_email_rewind(struct tld_email_list *list)
Rewind the reading of the emails.
LIBTLD_EXPORT struct tld_email_list * tld_email_alloc()
Allocate a list of emails object.
LIBTLD_EXPORT const char * tld_version()
Return the version of the library.
Definition tld.cpp:1654
LIBTLD_EXPORT int tld_email_count(struct tld_email_list *list)
Return the number of emails found after a parse.
LIBTLD_EXPORT enum tld_result tld_email_parse(struct tld_email_list *list, const char *emails, int flags)
Parse a list of emails in the email list object.
LIBTLD_EXPORT void tld_clear_info(struct tld_info *info)
Clear the info structure.
Definition tld.cpp:705
LIBTLD_EXPORT enum tld_result tld_load_tlds(const char *filename, int fallback)
Load a TLDs file as the file to be used by the tld() function.
Definition tld.cpp:744
LIBTLD_EXPORT const struct tld_file * tld_get_tlds()
Return a pointer to the current list of TLDs.
Definition tld.cpp:809
LIBTLD_EXPORT enum tld_category tld_word_to_category(const char *word, int n)
This is for backward compatibility.
LIBTLD_EXPORT enum tld_result tld_next_tld(struct tld_enumeration_state *state, struct tld_info *info)
Read the next TLD and return its info.
Definition tld.cpp:888
LIBTLD_EXPORT enum tld_result tld_check_uri(const char *uri, struct tld_info *info, const char *protocols, int flags)
Check that a URI is valid.
Definition tld.cpp:1319
LIBTLD_EXPORT void tld_free_tlds()
Clear the allocated TLD file.
Definition tld.cpp:828
tld_result
Definition tld.h:95
@ TLD_RESULT_SUCCESS
Success! The TLD of the specified URI is valid.
Definition tld.h:96
@ TLD_RESULT_NO_TLD
The input URI has no TLD defined.
Definition tld.h:99
@ TLD_RESULT_INVALID
The TLD was found, but it is marked as invalid.
Definition tld.h:97
@ TLD_RESULT_BAD_URI
The URI includes characters that are not accepted by the function.
Definition tld.h:100
@ TLD_RESULT_NOT_FOUND
The URI has a TLD that could not be determined.
Definition tld.h:101
@ TLD_RESULT_NULL
The input URI is empty.
Definition tld.h:98
tld_status
Definition tld.h:73
@ TLD_STATUS_EXCEPTION
Special status to indicate an exception which is not directly a TLD.
Definition tld.h:91
@ TLD_STATUS_UNDEFINED
Special status to indicate we did not find the TLD.
Definition tld.h:81
@ TLD_STATUS_RESERVED
The TLD is reserved so no one can use it.
Definition tld.h:78
@ TLD_STATUS_VALID
The TLD is currently valid.
Definition tld.h:74
@ TLD_STATUS_INFRASTRUCTURE
These TLDs are reserved for the Internet infrastructure.
Definition tld.h:79
@ TLD_STATUS_UNUSED
The TLD was officially assigned but not put to use.
Definition tld.h:77
@ TLD_STATUS_DEPRECATED
The TLD was once in use.
Definition tld.h:76
@ TLD_STATUS_PROPOSED
The TLD was proposed but not yet accepted.
Definition tld.h:75
LIBTLD_EXPORT int tld_email_next(struct tld_email_list *list, struct tld_email *e)
Retrieve the next email.
void list()
List the default schemes accepted.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.