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

📄 arp.c

📁 mcf5307实验源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/*************************************************************************/
/*                                                                       */
/*    CopyrIght (c)  1993 - 1996 Accelerated Technology, Inc.            */
/*                                                                       */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the      */
/* subject matter of this material.  All manufacturing, reproduction,    */
/* use, and sales rights pertaining to this subject matter are governed  */
/* by the license agreement.  The recipient of this software implicitly  */
/* accepts the terms of the license.                                     */
/*                                                                       */
/*************************************************************************/
/*
*
* Portions of this program were written by:       */
/****************************************************************************
*                                                                           *
*     part of:                                                              *
*     TCP/IP kernel for NCSA Telnet                                         *
*     by Tim Krauskopf                                                      *
*                                                                           *
*     National Center for Supercomputing Applications                       *
*     152 Computing Applications Building                                   *
*     605 E. Springfield Ave.                                               *
*     Champaign, IL  61820                                                  *
*                                                                           *
*****************************************************************************/

/******************************************************************************/
/*                                                                            */
/* FILE NAME                                            VERSION               */
/*                                                                            */
/*      ARP.C                                           NET 3.2               */
/*                                                                            */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*      Hardware level routines, data link layer                              */
/*                                                                            */
/* DATA STRUCTURES                                                            */
/*                                                                            */
/*                                                                            */
/* FUNCTIONS                                                                  */
/*                                                                            */
/*     arp_reply                        Address resolution                    */
/*     arp_request                      Send out an arp request packet        */
/*     arpinterpret                     Interpret ARP Packets                 */
/*     cacheupdate                      Update the ARP cache                  */
/*     cachelook                        Look up a cache entry number          */
/*     netdlayer                        Get data layer address                */
/*     netgetrarp                       Look for a RARP response              */
/*     getdlayer                        Check for the hardware address        */
/*     netsetgate                       Establish a gateway                   */
/*     ARP_Init                         Initialize the ARP component          */
/*     NU_Rarp                          Reverse ARP.                          */
/*     ARP_Event                        Process ARp events.                   */
/*                                                                            */
/* DEPENDENCIES                                                               */
/*                                                                            */
/*     protocol.h        External structure definitions for each type         */
/*                       of prtocol this program handles.                     */
/*     externs.h         External definitions for functions in NCSA           */
/*                       Telnet.                                              */
/*     nucleus.h         contains system constants common to both the         */
/*                       application and Nucleus PLUS components.             */
/*     tcpdefs.h         definitions for Nucleus - UDP program                */
/*     data.h            Declarations of global variables for TCP/IP          */
/*                       libraries                                            */
/*     target.h          Defines applicable to IBM PC/ PC-DOS environmentttttt*/
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/* NAME            DATE                    REMARKS                            */
/*                                                                            */
/* G. Johnson      06/06/96           Added a valid_entry field to the ARP    */
/*                                    cache.                                  */
/* Maiqi Qian      12/06/96           Fixed the time wrap around (spr0229)    */
/* G. Johnson      06/16/97           Updated the support for RARP.           */
/*                                                                            */
/******************************************************************************/

/*
* Includes
*/
#include "protocol.h"
#include "target.h"
#include "socketd.h"
#include "externs.h"
#include "data.h"
#ifdef PLUS
  #include "nucleus.h"
#else
  #include "nu_defs.h"    /* added during ATI mods - 10/20/92, bgh */
  #include "nu_extr.h"
#endif
#include "tcp.h"
#include "tcpdefs.h"
#include "tcp_errs.h"
#include "confile.h"
#include "netevent.h"
#include "arp.h"

/* This is the resolve list.  An item is placed on this list when a MAC address
   needs to be resolved.  Also, used by RARP when resolving for our own IP
   address.  It is removed once the address has been resolved or when failure
   occurs.
 */
ARP_RESOLVE_LIST  ARP_Res_List;

uint16            ARP_Res_Count;

