2016-11-24 22:52:19 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "ipfs/namesys/namesys.h"
|
|
|
|
#include "ipfs/namesys/isdomain.h"
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
void ipfs_isdomain_to_upper(char *dst, char *src)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
|
|
|
while(*src) {
|
|
|
|
*dst++ = toupper(*src++);
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_has_suffix (char *s, char *suf)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = s + strlen(s) - strlen(suf);
|
|
|
|
return strcmp(p, suf) == 0;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_is_at_array(tlds *a, char *s)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
|
|
|
char str[strlen(s)+1];
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
ipfs_isdomain_to_upper(str, s);
|
2016-11-24 22:52:19 +00:00
|
|
|
while(a->str) {
|
|
|
|
if (strcmp(a->str, str) == 0) {
|
|
|
|
return a->condition;
|
|
|
|
}
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_match_string (char *d)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
|
|
|
char str[strlen(d)+1], *p = str, *l;
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
ipfs_isdomain_to_upper(str, d);
|
2016-11-24 22:52:19 +00:00
|
|
|
|
|
|
|
// l point to last two chars.
|
|
|
|
l = p + strlen(p) - 2;
|
|
|
|
|
|
|
|
// can't start with a dot
|
|
|
|
if (*p == '.') {
|
|
|
|
return 0; // invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// last 2 chars can't be a dot or a number.
|
|
|
|
if ((*l >= 'A' && *l <= 'Z') && (l[1] >= 'A' && l[1] <= 'Z')) {
|
|
|
|
while (*p) {
|
|
|
|
if ((*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '.' || *p == '-') {
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
return 0; // invalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0; // invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; // valid
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
// ipfs_isdomain_is_icann_tld returns whether the given string is a TLD (Top Level Domain),
|
2016-11-24 22:52:19 +00:00
|
|
|
// according to ICANN. Well, really according to the TLDs listed in this
|
|
|
|
// package.
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_is_icann_tld(char *s)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
2016-12-05 11:55:17 +00:00
|
|
|
return ipfs_isdomain_is_at_array (TLDs, s);
|
2016-11-24 22:52:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
// ipfs_isdomain_is_extended_tld returns whether the given string is a TLD (Top Level Domain),
|
2016-11-24 22:52:19 +00:00
|
|
|
// extended with a few other "TLDs", .bit, .onion
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_is_extended_tld (char *s)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
2016-12-05 11:55:17 +00:00
|
|
|
return ipfs_isdomain_is_at_array (ExtendedTLDs, s);
|
2016-11-24 22:52:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
// ipfs_isdomain_is_tld returns whether the given string is a TLD (according to ICANN, or
|
2016-11-24 22:52:19 +00:00
|
|
|
// in the set of ExtendedTLDs listed in this package.
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_is_tld (char *s)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
2016-12-05 11:55:17 +00:00
|
|
|
return ipfs_isdomain_is_icann_tld (s) || ipfs_isdomain_is_extended_tld(s);
|
2016-11-24 22:52:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
// ipfs_isdomain_is_domain returns whether given string is a domain.
|
2016-11-24 22:52:19 +00:00
|
|
|
// It first checks the TLD, and then uses a regular expression.
|
2016-12-05 11:55:17 +00:00
|
|
|
int ipfs_isdomain_is_domain (char *s)
|
2016-11-24 22:52:19 +00:00
|
|
|
{
|
2016-12-08 09:48:38 +00:00
|
|
|
int err;
|
|
|
|
char *str, *tld;
|
2016-11-24 22:52:19 +00:00
|
|
|
|
2016-12-08 09:48:38 +00:00
|
|
|
str = malloc(strlen(s) + 1);
|
|
|
|
if (!str) {
|
|
|
|
return ErrAllocFailed;
|
|
|
|
}
|
|
|
|
memcpy(str, s, strlen(s) + 1);
|
2016-11-24 22:52:19 +00:00
|
|
|
s = str; // work with local copy.
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
if (ipfs_isdomain_has_suffix (s, ".")) {
|
2016-11-24 22:52:19 +00:00
|
|
|
s[strlen(s) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
tld = strrchr(s, '.');
|
|
|
|
|
|
|
|
if (!tld) { // don't have a dot.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tld++; // ignore last dot
|
|
|
|
|
2016-12-05 11:55:17 +00:00
|
|
|
if (!ipfs_isdomain_is_tld (tld)) {
|
2016-11-24 22:52:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-08 09:48:38 +00:00
|
|
|
err = ipfs_isdomain_match_string(s);
|
|
|
|
free (s);
|
|
|
|
return err;
|
2016-11-24 22:52:19 +00:00
|
|
|
}
|