libtld 2.0.14
A library to determine the Top-Level Domain name of any Internet URI.
validate_tld.cpp
Go to the documentation of this file.
1/* TLD library -- TLD validation command line tools
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
31#include "libtld/tld.h"
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <iostream>
36
38int err_count = 0;
39
41int verbose = 0;
42
53const char *schemes = "afp,adiumxtra,aw,beshare,bolo,cap,coap,crid,dns,feed,file,"
54 "finger,fish,ftp,ftps,git,gopher,http,https,icap,imap,"
55 "ipp,irc,irc6,ircs,mumble,mupdate,mysql,nfs,nntp,"
56 "opaquelocktoken,pop,psql,psyc,rmi,rsync,rtmp,rtsp,rtspu,"
57 "sftp,shttp,sieve,smb,snmp,soap.beep,soap.beeps,soldat,"
58 "ssh,teamspeak,telnet,tftp,tip,udp,unreal,ut2004,vemmi,"
59 "ventrilo,wais,webcal,wyciwyg,z39.50r,z39.50s";
60
62char const * user_schemes = nullptr;
63
70void check_uri(char const * uri)
71{
72 tld_result result;
73 if(strncasecmp(uri, "mailto:", 7) == 0)
74 {
75 tld_email_list mail;
76 result = mail.parse(uri + 7, 0);
77 }
78 else
79 {
80 struct tld_info info;
81 char const * s(user_schemes == nullptr ? schemes : user_schemes);
82 result = tld_check_uri(uri, &info, s, 0);
83
84 if(verbose)
85 {
86 std::cout << "URI: " << uri << std::endl; // LCOV_EXCL_LINE
87 std::cout << "Category: " << static_cast<int>(info.f_category) << std::endl; // LCOV_EXCL_LINE
88 std::cout << "Status: " << static_cast<int>(info.f_status) << std::endl; // LCOV_EXCL_LINE
89 if(info.f_country[0] != '\0') // LCOV_EXCL_LINE
90 {
91 std::cout << "Country: " << info.f_country << std::endl; // LCOV_EXCL_LINE
92 }
93 if(info.f_tld != nullptr)
94 {
95 // port or path may follow this TLD
96 //
97 char const * e(strchr(info.f_tld, ':'));
98 if(e == nullptr)
99 {
100 e = strchr(info.f_tld, '/');
101 }
102 if(e == nullptr)
103 {
104 std::cout << "TLD: " << info.f_tld << std::endl; // LCOV_EXCL_LINE
105 }
106 else
107 {
108 std::cout << "TLD: " << std::string(info.f_tld, e - info.f_tld) << std::endl; // LCOV_EXCL_LINE
109 }
110 std::cout << "Offset: " << info.f_offset << std::endl; // LCOV_EXCL_LINE
111 }
112 }
113 }
114 if(result != TLD_RESULT_SUCCESS)
115 {
116 std::cerr << "error: URI \"" << uri << "\" is not considered valid." << std::endl;
117 ++err_count;
118 }
119}
120
125void list()
126{
127 for(const char *s(schemes); *s != '\0'; ++s)
128 {
129 if(*s == ',')
130 {
131 printf("\n");
132 }
133 else
134 {
135 printf("%c", *s);
136 }
137 }
138 printf("\n");
139 exit(1);
140}
141
147void usage()
148{
149 printf("Usage: validate_tld [-<opts>] <uri> | <email>\n");
150 printf("Where <uri> or <email> are URIs starting with a valid scheme.\n");
151 printf("The <email> scheme is mailto:.\n");
152 printf("Where -<opts> are:\n");
153 printf(" -h | --help print out this help screen\n");
154 printf(" -l | --list print the default list of schemes\n");
155 printf(" -s | --schemes <list> set the list of schemes with user's defined schemes\n");
156 printf(" the list is a comma separate set of scheme names\n");
157 printf(" -v | --verbose request some verbosity of the tool's work\n");
158 exit(1);
159}
160
172int main(int argc, char *argv[])
173{
174 try
175 {
176 bool uri(false);
177
178 for(int i(1); i < argc; ++i)
179 {
180 if(argv[i][0] == '-')
181 {
182 if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
183 {
184 usage();
185 /*NOTREACHED*/
186 }
187 else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0)
188 {
189 list();
190 /*NOTREACHED*/
191 }
192 else if(strcmp(argv[i], "--version") == 0)
193 {
194 printf("%s\n", LIBTLD_VERSION);
195 if(verbose)
196 {
197 printf("libtld v%s\n", tld_version());
198 }
199 exit(1);
200 }
201 else if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--schemes") == 0)
202 {
203 ++i;
204 if(i >= argc)
205 {
206 fprintf(stderr, "error: the --schemes option requires a list of comma separated schemes.\n");
207 }
208 user_schemes = argv[i];
209 }
210 else if(strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0)
211 {
212 verbose = 1;
213 }
214 }
215 else
216 {
217 uri = true;
218 check_uri(argv[i]);
219 }
220 }
221
222 if(!uri)
223 {
224 fprintf(stderr, "error: no URI was specified on the command line.\n");
225 ++err_count;
226 }
227
228 return err_count > 0 ? 1 : 0;
229 }
230 catch(std::exception const& e) // LCOV_EXCL_LINE
231 {
232 // an exception occurred, print out the message and exit with an error
233 //
234 // note that this tool is not expecting any exception
235 // because we only access the C interface which does
236 // not throw
237 //
238 std::cerr << "exception: " << e.what() << std::endl; // LCOV_EXCL_LINE
239 exit(1); // LCOV_EXCL_LINE
240 }
241}
242
243// vim: ts=4 sw=4 et
The C++ side of the email list implementation.
Definition tld.h:223
tld_result parse(const std::string &emails, int flags)
Parse a new list of emails.
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
The public header of the libtld library.
#define LIBTLD_VERSION
The version of the library as a string.
Definition tld.h:51
LIBTLD_EXPORT const char * tld_version()
Return the version of the library.
Definition tld.cpp:1646
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
tld_result
Definition tld.h:92
@ TLD_RESULT_SUCCESS
Success! The TLD of the specified URI is valid.
Definition tld.h:93
int verbose
Whether the user asked for verbosity, false by default.
int main(int argc, char *argv[])
The validate tools.
void usage()
Print out the help of the tld tool.
char const * user_schemes
Hold a list of schemes as defined by the end user.
int err_count
Number of errors so we know whether to exit with 0 or 1.
void list()
List the default schemes accepted.
const char * schemes
List of schemes that we more or less support (some schemes have extensions using the semi-colon that ...
void check_uri(char const *uri)
Check the parameter as a URI.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.