static int32 arptime;
/****************************************************************************/
/* FUNCTION                                                                 */
/*                                                                          */
/*   arp_prepare_pkt                                                        */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*    This function will get a free buffer and fill in as much of the       */
/*    ARP packet fields as possible.  All other fields must be updated by   */
/*    the calling fumction.                                                 */
/*                                                                          */
/* AUTHOR                                                                   */
/*                                                                          */
/*    Glen Johnson,      Accelerated Technology Inc.                        */
/*                                                                          */
/* CALLED BY                                                                */
/*                                                                          */
/*    arp_request                                                           */
/*    arp_reply                                                             */
/*                                                                          */
/* CALLS                                                                    */
/*                                                                          */
/*    dll_dequeue                                                           */
/*                                                                          */
/* INPUTS                                                                   */
/*                                                                          */
/*    uint8 *   tipnum     Target IP address                                */
/*    uint8 *   thardware  Target hardware address                          */
/*    int16     pkt_type   Either an ARP Request or an ARP Reply            */
/*                                                                          */
/* OUTPUTS                                                                  */
/*                                                                          */
/*    struct pqueue *     A buffer that contains the packet to be sent.     */
/*                                                                          */
/* HISTORY                                                                  */
/*                                                                          */
/*    NAME                DATE        REMARKS                               */
/*                                                                          */
/*    Glen Johnson      02/10/95    Initial version.                        */
/*                                                                          */
/****************************************************************************/
struct pqueue HUGE *arp_prepare_pkt (uint8 *tipnum, const uint8 *thardware,
                                     int16 pkt_type)
{
    struct pqueue HUGE *buf_ptr;
    ARPKT *arp_pkt;


    /* Allocate a buffer to place the arp packet in. */
    buf_ptr = (struct pqueue HUGE *)dll_dequeue((tqe_t *)&buffer_freelist);

    if(buf_ptr == NU_NULL)
    {
        return (NU_NULL);
    }

    /* Initialize each field in the allocated buffer. */
    buf_ptr->data_len = sizeof(ARPKT);
    buf_ptr->next = (struct pqueue *)NU_NULL;
    buf_ptr->previous = (struct pqueue *)NU_NULL;
    buf_ptr->seqnum = 0;

    /* Set up a pointer to the packet. */
    arp_pkt = (ARPKT *)buf_ptr->packet;

    /* Copy in the dlayer header. */
    memcpy((void *)&arp_pkt->d, (const void *)&blankd, sizeof(DLAYER));

    /* Set up the target hardware address */
    memcpy((void *)arp_pkt->d.dest, (const void *)thardware, DADDLEN);

    /* Set the type to an ARP packet. */
    arp_pkt->d.type = intswap (EARP);           /* 0x0806 is ARP type */

    arp_pkt->hrd = intswap (HARDWARE_TYPE);    /*  Ether = 1 */
    arp_pkt->pro = intswap (ARPPRO);            /* IP protocol = 0x0800 */
    arp_pkt->hln = DADDLEN;                     /* Ethernet hardware length */
    arp_pkt->pln = 4;                           /* IP length = 4 */

    /* sender's hardware addr */
    memcpy((void *)arp_pkt->sha, (const void *)nnmyaddr, DADDLEN);

    memcpy((void *)arp_pkt->tha, (const void *)thardware, DADDLEN);

    /* sender's IP addr */
    memcpy((void *)arp_pkt->spa, (const void *)nnipnum, 4);

    /* put in IP address we want */
    memcpy((void *)arp_pkt->tpa, (const void *)tipnum, 4);

    arp_pkt->op = intswap(pkt_type);       /* request packet */

    return(buf_ptr);

} /* end arp_prepare_pkt */

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      arp_reply                                                        */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*   Address Resolution Protocol handling.  This can be looked at as     */
/*   Ethernet-dependent, but the data structure can handle any ARP       */
/*   hardware, with minor changes here.                                  */
/*                                                                       */
/* CALLED BY                                                             */
/*      arpinterpret                Interpret ARP Packets                */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      arp_prepare_pkt                                                  */
/*      NET_Send                                                         */
/*                                                                       */
/*************************************************************************/
int16 arp_reply(uint8 *thardware, uint8 *tipnum)
{
      struct pqueue HUGE *buf_ptr;


      buf_ptr = arp_prepare_pkt(tipnum, (const uchar *)thardware, ARPREP);

      if(buf_ptr == NU_NULL)
          return (-1);

      NET_Send(buf_ptr, sizeof(ARPKT), OTHER_PKT);

     /*
      *  check for conflicting IP number with your own
      */
     if (comparen (tipnum, nnipnum, 4))
     {    /* we are in trouble */
		  /* ERROR Conflict with Ethernet hardware address: %2x:%2x:%2x:%2x:%2x:%2x",
           * thardware[0], thardware[1], thardware[2], thardware[3],
           * thardware[4], thardware[5] */
         NU_Tcp_Log_Error (TCP_SAME_IP, TCP_RECOVERABLE,
                __FILE__, __LINE__);
         return (-3);
     }  /* end comparen true */
     return (NU_SUCCESS);        /* ARP ok */

⌨️ 快捷键说明

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