Skip to content

Top Level Domain parser based on Public Suffix List (PSL)

Notifications You must be signed in to change notification settings

maroofi/libctld

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LIBTLD

C Library to parse public suffix list (PSL)

PSL data

Before compiling the library, you must update PSL data (psl.dat file) by downloading the latest file from here

Documentation

See the full documentation here

Compile and test

## 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

How to use

#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;
}

Exposed API

  • 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)

ctld binary file

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