vendredi 8 juillet 2016

Parsing memory mapped file C

I'm currently building a functional DNS Server and I need some help to finish it. Currently I turn the server on and with the dig command I'm able to send my requests. The problem is how to answer them correctly. Before i have mapped in memory a file (using mmap) that contains domains names and their IP's, so if I receive any request regarding a local address I'm able to answer it by just parsing the mmaped file with a thread that gets the request from a linked list and extracting the needed information. I just can't seem to find a way to do it properly.

I'll leave my mmap code below:

void mmap_localdns(){
    int ldns;
    char *data;
    struct stat size;
    ldns=open("localdns.txt", O_RDONLY);
    stat("localdns.txt", &size);
    data = mmap((caddr_t)0, size.st_size, PROT_READ, MAP_SHARED, ldns, 0);
    close(ldns);
    printf("%s", data);

    char *pch;
    pch = strtok(&data, " ");

    while(pch != NULL){
        printf("%sn", pch);
        pch = strtok(NULL, " ");
    }
    //munmap(data, size.st_size);
}

Tried using strtok() but it keeps giving me a Segmentation Fault. Perhaps it's not the best solution :/

The file I'm talking about follow these rules:

mywebserver.so.local 192.168.1.20
fileserver.so.local 192.168.1.10
ww5.so.local 192.168.1.5

Thanks :)

Aucun commentaire:

Enregistrer un commentaire