C Library to parse public suffix list (PSL)
Before compiling the library, you must update PSL data (psl.dat file) by downloading the latest file from here
See the full documentation here
## you must have libidn2 installed before compiling # will make the library and binary files in ./bin folder make # ro run the test make test # build the doc doxygen Doxyfile
#include <libctld.h> #include <stdio.h> // can manually compile it with: // gcc -o example example.c -I./include src/cdict.c src/cstrlib.c src/clist.c src/libctld.c -lidn2 int main(int argc, char ** argv){ char * psl_data = "psl.dat"; ctld_ctx * ctx = ctld_parse_file(psl_data); if (!ctx){ fprintf(stderr, "Can not create CTLD context\n"); return 1; } ctld_result * res = ctld_parse(ctx, "account.google.com", 0); printf("suffix: %s\n", res->suffix); // com printf("registered domain: %s\n", res->registered_domain); // google.com printf("fqdn: %s\n", res->fqdn); // account.google.com printf("domain: %s\n", res->domain); // google ctld_result_free(res); ctld_free(ctx); return 0; }
If you want to compile the code and use the module in Lua, use the following command to compile it.
gcc -c -Wall -Werror -shared -fpic -Wl,-E src/lua_libctld.c src/libctld.c src/cstrlib.c src/clist.c src/cdict.c -I./include -I/usr/include/lua5.4 && gcc -shared -o libctld.so *.o -llua5.4 -lm -lidn2 && rm -f *.o
Here is an example of using the library in lua:
-- you must have psl.data file in this directory -- you must have libctld.so in this directory psl = require "libctld" inspect = require "inspect" p = psl.init("psl.dat") result, err, msg = psl.parse(p, "google.com", 0) print(inspect(result)) print(inspect(err)) print(inspect(msg)) --[[ { domain = "google", fqdn = "google.com", registered_domain = "google.com", suffix = "com" } nil nil ]]
-
void ctld_result_free(ctld_result *res)
-
void ctld_free(ctld_ctx *ctx)
-
ctld_ctx * ctld_parse_string(char *data)
-
ctld_ctx * ctld_parse_file(char *filename)
-
int ctld_is_domain_valid(char *domain)
-
ctld_result * ctld_parse(ctld_ctx *ctx, char *domain, int use_private_suffix)
-
int ctld_add_custom_suffix(ctld_ctx *ctx, char * suffix)
After making the project, the binary file generated in the bin directory named ctld. This is a command line tool for parsing and working with domain names in bash. The input to the command-line tool can be a fqdn, domain or URL.
Here is the supported options for the binary.
bash:~$ ctld -h [Help] Summary: cTLD: Top Level Domain parser based on Public Suffix List (psl). ctld [OPTIONS] FILE --tld Print suffix --rd Print registered domain --fqdn Print fully-qualified-domain-name --private Use private suffix list as well --err Print Errors only --custom=<param> Add a comma-separated list of custom suffixes (no space) -h , --help Print this help message -v , --version Print suffix