nbtstat Linux版源码, 通过IP获取主机名

时间:2024-09-03 22:33:20
nbtstat Linux版, 通过IP获取主机名/* NETBIOS name lookup tool - by eSDee of Netric (www.netric.org)
 * yeh.. i was really bored :-)
 *
 * [esdee@pant0ffel] gcc -o nbtstat nbtstat.c && ./nbtstat 10.0.0.4
 * Request sent, waiting for reply... [ctrl-c to abort]
 * Name                Type
 * ----------------------------------
 * HOEPELKO-ESPU0B <00> UNIQUE Workstation Service
 * HOEPELKO-ESPU0B <20> UNIQUE File Server Service
 * WORKGROUP       <00> GROUP  Domain Name
 * WORKGROUP       <1e> GROUP  Browser Service Elections
 * HOEPELKO-ESPU0B <03> UNIQUE Messenger Service
 * ADMINISTRATOR   <03> UNIQUE Messenger Service
 * WORKGROUP       <1d> UNIQUE Master Browser
 * ..__MSBROWSE__. <01> GROUP  Master Browser
 * MAC-addres = 00-02-a5-e1-fd-b7
 * [/home/esdee/nbtstat] */

#include  <stdio.h>
#include  <string.h>
#include  <sys/types.h>
#include  <sys/socket.h>
#include  <netinet/in.h>
#include  <netdb.h>
#include  <unistd.h>

char nbtname[]= /* netbios name packet */
{
    0x80,0xf0,0x00,0x10,0x00,0x01,0x00,0x00,
    0x00,0x00,0x00,0x00,0x20,0x43,0x4b,0x41,
    0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
    0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
    0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
    0x41,0x41,0x41,0x41,0x41,0x00,0x00,0x21,
    0x00,0x01
};

int display();

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

    unsigned ];
    unsigned char *ptr;

    struct sockaddr_in server;
    struct hostent *hp;

    int s;
    int total;

    unsigned int nb_num;
    unsigned int nb_type;

    ;

    ) {
        fprintf(stderr, ]);
        ;
    }

    ])) == NULL)  {
        fprintf(stderr,]);
        ;
    }

    )) < )  { /* 17 = UDP */
        perror("socket");
        ;
    }

    memset(recv,0x0, sizeof(recv));

    bzero((char *) &server, sizeof(server));
    bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);

    server.sin_family = hp->h_addrtype;
    server.sin_port = htons(); /* netbios-ns */

    , ()  {
        perror("sendto");
        ;
    } else {
        fprintf(stdout, "Request sent, waiting for reply... [ctrl-c to abort]\n");

        read(s, recv, );

        fprintf(stdout,"Name                Type\n"
                       "----------------------------------\n");
        ptr=recv+;
         total=*(ptr - ); /* max names */

        while(ptr < recv + sizeof(recv)) {
            memset(temp,0x0, sizeof(temp));
            strncpy(temp, ptr, );         /* copies the name into temp */

            ptr+=;
            nb_num  = *ptr;
            nb_type = *(ptr + );
            ptr+=;

            if (i==total) {          /* max names reached */
                ptr-=;         /* sets the pointer to the mac_addres field */
                fprintf(stdout,"\nMAC-addres = %02x-%02x-%02x-%02x-%02x-%02x\n\n",
                           *(ptr + ), *(ptr + ), *(ptr + ),
                           *(ptr + ), *(ptr + ), *(ptr + ));
                break;
            }

            display(temp,nb_num,nb_type);
            i++;
        }
    }
    close(s);
    ;
}

int display(char *name, unsigned int number, unsigned int type)
{
    ];
    int i;

    memset(description, 0x0, sizeof(description));

    /* list taken from http://support.microsoft.com/default.aspx?scid=KB;EN-US;q163409& */
    /* 0x04 - UNIQUE */
    /* 0x80 - GROUP */

    switch(number) {
        case 0x00:
            if (type <= 0x80) {
                    strncpy(description, );
            } else {
                    strncpy(description, );
            }
            break;
        case 0x01:
            if (type <= 0x80) {
                    strncpy(description, );
            } else {
                    strncpy(description, );
            }
            break;
        case 0x03:
            strncpy(description, );
            break;
        case 0x06:
            strncpy(description, );
            break;
        case 0x1b:
            strncpy(description, );
            break;
        case 0x1c:
            strncpy(description, );
            break;
        case 0x1d:
            strncpy(description, );
            break;
        case 0x1e:
            );
            break;
        case 0x1F:
            strncpy(description, );
            break;
        case 0x20:
            strncpy(description, );
            break;
        case 0x21:
            strncpy(description, );
            break;
        case 0x22:
            strncpy(description, );
            break;
        case 0x23:
            strncpy(description, );
            break;
        case 0x24:
            strncpy(description, );
            break;
        case 0x30:
            strncpy(description, );
            break;
        case 0x31:
            strncpy(description, );
            break;
        case 0x42:
            strncpy(description, );
            break;
        case 0x43:
            strncpy(description, );
            break;
        case 0x44:
            strncpy(description, );
            break;
        case 0x45:
            strncpy(description, );
            break;
        case 0x46:
            strncpy(description, );
            break;
        case 0x4C:
            strncpy(description, );
            break;
        case 0x52:
            strncpy(description, );
            break;
        case 0x6a:
            strncpy(description, );
            break;
        case 0x87:
            strncpy(description, );
            break;
        case 0xbe:
            strncpy(description, );
            break;
        case 0xbf:
            strncpy(description, );
            break;
        default:
            strncpy(description, );
            break;
    }

    ; i < strlen(name); i++) /* replaces weird chars with dots */
         || name[i] > ) name[i] = '.';

    if (name) fprintf(stdout, "%s <%02x> %s\n", name, number, description);
    ;

}