Posted on 2012-01-18 16:37 
Seed-L 阅读(635) 
评论(0)  编辑 收藏 引用  
			 
			
		 
		 // Module Name: nbcommon.c
// Module Name: nbcommon.c
 //
//
 // Description:
// Description:
 //    This file contains the function bodies for a set of
//    This file contains the function bodies for a set of 
 //    common NetBIOS functions.  See the descriptions for
//    common NetBIOS functions.  See the descriptions for
 //    each function on what each one does. These functions
//    each function on what each one does. These functions
 //    are used by the other NetBIOS sample programs so this
//    are used by the other NetBIOS sample programs so this
 //    file needs to be compiled to object code and linked
//    file needs to be compiled to object code and linked 
 //    with the other executable programs.
//    with the other executable programs.
 //
//
 // Compile:
// Compile:
 //    cl /c nbcommon.c
//    cl /c nbcommon.c
 //
//
 // Command Line Options:
// Command Line Options:
 //    NONE - Compile to object code
//    NONE - Compile to object code
 //
//
 #include <windows.h>
#include <windows.h>
 #include <stdio.h>
#include <stdio.h>
 #include <stdlib.h>
#include <stdlib.h>

 #include "nbcommon.h"
#include "nbcommon.h"

 //
//
 // Enumerate all LANA numbers
// Enumerate all LANA numbers
 //
//
 int LanaEnum(LANA_ENUM *lenum)
