📄 sockets.c
字号:
/*************************************************************************
*
* Copyright (c) 1993 - 2001 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 4.4
*
* DESCRIPTION
*
* This file defines our interface to the functions using the Berkely
* socket standard TCP/IP interface.
*
* DATA STRUCTURES
*
* SCK_Ticks_Per_Second
* next_socket_no
* SCK_Host_Name[MAX_HOST_NAME_LENGTH]
* *SCK_Sockets[NSOCKETS]
*
* FUNCTIONS
*
* NU_Abort
* NU_Accept
* NU_Bind
* NU_Close_Socket
* NU_Connect
* NU_Fcntl
* NU_Get_Host_by_Addr
* NU_Get_Host_by_Name
* NU_Get_Peer_Name
* NU_Get_Sock_Name
* NU_Get_UDP_Pnum
* NU_Getsockopt
* NU_Init_Net
* NU_Ioctl
* NU_Is_Connected
* NU_Listen
* NU_Push
* NU_Recv
* NU_Recv_From
* SCK_SearchTaskList
* NU_Send
* NU_Send_To
* NU_Setsockopt
* NU_Socket
* NU_Socket_Connected
* SCK_TaskTable_Entry_Delete
* NU_Add_Route
* NU_Recv_From_Raw
* NU_Send_To_Raw
* SCK_Clear_Accept_Entry
* SCK_Check_Listeners
* SCK_Create_Socket
* SCK_Suspend_Task
* timer_task
*
* DEPENDENCIES
*
* nucleus.h
* target.h
* externs.h
* ip.h
* tcp.h
* udp.h
* netevent.h
* socketd.h
* dns.h
* ld_extr.h
* arp.h
* ipraw.h
* tcpdefs.h
* nerrs.h
* netevent.h
*
**************************************************************************/
#include "plus/nucleus.h"
#include "net/target.h"
#include "net/inc/externs.h"
#include "net/inc/ip.h"
#include "net/inc/tcp.h"
#include "net/inc/udp.h"
#include "net/inc/netevent.h"
#include "net/inc/socketd.h"
#include "net/inc/dns.h"
#include "net/inc/ld_extr.h"
#include "net/inc/arp.h"
#include "net/inc/ipraw.h"
/* the following includes are for Nucleus references */
#include "net/inc/tcpdefs.h"
#include "net/inc/nerrs.h"
#include "net/inc/netevent.h"
#if (INCLUDE_SNMP == NU_TRUE)
#include SNMP_GLUE
#endif
/* 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);
#if (INCLUDE_LOOPBACK_DEVICE == NU_TRUE)
/* Declare and init the global to hold the name of the
loopback device. */
CHAR loopback_device_name[] = "loopback";
#endif
/* This global will contain just what it is named, the number
of Nucleus PLUS timer ticks in one second. This varible will
be initialized by the NU_Init_Net function and the value
should be supplied by PLUS. See TARGET.H for how this value
is determined, look for TICKS_PER_SECOND. */
UINT32 SCK_Ticks_Per_Second;
/* 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.
*/
INT next_socket_no;
/* Holds the local host name */
UINT8 SCK_Host_Name[MAX_HOST_NAME_LENGTH];
/* Import externals */
#if (INCLUDE_DNS == NU_TRUE)
/* IP addresses for DNS servers. */
extern CHAR DNS_Server[DNS_MAX_DNS_SERVERS][IP_ADDR_LEN];
#endif
/*
* The following structure is used to define the socket list for Nucleus NET.
* Sockets are requried for all TCP connections.
*/
struct sock_struct *SCK_Sockets[NSOCKETS];
/*************************************************************************
*
* FUNCTION
*
* NU_Init_Net
*
*
* DESCRIPTION
*
* This function initializes the network for Nucleus NET TCP/IP
* operations. It should only be called once.
*
* INPUTS
*
* mem_pool Pointer to the memory pool.
*
* OUTPUTS
*
* NU_INVAL
* NU_MEM_ALLOC
* NU_SUCCESS
*
*************************************************************************/
#if (NET_VERSION_COMP >= NET_4_2)
STATUS NU_Init_Net(NU_MEMORY_POOL *mem_pool)
#else
STATUS NU_Init_Net(VOID)
#endif
{
STATUS status;
#if (INCLUDE_LOOPBACK_DEVICE == NU_TRUE)
NU_DEVICE loopback_device;
#endif
VOID *pointer;
NU_SUPERV_USER_VARIABLES
/* Switch to supervisor mode. */
NU_SUPERVISOR_MODE();
/* Initialize the SCK_Sockets pointer-arry. MQ 12/13/96. */
UTL_Zero((CHAR *)SCK_Sockets, sizeof(SCK_Sockets));
/* Initialize next_socket_no */
next_socket_no = 0;
/* Initialize the host name to zeros */
UTL_Zero(SCK_Host_Name, MAX_HOST_NAME_LENGTH);
/* Initialize the net_stack_name to the default name */
memcpy(SCK_Host_Name, HOSTNAME, strlen(HOSTNAME));
/* Initialized the number of Nucleus PLUS timer ticks in one second.
This is used for various time related function of Nucleus NET. */
SCK_Ticks_Per_Second = TICKS_PER_SECOND;
/* Create Event queue. */
status = NU_Allocate_Memory(&System_Memory, &pointer,
(UNSIGNED)(SCK_EVENT_Q_NUM_ELEMENTS *
SCK_EVENT_Q_ELEMENT_SIZE
* sizeof(UNSIGNED)),
(UNSIGNED)NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_MEM_ALLOC);
}
pointer = TLS_Normalize_Ptr(pointer);
status = NU_Create_Queue(&eQueue, "EvtQueue", pointer, (UNSIGNED)SCK_EVENT_Q_NUM_ELEMENTS *
SCK_EVENT_Q_ELEMENT_SIZE,
NU_FIXED_SIZE, (UNSIGNED)SCK_EVENT_Q_ELEMENT_SIZE, NU_FIFO);
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_INVAL);
}
/* Create synchronization semaphore. */
status = NU_Create_Semaphore(&TCP_Resource, "TCP", (UNSIGNED)1, NU_FIFO);
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_INVAL);
}
status = NU_Create_Event_Group(&Buffers_Available, "BUFAVA");
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_INVAL);
}
/* Create Event Queue Dispatcher task. */
status = NU_Allocate_Memory(&System_Memory, &pointer, (UNSIGNED)5000,
(UNSIGNED)NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_MEM_ALLOC);
}
pointer = TLS_Normalize_Ptr(pointer);
status = NU_Create_Task(&NU_EventsDispatcher_ptr, "EvntDisp",
NU_EventsDispatcher, (UNSIGNED)0, NU_NULL, pointer,
(UNSIGNED)5000, EV_PRIORITY,
(UNSIGNED)0, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_INVAL);
}
/* Create timer task */
status = NU_Allocate_Memory(&System_Memory, &pointer, (UNSIGNED)5000,
(UNSIGNED)NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
/* Switch back to user mode. */
NU_USER_MODE();
return(NU_MEM_ALLOC);
}
pointer = TLS_Normalize_Ptr(pointer);
status = NU_Create_Task(&timer_task_ptr, "TIMER", timer_task, (UNSIGNED)0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -