//
// Function: PrintNameInfo
//
// Description:
//    Prints out a NetBIOS name and its related information
//
void PrintNameInfo(NAME_BUFFER *names, int namecount)
{
    char        namebuff[NCBNAMSZ + 1];
    int                i;

    if (namecount == 0)
    { 
        printf("No names in local name table\n\n\n");
        return;
    }
    printf("\nName             Type  Number  Flags\n");
    for(i=0; i < namecount ;i++)
    {
        FormatNetbiosName(names[i].name, namebuff);
        printf("%s <%02x>     %-2d   ", namebuff, 
            names[i].name[NCBNAMSZ-1], names[i].name_num);
 
        if (names[i].name_flags & REGISTERING)
            printf("Registering  ");
        else if (names[i].name_flags & REGISTERED)
            printf("Registered ");
        else if (names[i].name_flags & DEREGISTERED)
            printf("Deregistered ");
        else if (names[i].name_flags & DUPLICATE)
            printf("Duplicate ");
        else if (names[i].name_flags & DUPLICATE_DEREG)
            printf("Duplicate-Deregistered ");
        if (names[i].name_flags & GROUP_NAME)
            printf("Group-Name ");
        printf("\n");
    }
    printf("\n\n");
}
Esempio n. 2
0
//
// Function: ClientThread
//
// Description:
//    This thread takes the NCB structure of a connected session
//    and waits for incoming data which it then sends back to the
//    client until the session is closed.
//
DWORD WINAPI ClientThread(PVOID lpParam)
{
    PNCB        pncb = (PNCB)lpParam;
    NCB         ncb;
    char        szRecvBuff[MAX_BUFFER],
                szClientName[NCBNAMSZ + 1];
    DWORD       dwBufferLen = MAX_BUFFER,
                dwRetVal = NRC_GOODRET;

    // Send and receive messages until the session is closed
    // 
    FormatNetbiosName(pncb->ncb_callname, szClientName);
    while (1)
    {
        dwBufferLen = MAX_BUFFER;
        dwRetVal = Recv(pncb->ncb_lana_num, pncb->ncb_lsn,
			szRecvBuff, &dwBufferLen);
        if (dwRetVal != NRC_GOODRET)
            break;

        szRecvBuff[dwBufferLen] = 0;
        printf("READ [LANA=%d]: '%s'\n", pncb->ncb_lana_num, 
            szRecvBuff);

        dwRetVal = Send(pncb->ncb_lana_num, pncb->ncb_lsn,
			szRecvBuff, dwBufferLen);
        if (dwRetVal != NRC_GOODRET)
            break;
    }
    printf("Client '%s' on LANA %d disconnected\n", szClientName,
        pncb->ncb_lana_num);
    //
    // If the error returned from a read or write is NRC_SCLOSED then
    // all is well; otherwise some other error occured so hangup the
    // connection from this side.
    //
    if (dwRetVal != NRC_SCLOSED)
    {
        ZeroMemory(&ncb, sizeof(NCB));
        ncb.ncb_command = NCBHANGUP;
        ncb.ncb_lsn = pncb->ncb_lsn;
        ncb.ncb_lana_num = pncb->ncb_lana_num;

        if (Netbios(&ncb) != NRC_GOODRET)
        {
            printf("ERROR: Netbios: NCBHANGUP: %d\n",
				ncb.ncb_retcode);
            GlobalFree(pncb);
            dwRetVal = ncb.ncb_retcode;
        }
    }
    // The NCB structure passed in is dynamically allocated it so 
    // delete it before we go.
    //
    GlobalFree(pncb);
    return NRC_GOODRET; 
}
//
// Function: ClientThread
//
// Description:
//    The client thread blocks for data sent from clients and 
//    simply sends it back to them. This is a continuous loop
//    until the sessions is closed or an error occurs.  If
//    the read or write fails with NRC_SCLOSED then the session
//    has closed gracefully so exit the loop.
//
DWORD WINAPI ClientThread(PVOID lpParam)
{
    PNCB        pncb = (PNCB)lpParam;
    NCB         ncb;
    char        szRecvBuff[MAX_BUFFER];
    DWORD       dwBufferLen=MAX_BUFFER,
                dwRetVal=NRC_GOODRET;
    char        szClientName[NCBNAMSZ+1];

    FormatNetbiosName(pncb->ncb_callname, szClientName);

    while (1)
    {
        dwBufferLen = MAX_BUFFER;

        dwRetVal = Recv(pncb->ncb_lana_num, pncb->ncb_lsn, szRecvBuff, 
            &dwBufferLen);
        if (dwRetVal != NRC_GOODRET)
            break;
        szRecvBuff[dwBufferLen] = 0;
        printf("READ [LANA=%d]: '%s'\n", pncb->ncb_lana_num, 
            szRecvBuff);

        dwRetVal = Send(pncb->ncb_lana_num, pncb->ncb_lsn, szRecvBuff,
            dwBufferLen);
        if (dwRetVal != NRC_GOODRET)
            break;
    }
    printf("Client '%s' on LANA %d disconnected\n", szClientName, 
        pncb->ncb_lana_num);
 
    if (dwRetVal != NRC_SCLOSED)
    {
        // Some other error occured, hang up the connection
        //
        ZeroMemory(&ncb, sizeof(NCB));
        ncb.ncb_command = NCBHANGUP;
        ncb.ncb_lsn = pncb->ncb_lsn;
        ncb.ncb_lana_num = pncb->ncb_lana_num;

        if (Netbios(&ncb) != NRC_GOODRET)
        {
            printf("ERROR: Netbios: NCBHANGUP: %d\n", ncb.ncb_retcode);
            dwRetVal = ncb.ncb_retcode;
        }
        GlobalFree(pncb);
        return dwRetVal; 
    }
    GlobalFree(pncb);
    return NRC_GOODRET;
}