sockets.c

来自「mcf5307实验源代码」· C语言 代码 · 共 1,494 行 · 第 1/5 页

C
1,494
字号

/****************************************************************************/
/*                                                                          */
/*       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 implicity accepts the terms   */
/* of the license.                                                          */
/*                                                                          */
/****************************************************************************/
/******************************************************************************/
/*                                                                            */
/* FILENAME                                                 VERSION           */
/*                                                                            */
/*  sockets.c                                                 3.2             */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*  This file defines our interface to the functions using the Berkely        */
/*  socket standard TCP/IP interface.                                         */
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*  Craig L. Meredith, Accelerated Technology Inc.                            */
/*                                                                            */
/* DATA STRUCTURES                                                            */
/*                                                                            */
/*  global compenent data stuctures defined in this file                      */
/*                                                                            */
/* FUNCTIONS                                                                  */
/*                                                                            */
/*     NU_Init_Net                                                            */
/*     NU_Socket                                                              */
/*     NU_Bind                                                                */
/*     NU_Listen                                                              */
/*     NU_Accept                                                              */
/*     NU_Send                                                                */
/*     NU_Send_To                                                             */
/*     NU_Recv                                                                */
/*     NU_Recv_From                                                           */
/*     NU_Abort                                                               */
/*     NU_Close_Socket                                                        */
/*     connect_sock                                                           */
/*     NU_UpdateTaskTable                                                     */
/*     NU_TaskTableAdd                                                        */
/*     NU_SearchTaskList                                                      */
/*     NU_GetPnum                                                             */
/*     NU_Get_UDP_Pnum                                                        */
/*     NU_fcntl                                                               */
/*     NU_Get_Host_ID                                                         */
/*     NU_Connect                                                             */
/*     NU_Get_Host_By_Name                                                    */
/*     NU_Get_Peer_Name                                                       */
/*     NU_Socket_Connected                                                    */
/*     NU_Push                                                                */
/*     NU_Is_Connected                                                        */
/*     NU_Setsockopt                                                          */
/*     NU_Getsockopt                                                          */
/*     timer_task                                                             */
/*                                                                            */
/* DEPENDENCIES                                                               */
/*                                                                            */
/*  No other file dependencies                                                */
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*    NAME               DATE        REMARKS                                  */
/*                                                                            */
/*  Barbara G. Harwell   10/06/92   Initial version.                          */
/*  Craig L. Meredith    02/15/94   Clean up and changed code for             */
/*                                  the interrupt version.                    */
/*  Glen Johnson         04/30/96   Made some changes based on recommedations */
/*                                  of K. Goto of Hitachi.                    */
/*  Maiqi Qian           02/26/97   Added new function NU_Abort               */
/*  Maiqi Qian           04/10/97   Added SNMP control                        */
/*                                                                            */
/******************************************************************************/
#ifdef PLUS
  #include "nucleus.h"
#else /* PLUS */
  #include "nu_defs.h"
  #include "nu_extr.h"
#endif /* !PLUS */
#include "externs.h"
#include "data.h"
#include "target.h"
#include "protocol.h"
#include "sockext.h"
#include "netevent.h"
#include "confile.h"
#include "socketd.h"
#include "dns.h"

#if SNMP_INCLUDED
#include "snmp_g.h"
extern uint32 cfig_hostid;
#endif

/* the following includes are for Nucleus references */
#include "tcpdefs.h"
#include "tcp_errs.h"
#include "netevent.h"

#ifdef PLUS
  /* Global Declarations */
  NU_QUEUE          eQueue;
  NU_SEMAPHORE      TCP_Resource;
  NU_TASK           NU_EventsDispatcher_ptr;
  NU_TASK           timer_task_ptr;
  NU_EVENT_GROUP    Buffers_Available;

  VOID    NU_EventsDispatcher(UNSIGNED argc, VOID *argv);
  VOID    timer_task (UNSIGNED argc, VOID *argv);
#endif

/*  The following value is used to remove warnings from the compiler
	for unused parameters.  */
sint unused_parameter;
// modified by Ason. (1998.9.2.)
//extern sint SusTask_Queer;
// following for debug only.
unsigned long In_SusTask = 0;
unsigned long Out_SusTask = 0;
unsigned short sock_times_in = 0;
unsigned short sock_times_out = 0;
unsigned short RlsS_times_in = 0;
unsigned short RlsS_times_out = 0;
unsigned short GetS_times_in = 0;
unsigned short GetS_times_out = 0;
unsigned short Clean_times_in = 0;
unsigned short Clean_times_out = 0;
unsigned short TaskD_times_in = 0;
unsigned short TaskD_times_out = 0;
unsigned short MemoD_times_in = 0;
unsigned short MemoD_times_out = 0;
unsigned short While_times_in = 0;
unsigned short While_times_out = 0;

unsigned short RsumT_times_in = 0;
unsigned short RsumT_times_out = 0;
unsigned short RsumR_times_in = 0;
unsigned short RsumR_times_out = 0;


//unsigned long In_Conn = 0;
unsigned long Out_Conn = 0;
//extern NU_TASK Net_Task_C_CB;
//extern NU_TASK Net_Task_R_CB;
//extern NU_TASK Net_Task_S_CB;
extern unsigned short syntimes;
extern unsigned short syntimes_add;


