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

📄 ripx.c

📁 一个演示使用ipx协议在局域网来传递包的例子
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <dos.h>

typedef unsigned char   BYTE;
typedef unsigned short  WORD;
typedef unsigned long   DWORD;
typedef unsigned char   BOOL;

#define TRUE            1
#define FALSE           0

#include "ripx.h"

#define IPX_INT         0x7a
#define BYTESWAP(x)     _asm mov    ax, x\
                        _asm xchg   ah, al\
                        _asm mov    x, ax


static WORD                 wIPXErrorCode;
static BOOL                 bIPXEnabled=FALSE;
static IPX_LOCAL_ADDRESS    localAddress;

WORD IPX_GetLastError( void )
{
    if ( !bIPXEnabled ) return( 0xffff );
    return( wIPXErrorCode );
}

BOOL IPX_Available( void )
{
    /*  Test if IPX is set up okay  */

    union REGS      regs;
    unsigned        uSeg, uOff;

    wIPXErrorCode = 0x0000;
    if ( bIPXEnabled ) return( TRUE );

    int_getvector( IPX_INT,&uOff,&uSeg );
    if ( !uSeg && !uOff ) return( FALSE );
    regs.h.ah = IPX_INT;
    regs.h.al = 0x00;
    int86( 0x2f,&regs,&regs );
    bIPXEnabled = (regs.h.al==0xff?TRUE:FALSE);

    if ( bIPXEnabled )
    {
        /*  Determine our local address  */

        struct SREGS    sregs;

        memset( &localAddress,0x00,sizeof(localAddress) );

        regs.x.bx = 0x0009;
//*TBA*  In PMode, this won't work!!!
        regs.x.si = (unsigned short)(((unsigned long)&localAddress)&0xffff);
        sregs.es  = (unsigned short)(((unsigned long)&localAddress)>>16);
        int86x( IPX_INT,&regs,&regs,&sregs );
    }

    return( bIPXEnabled );
}

WORD IPX_OpenSocket( BOOL bPermanent,WORD wSocket )
{
    /*  Attempt to open a socket  */

    union REGS      regs;

    if ( !bIPXEnabled ) return( 0xffff );

    regs.x.bx = 0x0000;
    BYTESWAP(wSocket);
    regs.h.al = (bPermanent?0xff:0x00);
    regs.x.dx = wSocket;
    int86( IPX_INT,&regs,&regs );
    wIPXErrorCode = (WORD)(BYTE)regs.h.al;

    if ( wIPXErrorCode==0 )
    {
        wSocket = regs.x.dx;
        BYTESWAP( wSocket );
    }
    else wSocket=0xffff;

    return( wSocket );
}

BOOL IPX_CloseSocket( WORD wSocket )
{
    /*  Close a socket  */

    union REGS      regs;

    if ( !bIPXEnabled ) return( FALSE );

    regs.x.bx = 0x0001;
    BYTESWAP( wSocket );
    regs.x.dx = wSocket;
    int86( IPX_INT,&regs,&regs );
    wIPXErrorCode = 0x0000;
    return( TRUE );
}

BOOL IPX_GetLocalAddr( IPX_LOCAL_ADDRESS *pLocalAddr )
{
    if ( !bIPXEnabled ) return( FALSE );
    memcpy( pLocalAddr,&localAddress,sizeof(IPX_LOCAL_ADDRESS) );
    wIPXErrorCode=0x0000;
    return( TRUE );
}

BOOL IPX_SetIdleFlag( void )
{
    union REGS      regs;

    if ( !bIPXEnabled ) return( FALSE );
    regs.x.bx = 0x000a;
    int86( IPX_INT,&regs,&regs );
    wIPXErrorCode=0x0000;
    return( TRUE );
}

