Posted on 2012-01-18 16:37
Seed-L 阅读(603)
评论(0) 编辑 收藏 引用
// Module Name: nbcommon.c
//
// Description:
// This file contains the function bodies for a set of
// common NetBIOS functions. See the descriptions for
// each function on what each one does. These functions
// are used by the other NetBIOS sample programs so this
// file needs to be compiled to object code and linked
// with the other executable programs.
//
// Compile:
// cl /c nbcommon.c
//
// Command Line Options:
// NONE - Compile to object code
//
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "nbcommon.h"
//
// Enumerate all LANA numbers
//
int LanaEnum(LANA_ENUM *lenum)
{
NCB ncb;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (PUCHAR)lenum;
ncb.ncb_length = sizeof(LANA_ENUM);
if (Netbios(&ncb) != NRC_GOODRET)
{
printf("ERROR: Netbios: NCBENUM: %d\n", ncb.ncb_retcode);
return ncb.ncb_retcode;
}
return NRC_GOODRET;
}
//
// Reset each LANA listed in the LANA_ENUM structure. Also, set
// the NetBIOS environment (max sessions, max name table size),
// and use the first NetBIOS name.
//
int ResetAll(LANA_ENUM *lenum, UCHAR ucMaxSession,
UCHAR ucMaxName, BOOL bFirstName)
{
NCB ncb;
int i;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBRESET;
ncb.ncb_callname[0] = ucMaxSession;
ncb.ncb_callname[2] = ucMaxName;
ncb.ncb_callname[3] = (UCHAR)bFirstName;
for(i = 0; i < lenum->length; i++)
{
ncb.ncb_lana_num = lenum->lana[i];
if (Netbios(&ncb) != NRC_GOODRET)
{
printf("ERROR: Netbios: NCBRESET[%d]: %d\n",
ncb.ncb_lana_num, ncb.ncb_retcode);
return ncb.ncb_retcode;
}
}
return NRC_GOODRET;
}
//
// Add the given name to the given LANA number. Return the name
// number for the registered name.
//
int AddName(int lana, char *name, int *num)
{
NCB ncb;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBADDNAME;
ncb.ncb_lana_num = lana;
memset(ncb.ncb_name, ' ', NCBNAMSZ);
strncpy(ncb.ncb_name, name, strlen(name));
if (Netbios(&ncb) != NRC_GOODRET)
{
printf("ERROR: Netbios: NCBADDNAME[lana=%d;name=%s]: %d\n",
lana, name, ncb.ncb_retcode);
return ncb.ncb_retcode;
}
*num = ncb.ncb_num;
return NRC_GOODRET;
}
//
// Add the given NetBIOS group name to the given LANA
// number. Return the name number for the added name.
//
int AddGroupName(int lana, char *name, int *num)
{
NCB ncb;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBADDGRNAME;
ncb.ncb_lana_num = lana;
memset(ncb.ncb_name, ' ', NCBNAMSZ);
strncpy(ncb.ncb_name, name, strlen(name));
if (Netbios(&ncb) != NRC_GOODRET)
{
printf("ERROR: Netbios: NCBADDGRNAME[lana=%d;name=%s]: %d\n",
lana, name, ncb.ncb_retcode);
return ncb.ncb_retcode;
}
*num = ncb.ncb_num;
return NRC_GOODRET;
}
//
// Delete the given NetBIOS name from the name table associated
// with the LANA number
//
int DelName(int lana, char *name)
{
NCB ncb;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBDELNAME;
ncb.ncb_lana_num = lana;
memset(ncb.ncb_name, ' ', NCBNAMSZ);
strncpy(ncb.ncb_name, name, strlen(name));
if (Netbios(&ncb) != NRC_GOODRET)
{
printf("ERROR: Netbios: NCBADDNAME[lana=%d;name=%s]: %d\n",
lana, name, ncb.ncb_retcode);
return ncb.ncb_retcode;
}
return NRC_GOODRET;
}
//
// Send len bytes from the data buffer on the given session (lsn)
// and lana number.
//
int Send(int lana, int lsn, char *data, DWORD len)
{
NCB ncb;
int retcode;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBSEND;
ncb.ncb_buffer = (PUCHAR)data;
ncb.ncb_length = len;
ncb.ncb_lana_num = lana;
ncb.ncb_lsn = lsn;
retcode = Netbios(&ncb);
return retcode;
}
//
// Receive up to len bytes into the data buffer on the given session
// (lsn) and lana number.
//
int Recv(int lana, int lsn, char *buffer, DWORD *len)
{
NCB ncb;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBRECV;
ncb.ncb_buffer = (PUCHAR)buffer;
ncb.ncb_length = *len;
ncb.ncb_lana_num = lana;
ncb.ncb_lsn = lsn;
if (Netbios(&ncb) != NRC_GOODRET)
{
*len = -1;
return ncb.ncb_retcode;
}
*len = ncb.ncb_length;
return NRC_GOODRET;
}
//
// Disconnect the given session on the given lana number
//
int Hangup(int lana, int lsn)
{
NCB ncb;
int retcode;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBHANGUP;
ncb.ncb_lsn = lsn;
ncb.ncb_lana_num = lana;
retcode = Netbios(&ncb);
return retcode;
}
//
// Cancel the given asynchronous command denoted in the NCB
// structure parameter.
//
int Cancel(PNCB pncb)
{
NCB ncb;
ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBCANCEL;
ncb.ncb_buffer = (PUCHAR)pncb;
ncb.ncb_lana_num = pncb->ncb_lana_num;
if (Netbios(&ncb) != NRC_GOODRET)
{
printf("ERROR: NetBIOS: NCBCANCEL: %d\n", ncb.ncb_retcode);
return ncb.ncb_retcode;
}
return NRC_GOODRET;
}
//
// Format the given NetBIOS name so that it is printable. Any
// unprintable characters are replaced by a period. The outname
// buffer is the returned string, which is assumed to be at least
// NCBNAMSZ + 1 characters in length.
//
int FormatNetbiosName(char *nbname, char *outname)
{
int i;
strncpy(outname, nbname, NCBNAMSZ
1// Module Name: nbcommon.h
2//
3// Purpose:
4// This header file contains the function prototypes for a set
5// of functions that implement some of the most common NetBIOS
6// functions such as enumerating LANAs, adding names, removing
7// names, etc. The functions are implemented in Nbcommon.c
8//
9
10#ifndef NBCOMMON_H_
11#define NBCOMMON_H_
12
13#include <windows.h>
14#include <nb30.h>
15
16
17
18
19int Recv(int lana, int lsn, char *buffer, DWORD *len);
20int Send(int lana, int lsn, char *data, DWORD len);
21int AddName(int lana, char *name, int *num);
22int DelName(int lana, char *name);
23int AddGroupName(int lana, char *name, int *num);
24int ResetAll(LANA_ENUM *lenum, UCHAR ucMaxSession,
25 UCHAR ucMaxName, BOOL bFirstName);
26int LanaEnum(LANA_ENUM *lenum);
27int Hangup(int lana, int lsn);
28int Cancel(PNCB pncb);
29int FormatNetbiosName(char *nbname, char *outname);
30
31
32#endif );
outname[NCBNAMSZ - 1] = '\0';
for(i = 0; i < NCBNAMSZ - 1; i++)
{
// If the character isn't printable replace it with a '.'
//
if (!((outname[i] >= 32) && (outname[i] <= 126)))
outname[i] = '.';
}
return NRC_GOODRET;
}