/*
Compile:
gcc -Wall -o DNSUPd DNSUPd.c `curl-config --libs` `curl-config --cflags`
Install:
install -m 0755 DNSUPd /usr/local/bin/
*/
char *update_url = "http://freedns.afraid.org/dynamic/update.php?ENTER_AUTH_TOKEN_HERE";
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *) userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int
randrange(int min, int max)
{
int diff = max - min;
return (int) (((double) (diff + 1) / RAND_MAX) * rand() + min);
}
int
main(void)
{
/* Our process ID and Session ID */
pid_t pid , sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
//If we got a good PID, then
// we can exit the parent process.
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog("DNSUp", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
syslog(LOG_NOTICE, "Failed to start: %d", EXIT_FAILURE);
closelog();
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
syslog(LOG_NOTICE, "Failed to start: %d", EXIT_FAILURE);
closelog();
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
syslog(LOG_NOTICE, "Starting Up! Pid is %d", pid);
//seed the RNG
srand(time(NULL));
//start our loop...
while (1) {
//sleep(300);
int rand_sleep = randrange(180, 600);
//syslog(LOG_NOTICE, "Sleeping for %d", rand_sleep);
//sleep(rand_sleep);
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1); /* will be grown as needed by
* the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
//curl_easy_setopt(curl_handle, CURLOPT_URL, "http://freedns.afraid.org/dynamic/update.php?UPDATE_URL");
curl_easy_setopt(curl_handle, CURLOPT_URL, update_url);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &chunk);
/*
* some servers don't like requests that are made without a
* user-agent field, so we provide one
*/
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
} else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
//printf("%lu bytes retrieved\n", (long) chunk.size);
//printf("%s", chunk);
syslog(LOG_NOTICE, "Update Message: %s ", chunk);
syslog(LOG_NOTICE, "Sleeping for %d seconds", rand_sleep);
//reset chunk
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
if (chunk.memory)
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
sleep(rand_sleep);
}
syslog(LOG_NOTICE, "Exiting...");
closelog();
exit(EXIT_SUCCESS);
//shouldn 't really get to here.. :-D
}
syntax highlighted by Code2HTML, v. 0.9.1