BOOL IPX_SendPacket( IPX_ECB *pECB )
{
    union REGS      regs;
    struct SREGS    sregs;

    if ( !bIPXEnabled ) return( FALSE );
    regs.x.bx = 0x0003;
//*TBA* This won't work for PMode...
    regs.x.si = (unsigned short)(((unsigned long)pECB)&0xffff);
    sregs.es  = (unsigned short)(((unsigned long)pECB)>>16);
    int86x( IPX_INT,&regs,&regs,&sregs );
    wIPXErrorCode=0x0000;
    return( TRUE );
}

BOOL IPX_ListenForPacket( IPX_ECB *pECB )
{
    union REGS      regs;
    struct SREGS    sregs;

    if ( !bIPXEnabled ) return( FALSE );
    regs.x.bx = 0x0004;
//*TBA* This won't work for PMode...
    regs.x.si = (unsigned short)(((unsigned long)pECB)&0xffff);
    sregs.es  = (unsigned short)(((unsigned long)pECB)>>16);
    int86x( IPX_INT,&regs,&regs,&sregs );
    wIPXErrorCode = (WORD)regs.h.al;
    return( (wIPXErrorCode==0) );
}

BOOL IPX_InitSendPacket( IPX_HEADER *pHDR, IPX_ECB *pECB, WORD wSocket, WORD wDataLength )
{
    /*  Intiailise packet header and event control block for send  */

    WORD        wRevLength=wDataLength+sizeof(IPX_HEADER);

    if ( !bIPXEnabled ) return( FALSE );

    memset( pHDR,0x00,sizeof(IPX_HEADER) );
    memset( pECB,0x00,sizeof(IPX_ECB) );

    /*  Fill out ECB  */

    BYTESWAP( wRevLength );
    BYTESWAP( wSocket );

    pECB->wSocket                   = wSocket;
    pECB->wFragCount                = 1;
//*TBA* The following won't work for PMode!!!!!
    pECB->addrFrag.dwOffsetSegment  = (DWORD)pHDR;
    pECB->wFragSize                 = sizeof(IPX_HEADER) + wDataLength;
    memset( &pECB->immedAddr,0xff,sizeof(pECB->immedAddr) );

    /*  Fill out header  */

    pHDR->wChecksum                 = 0xffff;
    pHDR->wLength                   = wRevLength;
    pHDR->cbPacketType              = 0x00;
    pHDR->destAddr.netAddr.dwAddr   = localAddress.netAddr.dwAddr;
    memset( &pHDR->destAddr.nodeAddr,0xff,sizeof(pHDR->destAddr.nodeAddr) );
    pHDR->destAddr.wSocket          = wSocket;
    pHDR->srcAddr.netAddr.dwAddr    = localAddress.netAddr.dwAddr;
    memcpy( &pHDR->srcAddr.nodeAddr,&localAddress.nodeAddr,sizeof(pHDR->srcAddr.nodeAddr) );
    pHDR->srcAddr.wSocket           = wSocket;

    wIPXErrorCode = 0x0000;

    return( TRUE );
}

BOOL IPX_InitReceivePacket( IPX_HEADER *pHDR, IPX_ECB *pECB, WORD wSocket, WORD wDataLength )
{
    /*  Initialise packet header and event control block for receive  */

    if ( !bIPXEnabled ) return( FALSE );

    memset( pHDR,0x00,sizeof(IPX_HEADER) );
    memset( pECB,0x00,sizeof(IPX_ECB) );

    /*  Fill out ECB  */

    BYTESWAP( wSocket );

    pECB->cbInUse                   = (BYTE)0x1d;
    pECB->wSocket                   = wSocket;
    pECB->wFragCount                = 1;
//*TBA* The following won't work for PMode!!!!!
    pECB->addrFrag.dwOffsetSegment  = (DWORD)pHDR;
    pECB->wFragSize                 = sizeof(IPX_HEADER) + wDataLength;

    /*  Tell driver to watch out for our packet(s)  */

    IPX_ListenForPacket( pECB );

    wIPXErrorCode = 0x0000;
    return( TRUE );
}

⌨️ 快捷键说明

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