libtld 2.0.14
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 14
51#define LIBTLD_VERSION "2.0.14"
52
68
70{
77 TLD_STATUS_EXAMPLE,
79
80 /* The following status is returned when the 2nd level domain names
81 * were not used for some top domain names that now required a 2nd level.
82 * For example, the UK had parliament.uk as an exception. Since the
83 * UK now allows .uk to be used directly, those exceptions are gone.
84 *
85 * In other words, that entry is NOT a TLD. It is an actual website
86 * URI.
87 */
89};
90
92{
94 TLD_RESULT_INVALID, /* TLD not acceptable (reserved, deprecated, etc.) */
95 TLD_RESULT_NULL, /* URI is a NULL pointer */
96 TLD_RESULT_NO_TLD, /* no '.' in the URI */
97 TLD_RESULT_BAD_URI, /* URI includes two '.' one after another or some other problem */
99};
100
102{
105 char f_country[48]; /* current longest is "South Georgia and the South Sandwich Islands" */
106 const char * f_tld; /* pointer within your URI string */
108 int f_tld_index;
109};
110
112{
113 const char * f_name;
114 int f_name_length;
115 const char * f_value;
116 int f_value_length;
117};
118
120{
121 int f_depth;
122 int f_offset[10];
123 char f_domain[64]; /* TODO: max. size needs to be verified */
124};
125
126#define VALID_URI_ASCII_ONLY 0x0001
127#define VALID_URI_NO_SPACES 0x0002
128
129/* defined in tld_file.h */
130struct tld_file;
131
132extern LIBTLD_EXPORT const char *tld_version();
133
134
135extern LIBTLD_EXPORT void tld_clear_info(struct tld_info * info);
136extern LIBTLD_EXPORT enum tld_result tld(const char *uri, struct tld_info * info);
137extern LIBTLD_EXPORT enum tld_result tld_load_tlds(const char *filename, int fallback);
138extern LIBTLD_EXPORT const struct tld_file * tld_get_tlds();
139extern LIBTLD_EXPORT void tld_free_tlds();
140extern LIBTLD_EXPORT enum tld_result tld_next_tld(struct tld_enumeration_state * state, struct tld_info * info);
141extern LIBTLD_EXPORT enum tld_result tld_check_uri(const char * uri, struct tld_info * info, const char *protocols, int flags);
142extern LIBTLD_EXPORT char * tld_domain_to_lowercase(const char *domain);
143extern LIBTLD_EXPORT int tld_tag_count(struct tld_info * info);
144extern LIBTLD_EXPORT enum tld_result tld_get_tag(struct tld_info * info, int tag_idx, struct tld_tag_definition * tag);
145extern LIBTLD_EXPORT const char * tld_status_to_string(enum tld_status status);
146extern LIBTLD_EXPORT enum tld_category tld_word_to_category(const char *word, int n);
147
148
149
151{
152 const char * f_group;
153 const char * f_original_email;
154 const char * f_fullname;
155 const char * f_username;
156 const char * f_domain;
157 const char * f_email_only;
159};
160
170
171struct tld_email_list;
172
175extern LIBTLD_EXPORT enum tld_result tld_email_parse(struct tld_email_list *list, const char *emails, int flags);
178extern LIBTLD_EXPORT int tld_email_next(struct tld_email_list *list, struct tld_email *e);
179
180
181#ifdef __cplusplus
182}
183#endif
184
185#ifdef __cplusplus
186/* For C++ users */
187#include <string>
188#include <vector>
189#include <stdexcept>
190
191
192struct invalid_domain : public std::runtime_error
193{
194 invalid_domain(const char *what_str = "this tld object is not currently valid") : runtime_error(what_str) {}
195};
196
197
199{
200public:
201 tld_object(const char *domain_name = NULL);
202 tld_object(const std::string& domain_name);
203 void set_domain(const char *domain_name);
204 void set_domain(const std::string& domain_name);
205 tld_result result() const;
206 tld_status status() const;
207 bool is_valid() const;
208 std::string domain() const;
209 std::string sub_domains() const;
210 std::string full_domain() const;
211 std::string domain_only() const;
212 std::string tld_only() const;
213 tld_category category() const;
214 std::string country() const;
215private:
216 std::string f_domain = std::string();
217 tld_info f_info = tld_info();
219};
220
221
223{
224public:
226 {
227 tld_result parse(const std::string& email);
228 tld_result parse_group(const std::string& group);
229
230 std::string f_group = std::string();
231 std::string f_original_email = std::string();
232 std::string f_fullname = std::string();
233 std::string f_username = std::string();
234 std::string f_domain = std::string();
235 std::string f_email_only = std::string();
236 std::string f_canonicalized_email = std::string();
237 };
238 typedef std::vector<tld_email_t> tld_email_list_t;
239
241 tld_result parse(const std::string& emails, int flags);
242 static std::string quote_string(const std::string& name, char quote);
243 int count() const;
244 void rewind() const;
245 bool next(tld_email_t& e) const;
246 bool next(tld_email *e) const;
247
248 static tld_email_field_type email_field_type(const std::string& name);
249
250private:
251 void parse_all_emails();
252
253 std::string f_input = std::string();
254 int f_flags = 0;
256 std::string f_last_group = std::string();
257 mutable int f_pos = 0;
259};
260#endif
261/*#ifdef __cplusplus*/
262
263#endif
264/*#ifndef LIB_TLD_H*/
265/* vim: ts=4 sw=4 et
266 */
Class used to ease the use o the tld() function in C++.
Definition tld.h:199
Exception thrown when querying for data of an invalid domain.
Definition tld.h:193
invalid_domain(const char *what_str="this tld object is not currently valid")
Initialize the invalid_domain exception.
Definition tld.h:194
Parts of one email.
Definition tld.h:226
The C++ side of the email list implementation.
Definition tld.h:223
std::vector< tld_email_t > tld_email_list_t
A vector of email details.
Definition tld.h:238
Parts of one email.
Definition tld.h:151
const char * f_group
The group this emails was defined in.
Definition tld.h:152
const char * f_canonicalized_email
The email including the display name.
Definition tld.h:158
const char * f_original_email
The email as read from the source.
Definition tld.h:153
const char * f_email_only
The complete email address without display name.
Definition tld.h:157
const char * f_username
The user being named in this email address.
Definition tld.h:155
const char * f_domain
The domain part of the email address.
Definition tld.h:156
const char * f_fullname
The user full or display name.
Definition tld.h:154
Set of information returned by the tld() function.
Definition tld.h:102
enum tld_category f_category
The category of the TLD.
Definition tld.h:103
enum tld_status f_status
The status of the TLD.
Definition tld.h:104
int f_offset
The offset to the TLD in the URI string you supplied.
Definition tld.h:107
char f_country[48]
The country where this TLD is used.
Definition tld.h:105
const char * f_tld
Pointer to the TLD in the URI string you supplied.
Definition tld.h:106
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:162
@ TLD_EMAIL_FIELD_TYPE_UNKNOWN
The input does not represent valid emails.
Definition tld.h:164
@ TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST
The input represents a mandatory list of mailboxes.
Definition tld.h:167
@ TLD_EMAIL_FIELD_TYPE_MAILBOX
The input represents a mailbox.
Definition tld.h:166
@ TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT
The input represents an optional list of email addresses.
Definition tld.h:168
@ TLD_EMAIL_FIELD_TYPE_INVALID
The input of email_field_type() was not valid.
Definition tld.h:163
@ TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST
The input represents a mailbox list.
Definition tld.h:165
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:54
@ TLD_CATEGORY_PROFESSIONALS
Professional TLDs.
Definition tld.h:56
@ TLD_CATEGORY_LANGUAGE
Language specific TLDs.
Definition tld.h:57
@ TLD_CATEGORY_BRAND
The TLD is owned and represents a brand.
Definition tld.h:64
@ TLD_CATEGORY_COUNTRY
A country extension.
Definition tld.h:61
@ TLD_CATEGORY_GROUP
Groups specific TLDs.
Definition tld.h:58
@ TLD_CATEGORY_TECHNICAL
Technical extensions are considered internal.
Definition tld.h:60
@ TLD_CATEGORY_LOCATION
Another region specific TLDs.
Definition tld.h:62
@ TLD_CATEGORY_INTERNATIONAL
International TLDs.
Definition tld.h:55
@ TLD_CATEGORY_UNDEFINED
The TLD was not found.
Definition tld.h:66
@ TLD_CATEGORY_ENTREPRENEURIAL
A private extension.
Definition tld.h:63
@ TLD_CATEGORY_REGION
Region specific TLDs.
Definition tld.h:59
@ TLD_CATEGORY_CONTACT
The attached TLD has contact information.
Definition tld.h:65
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:1646
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:1311
LIBTLD_EXPORT void tld_free_tlds()
Clear the allocated TLD file.
Definition tld.cpp:828
tld_result
Definition tld.h:92
@ TLD_RESULT_SUCCESS
Success! The TLD of the specified URI is valid.
Definition tld.h:93
@ TLD_RESULT_NO_TLD
The input URI has no TLD defined.
Definition tld.h:96
@ TLD_RESULT_INVALID
The TLD was found, but it is marked as invalid.
Definition tld.h:94
@ TLD_RESULT_BAD_URI
The URI includes characters that are not accepted by the function.
Definition tld.h:97
@ TLD_RESULT_NOT_FOUND
The URI has a TLD that could not be determined.
Definition tld.h:98
@ TLD_RESULT_NULL
The input URI is empty.
Definition tld.h:95
tld_status
Definition tld.h:70
@ TLD_STATUS_EXCEPTION
Special status to indicate an exception which is not directly a TLD.
Definition tld.h:88
@ TLD_STATUS_UNDEFINED
Special status to indicate we did not find the TLD.
Definition tld.h:78
@ TLD_STATUS_RESERVED
The TLD is reserved so no one can use it.
Definition tld.h:75
@ TLD_STATUS_VALID
The TLD is currently valid.
Definition tld.h:71
@ TLD_STATUS_INFRASTRUCTURE
These TLDs are reserved for the Internet infrastructure.
Definition tld.h:76
@ TLD_STATUS_UNUSED
The TLD was officially assigned but not put to use.
Definition tld.h:74
@ TLD_STATUS_DEPRECATED
The TLD was once in use.
Definition tld.h:73
@ TLD_STATUS_PROPOSED
The TLD was proposed but not yet accepted.
Definition tld.h:72
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.