int LanaEnum(LANA_ENUM *lenum)


 {
{
 NCB                ncb;
    NCB                ncb;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBENUM;
    ncb.ncb_command = NCBENUM;
 ncb.ncb_buffer = (PUCHAR)lenum;
    ncb.ncb_buffer = (PUCHAR)lenum;
 ncb.ncb_length = sizeof(LANA_ENUM);
    ncb.ncb_length = sizeof(LANA_ENUM);

 if (Netbios(&ncb) != NRC_GOODRET)
    if (Netbios(&ncb) != NRC_GOODRET)

 
     {
{
 printf("ERROR: Netbios: NCBENUM: %d\n", ncb.ncb_retcode);
        printf("ERROR: Netbios: NCBENUM: %d\n", ncb.ncb_retcode);
 return ncb.ncb_retcode;
        return ncb.ncb_retcode;
 }
    }
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}

 //
//
 // Reset each LANA listed in the LANA_ENUM structure. Also, set
// Reset each LANA listed in the LANA_ENUM structure. Also, set
 // the NetBIOS environment (max sessions, max name table size),
// the NetBIOS environment (max sessions, max name table size),
 // and use the first NetBIOS name.
// and use the first NetBIOS name.
 //
//
 int ResetAll(LANA_ENUM *lenum, UCHAR ucMaxSession,
int ResetAll(LANA_ENUM *lenum, UCHAR ucMaxSession, 
 UCHAR ucMaxName, BOOL bFirstName)
             UCHAR ucMaxName, BOOL bFirstName)


 {
{
 NCB                ncb;
    NCB                ncb;
 int                i;
    int                i;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBRESET;
    ncb.ncb_command = NCBRESET;
 ncb.ncb_callname[0] = ucMaxSession;
    ncb.ncb_callname[0] = ucMaxSession;
 ncb.ncb_callname[2] = ucMaxName;
    ncb.ncb_callname[2] = ucMaxName;
 ncb.ncb_callname[3] = (UCHAR)bFirstName;
    ncb.ncb_callname[3] = (UCHAR)bFirstName;

 for(i = 0; i < lenum->length; i++)
    for(i = 0; i < lenum->length; i++)

 
     {
{
 ncb.ncb_lana_num = lenum->lana[i];
        ncb.ncb_lana_num = lenum->lana[i];
 if (Netbios(&ncb) != NRC_GOODRET)
        if (Netbios(&ncb) != NRC_GOODRET)

 
         {
{
 printf("ERROR: Netbios: NCBRESET[%d]: %d\n",
            printf("ERROR: Netbios: NCBRESET[%d]: %d\n",
 ncb.ncb_lana_num, ncb.ncb_retcode);
                ncb.ncb_lana_num, ncb.ncb_retcode);
 return ncb.ncb_retcode;
            return ncb.ncb_retcode;
 }
        }
 }
    }
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}

 //
//
 // Add the given name to the given LANA number. Return the name
// Add the given name to the given LANA number. Return the name
 // number for the registered name.
// number for the registered name.
 //
//
 int AddName(int lana, char *name, int *num)
int AddName(int lana, char *name, int *num)


 {
{
 NCB                ncb;
    NCB                ncb;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBADDNAME;
    ncb.ncb_command = NCBADDNAME;
 ncb.ncb_lana_num = lana;
    ncb.ncb_lana_num = lana;
 memset(ncb.ncb_name, ' ', NCBNAMSZ);
    memset(ncb.ncb_name, ' ', NCBNAMSZ);
 strncpy(ncb.ncb_name, name, strlen(name));
    strncpy(ncb.ncb_name, name, strlen(name));

 if (Netbios(&ncb) != NRC_GOODRET)
    if (Netbios(&ncb) != NRC_GOODRET)

 
     {
{
 printf("ERROR: Netbios: NCBADDNAME[lana=%d;name=%s]: %d\n",
        printf("ERROR: Netbios: NCBADDNAME[lana=%d;name=%s]: %d\n",
 lana, name, ncb.ncb_retcode);
            lana, name, ncb.ncb_retcode);
 return ncb.ncb_retcode;
        return ncb.ncb_retcode;
 }
    }
 *num = ncb.ncb_num;
    *num = ncb.ncb_num;
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}

 //
//
 // Add the given NetBIOS group name to the given LANA
// Add the given NetBIOS group name to the given LANA
 // number. Return the name number for the added name.
// number. Return the name number for the added name.
 //
//
 int AddGroupName(int lana, char *name, int *num)
int AddGroupName(int lana, char *name, int *num)


 {
{
 NCB                ncb;
    NCB                ncb;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBADDGRNAME;
    ncb.ncb_command = NCBADDGRNAME;
 ncb.ncb_lana_num = lana;
    ncb.ncb_lana_num = lana;
 memset(ncb.ncb_name, ' ', NCBNAMSZ);
    memset(ncb.ncb_name, ' ', NCBNAMSZ);
 strncpy(ncb.ncb_name, name, strlen(name));
    strncpy(ncb.ncb_name, name, strlen(name));

 if (Netbios(&ncb) != NRC_GOODRET)
    if (Netbios(&ncb) != NRC_GOODRET)

 
     {
{
 printf("ERROR: Netbios: NCBADDGRNAME[lana=%d;name=%s]: %d\n",
        printf("ERROR: Netbios: NCBADDGRNAME[lana=%d;name=%s]: %d\n",
 lana, name, ncb.ncb_retcode);
            lana, name, ncb.ncb_retcode);
 return ncb.ncb_retcode;
        return ncb.ncb_retcode;
 }
    }
 *num = ncb.ncb_num;
    *num = ncb.ncb_num;
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}

 //
//
 // Delete the given NetBIOS name from the name table associated
// Delete the given NetBIOS name from the name table associated
 // with the LANA number
// with the LANA number
 //
//
 int DelName(int lana, char *name)
int DelName(int lana, char *name)


 {
{
 NCB                ncb;
    NCB                ncb;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBDELNAME;
    ncb.ncb_command = NCBDELNAME;
 ncb.ncb_lana_num = lana;
    ncb.ncb_lana_num = lana;
 memset(ncb.ncb_name, ' ', NCBNAMSZ);
    memset(ncb.ncb_name, ' ', NCBNAMSZ);
 strncpy(ncb.ncb_name, name, strlen(name));
    strncpy(ncb.ncb_name, name, strlen(name));

 if (Netbios(&ncb) != NRC_GOODRET)
    if (Netbios(&ncb) != NRC_GOODRET)

 
     {
{
 printf("ERROR: Netbios: NCBADDNAME[lana=%d;name=%s]: %d\n",
        printf("ERROR: Netbios: NCBADDNAME[lana=%d;name=%s]: %d\n",
 lana, name, ncb.ncb_retcode);
            lana, name, ncb.ncb_retcode);
 return ncb.ncb_retcode;
        return ncb.ncb_retcode;
 }
    }
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}

 //
//
 // Send len bytes from the data buffer on the given session (lsn)
// Send len bytes from the data buffer on the given session (lsn)
 // and lana number.
// and lana number.
 //
//
 int Send(int lana, int lsn, char *data, DWORD len)
int Send(int lana, int lsn, char *data, DWORD len)


 {
{
 NCB                ncb;
    NCB                ncb;
 int                retcode;
    int                retcode;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBSEND;
    ncb.ncb_command = NCBSEND;
 ncb.ncb_buffer = (PUCHAR)data;
    ncb.ncb_buffer = (PUCHAR)data;
 ncb.ncb_length = len;
    ncb.ncb_length = len;
 ncb.ncb_lana_num = lana;
    ncb.ncb_lana_num = lana;
 ncb.ncb_lsn = lsn;
    ncb.ncb_lsn = lsn;
 
  
 retcode = Netbios(&ncb);
    retcode = Netbios(&ncb);

 return retcode;
    return retcode;
 }
}

 //
//
 // Receive up to len bytes into the data buffer on the given session
// Receive up to len bytes into the data buffer on the given session 
 // (lsn) and lana number.
// (lsn) and lana number.
 //
//
 int Recv(int lana, int lsn, char *buffer, DWORD *len)
int Recv(int lana, int lsn, char *buffer, DWORD *len)


 {
{
 NCB                ncb;
    NCB                ncb;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBRECV;
    ncb.ncb_command = NCBRECV;
 ncb.ncb_buffer = (PUCHAR)buffer;
    ncb.ncb_buffer = (PUCHAR)buffer;
 ncb.ncb_length = *len;
    ncb.ncb_length = *len;
 ncb.ncb_lana_num = lana;
    ncb.ncb_lana_num = lana;
 ncb.ncb_lsn = lsn;
    ncb.ncb_lsn = lsn;

 if (Netbios(&ncb) != NRC_GOODRET)
    if (Netbios(&ncb) != NRC_GOODRET)

 
     {
{
 *len = -1;
        *len = -1;
 return ncb.ncb_retcode;
        return ncb.ncb_retcode;
 }
    }
 *len = ncb.ncb_length;
    *len = ncb.ncb_length;

 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}
 //
//
 // Disconnect the given session on the given lana number
// Disconnect the given session on the given lana number
 //
//
 int Hangup(int lana, int lsn)
int Hangup(int lana, int lsn)


 {
{
 NCB                ncb;
    NCB                ncb;
 int                retcode;
    int                retcode;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBHANGUP;
    ncb.ncb_command = NCBHANGUP;
 ncb.ncb_lsn = lsn;
    ncb.ncb_lsn = lsn;
 ncb.ncb_lana_num = lana;
    ncb.ncb_lana_num = lana;

 retcode = Netbios(&ncb);
    retcode = Netbios(&ncb);

 return retcode;
    return retcode;
 }
}

 //
//
 // Cancel the given asynchronous command denoted in the NCB
// Cancel the given asynchronous command denoted in the NCB
 // structure parameter.
// structure parameter.
 //
//
 int Cancel(PNCB pncb)
int Cancel(PNCB pncb)


 {
{
 NCB                ncb;
    NCB                ncb;

 ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(&ncb, sizeof(NCB));
 ncb.ncb_command = NCBCANCEL;
    ncb.ncb_command = NCBCANCEL;
 ncb.ncb_buffer = (PUCHAR)pncb;
    ncb.ncb_buffer = (PUCHAR)pncb;
 ncb.ncb_lana_num = pncb->ncb_lana_num;
    ncb.ncb_lana_num = pncb->ncb_lana_num;

 if (Netbios(&ncb) != NRC_GOODRET)
    if (Netbios(&ncb) != NRC_GOODRET)

 
     {
{
 printf("ERROR: NetBIOS: NCBCANCEL: %d\n", ncb.ncb_retcode);
        printf("ERROR: NetBIOS: NCBCANCEL: %d\n", ncb.ncb_retcode);
 return ncb.ncb_retcode;
        return ncb.ncb_retcode;
 }
    }
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}

 //
//
 // Format the given NetBIOS name so that it is printable. Any
// Format the given NetBIOS name so that it is printable. Any
 // unprintable characters are replaced by a period. The outname
// unprintable characters are replaced by a period. The outname
 // buffer is the returned string, which is assumed to be at least
// buffer is the returned string, which is assumed to be at least 
 // NCBNAMSZ + 1 characters in length.
// NCBNAMSZ + 1 characters in length.
 //
//
 int FormatNetbiosName(char *nbname, char *outname)
int FormatNetbiosName(char *nbname, char *outname)


 {
{
 int        i;
    int        i;

 strncpy(outname, nbname, NCBNAMSZ
    strncpy(outname, nbname, NCBNAMSZ
 1 // Module Name: nbcommon.h
// Module Name: nbcommon.h
 2 //
//
 3 // Purpose:
// Purpose:
 4 //    This header file contains the function prototypes for a set
//    This header file contains the function prototypes for a set
 5 //    of functions that implement some of the most common NetBIOS
//    of functions that implement some of the most common NetBIOS
 6 //    functions such as enumerating LANAs, adding names, removing
//    functions such as enumerating LANAs, adding names, removing
 7 //    names, etc.  The functions are implemented in Nbcommon.c
//    names, etc.  The functions are implemented in Nbcommon.c
 8 //
// 
 9
10 #ifndef NBCOMMON_H_
#ifndef NBCOMMON_H_
11 #define NBCOMMON_H_
#define NBCOMMON_H_
12 
   
13 #include <windows.h>
#include <windows.h>
14 #include <nb30.h>
#include <nb30.h>
15
16
17
18
19 int Recv(int lana, int lsn, char *buffer, DWORD *len);
int Recv(int lana, int lsn, char *buffer, DWORD *len);
20 int Send(int lana, int lsn, char *data, DWORD len);
int Send(int lana, int lsn, char *data, DWORD len);
21 int AddName(int lana, char *name, int *num);
int AddName(int lana, char *name, int *num);
22 int DelName(int lana, char *name);
int DelName(int lana, char *name);
23 int AddGroupName(int lana, char *name, int *num);
int AddGroupName(int lana, char *name, int *num);
24 int ResetAll(LANA_ENUM *lenum, UCHAR ucMaxSession,
int ResetAll(LANA_ENUM *lenum, UCHAR ucMaxSession, 
25 UCHAR ucMaxName, BOOL bFirstName);
         UCHAR ucMaxName, BOOL bFirstName);
26 int LanaEnum(LANA_ENUM *lenum);
int LanaEnum(LANA_ENUM *lenum);
27 int Hangup(int lana, int lsn);
int Hangup(int lana, int lsn);
28 int Cancel(PNCB pncb);
int Cancel(PNCB pncb);
29 int FormatNetbiosName(char *nbname, char *outname);
int FormatNetbiosName(char *nbname, char *outname);
30
31
32 #endif
#endif );
 outname[NCBNAMSZ - 1] = '\0';
    outname[NCBNAMSZ - 1] = '\0';
 for(i = 0; i < NCBNAMSZ - 1; i++)
    for(i = 0; i < NCBNAMSZ - 1; i++)

 
     {
{
 // If the character isn't printable replace it with a '.'
        // If the character isn't printable replace it with a '.'
 //
        //
 if (!((outname[i] >= 32) && (outname[i] <= 126)))
        if (!((outname[i] >= 32) && (outname[i] <= 126)))
 outname[i] = '.';
            outname[i] = '.';
 }
    }
 return NRC_GOODRET;
    return NRC_GOODRET;
 }
}