char buffer11[40];
char buffer12[40];
char buffer13[40];
char buffer14[60];

/*  next_socket_no is used to record the last position searched in the
	socketlist.  The next time the socket list is serached an unused socket
	should be found immediately.  The alternative is to begin searchin from the
	start of the list everytime.  Chances are a lot of used sockets would be
	found before finding an unused one.
*/
sint next_socket_no;

int16 NU_TaskTable_Entry_Delete(int16);

/******************************************************************************/
/*                                                                            */
/*  FUNCTION                                "NU_Init_Net"                     */
/*                                                                            */
/*                                                                            */
/*  DESCRIPTION                                                               */
/*                                                                            */
/*      This function initializes the network for Nucleus NET TCP/IP          */
/*      operations.  It should only be called once.                           */
/*                                                                            */
/*  AUTHOR                                                                    */
/*                                                                            */
/*      Neil Henderson              Accelerated Technology, Inc.              */
/*                                                                            */
/*  CALLED FROM                                                               */
/*                                                                            */
/*      User Applications                                                     */
/*                                                                            */
/*  ROUTINES CALLED                                                           */
/*                                                                            */
/*      Snetinit                    Network initialization routine.           */
/*                                                                            */
/*  INPUTS                                                                    */
/*                                                                            */
/*      None                                                                  */
/*                                                                            */
/*  OUTPUTS                                                                   */
/*                                                                            */
/*      status                      status of the initialization              */
/*                                                                            */
/*         NAME            DATE       REMARKS                                 */
/*                                                                            */
/*     G. Johnson        05/31/96     Moved creation of NET tasks to after    */
/*                                    creation of resources (semaphores etc.) */
/*                                                                            */
/******************************************************************************/
sint NU_Init_Net(int8 * ip_address, int16 irq, uint32 ram_addr,
                  uint32 io_addr)
{
    sint     status;
#ifdef PLUS
    VOID     *pointer;
#endif

    /* Initialize the socket_list pointer-arry. MQ  12/13/96. */
	for (next_socket_no=0; next_socket_no<NSOCKETS; next_socket_no++)
        socket_list[next_socket_no] = NU_NULL;

    /* Initialize next_socket_no */
    next_socket_no = 0;

#if SNMP_INCLUDED
    /* Initialize the SNMP agent's IP address. */
    cfig_hostid = *(uint32 *)ip_address;
#endif

#ifdef PLUS
    /* Create Event queue.  */
    status = NU_Allocate_Memory(&System_Memory, &pointer,
                            (UNSIGNED)(2400*sizeof(UNSIGNED)),
                            (UNSIGNED)NU_NO_SUSPEND);

    if (status != NU_SUCCESS)
        return(-1);

    pointer = normalize_ptr(pointer);

    status = NU_Create_Queue(&eQueue, "EvtQueue", pointer, (UNSIGNED)800,
                            NU_FIXED_SIZE, (UNSIGNED)3, NU_FIFO);
    if (status != NU_SUCCESS)
        return(-1);

    /* Create synchronization semaphore.  */
    status = NU_Create_Semaphore(&TCP_Resource, "TCP", (UNSIGNED)1, NU_FIFO);
    if (status != NU_SUCCESS)
        return(-1);

    status = NU_Create_Event_Group(&Buffers_Available, "BUFAVA");
    if (status != NU_SUCCESS)
        return(-1);

    /* Create Event Queue Dispatcher task.  */
    status = NU_Allocate_Memory(&System_Memory, &pointer, (UNSIGNED)2000,
                                (UNSIGNED)NU_NO_SUSPEND);
    if (status != NU_SUCCESS)
        return(-1);

    pointer = normalize_ptr(pointer);

    status = NU_Create_Task(&NU_EventsDispatcher_ptr, "EvntDisp",
                        NU_EventsDispatcher, (UNSIGNED)0, NU_NULL, pointer,
                        (UNSIGNED)2000, EV_PRIORITY, (UNSIGNED)0, NU_PREEMPT,
                        NU_START);
    if (status != NU_SUCCESS)
        return(-1);


    /* Create timer task */
    status = NU_Allocate_Memory(&System_Memory, &pointer, (UNSIGNED)3000,
                               (UNSIGNED)NU_NO_SUSPEND);
   if (status != NU_SUCCESS)
        return(-1);

    pointer = normalize_ptr(pointer);

   status = NU_Create_Task(&timer_task_ptr, "TIMER", timer_task, (UNSIGNED)0,
                            NU_NULL, pointer, (UNSIGNED)3000, TM_PRIORITY,
                            (UNSIGNED)0, NU_PREEMPT, NU_START);
   if (status != NU_SUCCESS)
        return(-1);

#endif /* PLUS */

    /* Initialize the each field in Scon. */
    /* Initialize the  irq number. */
    Scon.irqnum = (uchar)irq;

    /* Set the IP number. */
    memcpy((VOID *)Scon.myipnum, (const VOID *)ip_address, IPADDLEN);

    /* Set up the broadcast IP address. */
    memcpy((VOID *)Scon.broadip, (const VOID *)broadip, DADDLEN);

    /* Set up the local host name. */
    strcpy((char *)Scon.me, HOSTNAME);

    /* Setup the nameserver = 1-domain 2-IEN116  */
    Scon.nstype = 1;

⌨️ 快捷键说明

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