⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nbcommon.c

📁 NetBios Network programming
💻 C
字号:
/******************************************************************************\
*       This is a part of the Microsoft Source Code Samples.
*       Copyright (C) 1997 Microsoft Corporation.
*       All rights reserved.
*       This source code is only intended as a supplement to
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the
*       Microsoft sample programs.
\******************************************************************************/

//***************************************************************************
// File: nbcommon.c
//
// Decription:
//	This file contains functions common to both client and server
//	side transactions. These functions include enumerating all existing
//	lana numbers, adding and removing a name to the NetBios name table, 
//	canceling an outstanding NetBios command, reseting a lana, sending
//	a text string, reading a text string, and hanging up a NetBios 
//	session.
//
//***************************************************************************
#include "nbcommon.h"

//***************************************************************************
// Function: EnumerateLANA
//
// Description:
//      This function enumerates all lana numbers for an adaptor.
//***************************************************************************
int EnumerateLANA(LANA_ENUM *lenum)
{
    NCB ncb;
    int i, nRet;

    ZeroMemory(&ncb, sizeof(NCB));
    ZeroMemory(lenum, sizeof(LANA_ENUM));

    ncb.ncb_command = NCBENUM;
    ncb.ncb_buffer = (UCHAR *)lenum;
    ncb.ncb_length = sizeof(LANA_ENUM);

    nRet = Netbios(&ncb);

    if (nRet != NRC_GOODRET)
    {
        printf("LANA Enumeration failed with: %d\n", nRet);
        ExitProcess(0);
    }

    printf("Total Number of LANAs-> %d\n", (int)lenum->length);
    for(i=0; i < lenum->length; i++)  
    {
        printf("Found LANA: %d\n", (int)lenum->lana[i]);
    }

    return nRet;
}

//***************************************************************************
// Function: AddName
//
// Description:
//      This function adds the provided name to the NetBIOS name table
// 	on the specified LANA number.
//***************************************************************************
int AddName(int lana, char *name)
{
    NCB     ncb;
    int     dwRetCode;

    ZeroMemory(&ncb, sizeof(NCB));
    
    FillMemory(ncb.ncb_name, sizeof(ncb.ncb_name), ' ');
    CopyMemory(ncb.ncb_name, name, strlen(name));

    ncb.ncb_lana_num = lana;
    ncb.ncb_command = NCBADDNAME;

    dwRetCode = Netbios(&ncb);

    if (dwRetCode != NRC_GOODRET)
    {
        printf("Unable to add name: %d on lana %d\n", ncb.ncb_retcode, lana);
        ExitProcess(0);
    }

    printf("Added name [%s] on lana [%d]\n", name, lana);

    return dwRetCode;
}

//***************************************************************************
// Function: CancelCallback
//
// Description:
//      This function simply prints information about a NetBIOS session
//      that was cancelled. This function only gets called when a specified
//      session is cancelled.
//***************************************************************************
void CALLBACK CancelCallback(PNCB pncb)
{
    if (pncb->ncb_retcode == NRC_GOODRET)
        printf("Cancelled: lana [%d] lsn[%d]\n", pncb->ncb_lana_num, pncb->ncb_lsn);
    else
        printf("NetBIOS Error: CancelCallback: %d\n", pncb->ncb_retcode);
    return;
}

//***************************************************************************
// Function: Cancel
//
// Description:
//      This function cancels the specified NetBIOS call.  This is an
//      asynchronous call that uses a post routine to notify the user
//      which session on which lana number was cancelled.
//***************************************************************************
void Cancel(PNCB pncb)
{
    NCB     ncb;

    printf("Cancelling on LANA: %d\n", pncb->ncb_lana_num);

    if (pncb->ncb_cmd_cplt != NRC_PENDING)
        return;
    ZeroMemory(&ncb, sizeof(NCB));

    ncb.ncb_command = NCBCANCEL | ASYNCH;
    //ncb.ncb_command = NCBCANCEL;
    ncb.ncb_lana_num = pncb->ncb_lana_num;
    ncb.ncb_buffer = (PUCHAR)pncb;
    ncb.ncb_post   = CancelCallback;

    Netbios(&ncb);

    return;
}

//***************************************************************************
// Function: Reset
//
// Description:
//      This function resets the adaptor for the specified lana number.
//      It also sets the maximum name table and session table sizes to 254
//      (as opposed to the default 16).
//
//	There ia a bug in Win95/98(beta) that causes a fatal exception if
//	a reset is issued asynchronously.  
//***************************************************************************
int Reset(int lana)
{
    NCB     ncb;
    int     nRet;

    memset(&ncb, 0, sizeof(NCB));

    ncb.ncb_callname[0] = (UCHAR)254;       // Max sessions
    ncb.ncb_callname[2] = (UCHAR)254;       // Max name table size
    ncb.ncb_callname[3] = (UCHAR)FALSE;     // Usage of name number 1

    ncb.ncb_lana_num = lana;
    ncb.ncb_command = NCBRESET;
    ncb.ncb_lsn = 0;

    nRet = Netbios(&ncb);

    if (nRet != NRC_GOODRET)
        printf("reset failed  0x%x %d\n", ncb.ncb_retcode, ncb.ncb_retcode);
    else
        printf("reset succeeded\n");
    return 0;
}

