dns_server.c

dns_server.c · 5.3 KB

From the archive dns_server.zip

Download the archive   « Back to the archive contents

/* DNS server V1.0
   Anthony McDonald aka Lokie
   lokie.spods@ntlworld.com

   I hate writing this as a seperate process, but then again
   its simpler to write a multi-threaded DNS server than it
   is to fix PGPlus to run DNS via threads.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/* change this to suit, 10 should be more than enough for most talkers */
#define MAX_PCOUNT 10
#define MAX_INET_ADDR 40
#define BUF_SIZE 2048

struct dns_struct
{
    int job;
    struct dns_struct *next;
    char num_addr[MAX_INET_ADDR];
    char inet_addr[MAX_INET_ADDR];
};

pthread_mutex_t dns_list_mutex, dns_count_mutex;
int pcount = 0;
struct dns_struct *top_dns = NULL, *hold_dns = NULL;
int int_sock[2];

/* I could move this to main, but I like seperating the init of the mutex'es just incase I ever add more */

void init_pthread_dns(void)
{
    pthread_mutex_init(&dns_list_mutex, NULL);
    pthread_mutex_init(&dns_count_mutex, NULL);
}

/* a simple fifo stack system just incase we run out of free threads */

void push_dns(struct dns_struct *push)
{
    struct dns_struct *walk = hold_dns;

    if (!hold_dns)
    {
        hold_dns = push;
        return;
    }
    while(walk->next)
        walk = walk->next;
    walk->next = push;
}

struct dns_struct *pop_dns(void)
{
   struct dns_struct *walk = hold_dns;

   hold_dns = hold_dns->next;
   return walk;
}

/* DNS lookup done via a Pthread */

void *do_pthread_dns(void *silly)
{
    struct dns_struct *wrk = silly;
    struct in_addr prep;
    struct hostent hostbuff, *hp = NULL;
    int hperr, size = 1024;
    char nbuff[1024];

    inet_aton(wrk->num_addr, &prep);
    gethostbyaddr_r((char *) &prep.s_addr, sizeof(prep.s_addr), AF_INET, &hostbuff, nbuff, size, &hp, &hperr);
    if (hp)
        strncpy(wrk->inet_addr, hp->h_name, MAX_INET_ADDR - 1);
    else
        strcpy(wrk->inet_addr, wrk->num_addr);
    pthread_mutex_lock(&dns_list_mutex);
    wrk->next = top_dns;
    top_dns = wrk;
    pthread_mutex_unlock(&dns_list_mutex);
    pthread_mutex_lock(&dns_count_mutex);
    pcount--;
    pthread_mutex_unlock(&dns_count_mutex);
    write(int_sock[1], "\n", 1);
    return NULL;
}

/* check if we have DNS to return, return it and do all the other cleanup */

void check_dns(void)
{
    struct dns_struct *temp;
    static char out_buffer[128]; /* Yes its really that small :-)'s */

    pthread_mutex_lock(&dns_list_mutex);
    if (!top_dns)
    {
        pthread_mutex_unlock(&dns_list_mutex);
        return;
    }
    temp = top_dns;
    top_dns = top_dns->next;
    pthread_mutex_unlock(&dns_list_mutex);
    sprintf(out_buffer, "%d:%s\n", temp->job, temp->inet_addr);
    write(1, out_buffer, strlen(out_buffer));
    free(temp);
}

/* read an entry, and pop onto the waiting list */

void read_dns(void)
{
    static char inp_buffer[BUF_SIZE];
    static int inp_pos = 0;
    struct dns_struct *wrk;
    int cooked = 0;
    char c;

    ioctl(0, FIONREAD, &cooked);
    while(cooked)
    {
        read(0, &c, 1);
        if (c == '\n')
        {
            inp_buffer[inp_pos] = '\0';
            wrk = malloc(sizeof(struct dns_struct));
            if (wrk)
            {
                memset(wrk, 0, sizeof(struct dns_struct));
                sscanf(inp_buffer, "%d:%39s", &wrk->job, wrk->num_addr);
                push_dns(wrk);
            }
            inp_pos = 0;
            break;
        }
        else
        {
            if (inp_pos != BUF_SIZE - 1)
                inp_buffer[inp_pos++] = c;
        }
        cooked--;
    }    
}

int main(int argc, char *argv[])
{
    fd_set fset;
    pthread_t mythread;
    int cooked = 0;
    char c;
    struct timeval tv;

    init_pthread_dns();
    pipe(int_sock);
    while (1)
    {
        if (1 == getppid())
            exit(0);
        FD_ZERO(&fset);
        FD_SET(0, &fset);
        FD_SET(int_sock[0], &fset);
        tv.tv_sec = 60;
        tv.tv_usec = 0;
        if (select(FD_SETSIZE, &fset, 0, 0, &tv))
        {
            if (FD_ISSET(0, &fset))
                read_dns();
            if (FD_ISSET(int_sock[0], &fset))
            {
                ioctl(int_sock[0], FIONREAD, &cooked);
                while(cooked)
                {
                    read(int_sock[0], &c, 1);
                    check_dns();
                    cooked--;
                }
            }
            while (hold_dns)
            {
                pthread_mutex_lock(&dns_count_mutex);
                if (pcount < MAX_PCOUNT)
                {
                    pthread_create(&mythread, NULL, do_pthread_dns, (void *) pop_dns());
                    pthread_detach(mythread);
                    pcount++;
                    pthread_mutex_unlock(&dns_count_mutex);
                }
                else
                {
                    pthread_mutex_unlock(&dns_count_mutex);
                    break;
                }
            }
        }
    }
}

Site Stats

Snippets: 1791
Codebases: 571