Skip to content

Commit

Permalink
replaceing obsolete gethostbyname for getaddrinfo and small idenatati…
Browse files Browse the repository at this point in the history
…ons changes and moving a define to the top of the source and adding gitignore file
  • Loading branch information
carvilsi committed Sep 11, 2023
1 parent 28db7b5 commit e50c52e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
canaryfy
tags
.*.swp

26 changes: 15 additions & 11 deletions canaryfy.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
#include <math.h>
#include <netdb.h>
#include <assert.h>
#include <errno.h>
#include <time.h>

#include "base32.h"

#define MAX_DNS_LEN 254
#define MAX_LABEL_LEN 62
#define PORT "80"

#define BASE32_SIZE(x) ((int)(ceil(ceil(8.0 * (x) / 5) / 8) * 8))

#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

#ifdef DEBUG
#define dprintf(...) printf (__VA_ARGS__)
Expand All @@ -43,9 +46,12 @@
char *files[256];

static void request_hostname(char *hostname) {
if (gethostbyname(hostname) == NULL) {
dprintf("%s\n", hstrerror(h_errno));
}
struct addrinfo hints, *resp;
memset(&hints, 0, sizeof(hints));
int r = getaddrinfo(hostname, PORT, &hints, &resp);
if (r != 0)
dprintf("%s\n", gai_strerror(r));

dprintf("Lookup done\n");
}

Expand Down Expand Up @@ -100,23 +106,22 @@ static void process_event(struct inotify_event *i, char *token, unsigned int tok
if (i->len > 0) {
dprintf("file read on dir = %s/%s\n", files[i->wd-1], i->name);

size = asprintf(&fn, "%s/%s", files[i->wd-1], i->name);
if (size == -1)
size = asprintf(&fn, "%s/%s", files[i->wd-1], i->name);
if (size == -1)
return;
} else {
dprintf("file read = %s\n", files[i->wd-1]);
size = asprintf(&fn, "%s", files[i->wd-1]);
if (size == -1)
size = asprintf(&fn, "%s", files[i->wd-1]);
if (size == -1)
return;
}

dprintf("Free space is %d\n", (unsigned int) free_space);

dprintf("filename is %s\n", fn);

build_base32_hostname(hostname, free_space, fn);

//assert(sizeof(hostname) > size );

dprintf("base32 filename: %s (%d bytes)\n", hostname, (unsigned int) strlen(hostname));

snprintf(hostname+strlen(hostname), sizeof(hostname)-(strlen(hostname)+1),".L%02.f.%s", ((float)rand())/RAND_MAX*99, token);
Expand All @@ -127,7 +132,6 @@ static void process_event(struct inotify_event *i, char *token, unsigned int tok
free(fn);
}

#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

int main(int argc, char *argv[])
{
Expand Down

0 comments on commit e50c52e

Please sign in to comment.