//***************************************************************************
// Function: Send
//
// Description:
//      This function sends data from the lana and local session
//      numbers provided in the supplied PNCB structure passed into the
//      function.  The function sends the data synchronously.
//***************************************************************************
int Send(PNCB pncb, PUCHAR data)
{
    NCB     ncb;
    int     iReturnCode;

    ZeroMemory(&ncb, sizeof(NCB));

    ncb.ncb_buffer = data;
    ncb.ncb_length = strlen((const char *)data);
    ncb.ncb_lana_num = pncb->ncb_lana_num;
    ncb.ncb_lsn = pncb->ncb_lsn;
    ncb.ncb_command = NCBSEND;

    printf("LSN: %d\n", ncb.ncb_lsn);
 
    iReturnCode = Netbios(&ncb);

    if (iReturnCode != NRC_GOODRET)
    {
	if (iReturnCode == NRC_SCLOSED)
	    return iReturnCode;
	else
	{
            printf("NetBIOS Error: NBSend: %d\n", ncb.ncb_retcode);
            ExitProcess(0);
	}
    }

    return iReturnCode;
}

//***************************************************************************
// Function: Receive
//
// Description:
//      This function receives data from the lana and local session
//      numbers provided in the supplied PNCB structure passed into the
//      function.  The function keeps reading while there is data present.
//***************************************************************************
PUCHAR Receive(PNCB pncb)
{
    NCB     ncb;
    int     iReturnCode,
            nread;
    PUCHAR  tmpstr,
            mainstr;

    // Allocate the buffer to hold the data initially
    //
    mainstr = tmpstr = (PUCHAR) GlobalAlloc(GMEM_FIXED, MAX_MSG_SZ+1);

    ZeroMemory(&ncb, sizeof(NCB));
    //
    // Fill in the NCB structure with the appropriate values
    //
    ncb.ncb_lsn = pncb->ncb_lsn;
    ncb.ncb_lana_num = pncb->ncb_lana_num;
    ncb.ncb_buffer = tmpstr;
    ncb.ncb_length = MAX_MSG_SZ;
    ncb.ncb_command = NCBRECV;

    nread = 0;
    //
    // Loop and do receives until there is no more data to be read
    //
    do {
        if (nread > 0)
        {
            // The buffer is smaller than the current total amount of data to be read.
            //
            // Allocate a new buffer that is equal to the size of the old
            //   buffer plus our standard buffer size (maximum read size)
            tmpstr = (PUCHAR) GlobalAlloc(GMEM_FIXED, MAX_MSG_SZ * (nread + 1) + 1);

            // Copy the old buffer into the larger one
            strncpy((PCHAR)tmpstr, (const char *)mainstr, MAX_MSG_SZ * nread);
            // Free up the old buffer
            GlobalFree(ncb.ncb_buffer);
            // New buffer can now read an additional amount of data.
            // Set the pointer to the buffer to offset into the free space
            //   of the new memory.
            ncb.ncb_buffer = &tmpstr[MAX_MSG_SZ * nread] ;
            mainstr = tmpstr;

            ncb.ncb_length = MAX_MSG_SZ;
        }
        iReturnCode = Netbios(&ncb);
        //
        // Reset the buffer back to the start of the allocated space in order
        //  to NULL terminate it in the correct place
        //
        ncb.ncb_buffer = mainstr;
        ncb.ncb_buffer[(MAX_MSG_SZ * nread) + ncb.ncb_length] = '\0';

        nread++;
    } while (ncb.ncb_retcode == NRC_INCOMP);

    if (ncb.ncb_retcode != 0)
    {
	if (ncb.ncb_retcode == NRC_SCLOSED)
	    return NULL;
	else
	{
            printf("NetBIOS Receive Error: %d\n", ncb.ncb_retcode);
            ExitProcess(0);
	}
    }

    return mainstr;
}

//***************************************************************************
// Function: Hangup
//
// Description:
//      This function disconnects the specified connection (based on the
//      lana number and local session number).
//***************************************************************************
void Hangup(PNCB pncb)
{
    NCB     ncb;
    int     nRet;

    ZeroMemory(&ncb, sizeof(NCB));

    ncb.ncb_command = NCBHANGUP;
    ncb.ncb_lsn = pncb->ncb_lsn;

    nRet = Netbios(&ncb);

    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -