Skip to content

Commit

Permalink
str: Add function for converting string into DNS name
Browse files Browse the repository at this point in the history
Add str2fqdn for converting hostname string into DNS name notation:

    www.xxxx.yy.com -> 3www4xxxx2yy3com0

Returned string must be freed after use by the caller.

Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
vkochan authored and tklauser committed Jun 2, 2017
1 parent 906cb24 commit a2524f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions str.c
Expand Up @@ -160,3 +160,40 @@ int str2mac(const char *str, uint8_t *mac, size_t len)

return 0;
}

char *str2fqdn(const char *str)
{
size_t slen = strlen(str);
size_t flen = 0;
char *fqdn;
char *tmp;
char *dup;
int i = 0;
int c = 0;

dup = xstrdup(str);
tmp = dup;

fqdn = xzmalloc(slen + 2);

while (tmp <= dup + slen && c++ <= slen) {
if (tmp[i] == '.' || tmp[i] == '\0') {
size_t dlen;

tmp[i] = '\0';
dlen = strlen(tmp);
fqdn[flen] = dlen;
memcpy(&fqdn[flen + 1], tmp, dlen);
flen += dlen + 1;
tmp += dlen + 1;
i = 0;

continue;
}

i++;
}

xfree(dup);
return fqdn;
}
1 change: 1 addition & 0 deletions str.h
Expand Up @@ -14,5 +14,6 @@ extern char *argv2str(int startind, int argc, char **argv);
extern char **argv_insert(char **argv, size_t *count, const char *str);
extern void argv_free(char **argv);
extern int str2mac(const char *str, uint8_t *mac, size_t len);
extern char *str2fqdn(const char *str);

#endif /* STR_H */

0 comments on commit a2524f2

Please sign in to comment.