Senin, 26 September 2011

Bagaimana cara Nslookup bekerja

Code ini dalam bahasa C yang mengimplementasikan cara kerja nslookup,,

// simple nslookup program
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/io.h>

int main(int argc, char *argv[])
{
    struct sockaddr_in ipv4_config;
    struct hostent *host_config;
    struct in_addr *inet_config;
    char *ipv4_addr;
    typedef int uint_32;
 
    printf("[+] syn_nslookup program\n");
    printf("[+] coding : Vanzoel\n\n");
 
    if (argc < 2)
    {
        fprintf(stderr, "[+] Usage : %s <supported_ipv4_address>\n", argv[0]);
        fprintf(stderr, "[+] Ex : %s ssl.facebook.com\n", argv[0]);
        exit(1);
    }
 
    // dynamic allocating memory
    host_config = (struct hostent *) malloc(sizeof(*host_config));
    inet_config = (struct in_addr *) malloc(sizeof(*inet_config));
 
    ipv4_addr = argv[1];
    host_config = gethostbyname(ipv4_addr);
 
    // based on BSD netinet/in.h, arpa/inet.h, netdb.h, sys/socket.h
    // host address structure
    /* struct hostent {
            (official name of the host)
            char        *h_name; 
            (alternative names of the original host)
            char        **h_aliases;
            (host address type, e.g : AF_INET, AF_INET6)
            int        h_addrtype;
            (length of address, in bytes, of each address)
            int        h_length;
            (vector of addresses for the host)
            char        **h_addr_list;
            (this is a synonym for h_addr_list[0])
            char         *h_addr;
        };        */
     
     
    if (host_config == NULL)
    {
        fprintf(stderr, "[+] Unable to resolve address %s\n", argv[1]);
    }
    else
    {
        if (host_config)
        {
            printf("[+] Name : %s\n", host_config->h_name);
            while (*host_config->h_aliases)
            {
                printf("[+] Alias : %s\n", *host_config->h_aliases++);
            }
         
            while (*host_config->h_addr_list)
            {
                bcopy(*host_config->h_addr_list++, (char *)inet_config, sizeof(inet_config));
                printf("[+] Address : %s\n", inet_ntoa(*inet_config));
            }
        }
    }
 
    return 0;
}

works in Debian Squeeze 6.02
compile : gcc -o syn_nslookup syn_nslookup.c

example :
[vanzoel@localhost:/home/zul/code-C ~] # ./sync facebook.com
[+] syn_nslookup program
[+] coding : Vanzoel

[+] Name : facebook.com
[+] Address : 69.63.181.12
[+] Address : 69.63.189.11
[+] Address : 69.63.189.16


thanks to Paulus Gandung Prakosa_ (0x1337day)

Tidak ada komentar:

Posting Komentar