sockets.c
来自「mcf5307实验源代码」· C语言 代码 · 共 1,494 行 · 第 1/5 页
C
1,494 行
Scon.ndom = 3; /* number of retries for domain requests */
Scon.address = ram_addr; /* hardware address */
Scon.ioaddr = io_addr; /* io address of hardware */
Scon.wire = 1; /* thin or thick ethernet wire */
status = Snetinit();
#if SNMP_INCLUDED
/* Update the SNMP Address Translation Table. */
SNMP_atTableUpdate(SNMP_ADD, 1, nnmyaddr, nnipnum);
/* Update the SNMP IP address table. The last parameter is the max size
packet that can be reassembled. At this time we can not reassemble
packets.
*/
SNMP_ipAdEntUpdate(SNMP_ADD, 1, nnipnum, nnmask, (broadip[3] & 0x1), 0);
/* Update the SNMP IP Address Translation Table. */
SNMP_ipNetToMediaTableUpdate(SNMP_ADD, 1, nnmyaddr, nnipnum, 4);
#endif /* SNMP_INCLUDED */
return(status);
} /* NU_Init_Net */
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* NU_Socket */
/* */
/* DESCRIPTION */
/* */
/* This function is responsible for establishing a new socket */
/* descriptor and defining the type of communication protocol to */
/* be established. Must be called by both clien and server whether */
/* connection-oriented or connectionless transfer is established. */
/* */
/* CALLED FROM */
/* */
/* User Applications */
/* */
/* CALLS */
/* */
/* NU_Obtain_Semaphore */
/* NU_Release_Semaphore */
/* NU_proto_list */
/* NU_Allocate_Memory */
/* */
/*************************************************************************/
int16 NU_Socket (int16 family, int16 type, int16 protocol)
{
struct sock_struct *sockptr; /* pointer to current socket */
#ifdef PLUS
STATUS status;
#else
sint status; /* status of memory allocation */
#endif
sint return_status = NU_NO_SOCKET_SPACE; /* initialized to error status */
sint counter; /* to traverse the socket list */
/* possible protocols based on [FAMILY, TYPE] */
uint NU_proto_list[][5] =
{
{NU_PROTO_INVALID, NU_PROTO_INVALID, NU_PROTO_INVALID,
NU_PROTO_INVALID, NU_PROTO_INVALID},
{NU_PROTO_TCP, NU_PROTO_UDP, NU_PROTO_ICMP,
NU_PROTO_INVALID, NU_PROTO_INVALID},
{NU_PROTO_INVALID, NU_PROTO_INVALID, NU_PROTO_INVALID,
NU_PROTO_INVALID, NU_PROTO_INVALID}
};
/* Make sure that family and type are in range. */
if (family < 0 || family > 2 || type < 0 || type > 4)
return (NU_INVALID_PROTOCOL);
// sprintf(buffer11,"Sockin-%u",++sock_times_in);
// __TextOut(10,180,12,buffer11);
/* Clean up warnings. This parameter is used for socket compatibility
but we are currently not making any use of it. */
unused_parameter = protocol;
/* Don't let any other users in until we are done. */
#ifdef PLUS
NU_Obtain_Semaphore(&TCP_Resource, NU_SUSPEND);
#else
NU_Request_Resource(TCP_Resource, NU_WAIT_FOREVER);
#endif
/* search the socket list to be sure there is room for another
connection */
for (counter = 0; counter < NSOCKETS; counter++)
{
if (socket_list[next_socket_no] == NU_NULL)
{
/* verify that we support the programmer's choice */
if(NU_proto_list[family][type])
{
/* allocate a socket structure */
#ifdef PLUS
status = NU_Allocate_Memory(&System_Memory, (VOID *) &sockptr,
(UNSIGNED)sizeof(struct sock_struct),
(UNSIGNED)NU_NO_SUSPEND);
#else
status = NU_Alloc_Memory (sizeof(struct sock_struct),
(unsigned int **)&sockptr,
NU_NO_TIMEOUT);
#endif
if (status == NU_SUCCESS)
{
sockptr = normalize_ptr(sockptr);
socket_list[next_socket_no] = sockptr;
/* fill only the protocol portion of the socket structure */
sockptr->protocol = NU_proto_list[family][type];
/* initialize the port and ip portions of the socket */
sockptr->local_addr.ip_num.is_ip_addrs[0] = NULL_IP;
sockptr->local_addr.ip_num.is_ip_addrs[1] = NULL_IP;
sockptr->local_addr.ip_num.is_ip_addrs[2] = NULL_IP;
sockptr->local_addr.ip_num.is_ip_addrs[3] = NULL_IP;
sockptr->local_addr.port_num = NU_IGNORE_VALUE;
sockptr->foreign_addr.ip_num.is_ip_addrs[0] = NULL_IP;
sockptr->foreign_addr.ip_num.is_ip_addrs[1] = NULL_IP;
sockptr->foreign_addr.ip_num.is_ip_addrs[2] = NULL_IP;
sockptr->foreign_addr.ip_num.is_ip_addrs[3] = NULL_IP;
sockptr->foreign_addr.port_num = NU_IGNORE_VALUE;
sockptr->block = NU_FALSE;
sockptr->listener = NU_FALSE;
/* Initialize the socket options. The abiltiy to transmit
broadcast messages is enabled by default. */
sockptr->so_options = SO_BROADCAST;
/* setup the Optimized Data Handling field */
sockptr->odh_flag = NU_ODH_DISABLE;
/* return the socket_list index */
return_status = next_socket_no;
next_socket_no++;
if (next_socket_no >= NSOCKETS)
next_socket_no = 0;
} /* end status == NU_SUCCESS */
else
{
NU_Tcp_Log_Error (TCP_SESS_MEM, TCP_RECOVERABLE,
__FILE__, __LINE__);
/* Tell the user there is no memory. */
return_status = NU_NO_SOCK_MEMORY;
} /* end status != NU_SUCCESS */
} /* end check for support for programmer's choice */
else
{
/* return an error code */
return_status = NU_INVALID_PROTOCOL;
} /* end no support for programmer's choice */
/* discontinue after we find an empty space in the socket list */
break;
} /* end socket_list slot is null */
next_socket_no++;
if (next_socket_no >= NSOCKETS)
next_socket_no = 0;
} /* end for loop */
#ifdef PLUS
NU_Release_Semaphore(&TCP_Resource);
#else
NU_Release_Resource(TCP_Resource);
#endif
// sprintf(buffer11,"Sockout-%u",++sock_times_out);
// __TextOut(10,200,12,buffer11);
/* return a socket_list index or an error status to caller */
return (return_status);
} /* end of NU_Socket */
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* NU_Bind */
/* */
/* DESCRIPTION */
/* */
/* This function is responsible for assigning a local address */
/* to a socket. */
/* */
/* CALLED FROM */
/* */
/* User Applications */
/* */
/* CALLS */
/* */
/* NU_Obtain_Semaphore */
/* NU_Release_Semaphore */
/* */
/*************************************************************************/
int16 NU_Bind(int16 socketd, struct addr_struct *myaddr, int16 addrlen)
{
/* Clean up warnings. This parameter is used for socket compatibility
but we are currently not making any use of it. */
unused_parameter = addrlen;
/* Validate the socket number. */
if ((socketd < 0) || (socketd >= NSOCKETS))
return(NU_INVALID_SOCKET);
/* Don't let any other users in until we are done. */
#ifdef PLUS
NU_Obtain_Semaphore(&TCP_Resource, NU_SUSPEND);
#else
NU_Request_Resource(TCP_Resource, NU_WAIT_FOREVER);
#endif
/* Fill the local portion of the socket descriptor */
memcpy (&(socket_list[socketd]->local_addr.ip_num), &(myaddr->id), 4);
socket_list[socketd]->local_addr.port_num = (int16)myaddr->port;
/* Allow others to use the TCP resource */
#ifdef PLUS
NU_Release_Semaphore(&TCP_Resource);
#else
NU_Release_Resource(TCP_Resource);
#endif
/* Return the updated socket descriptor to the caller */
return(socketd);
} /* end of NU_Bind */
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* NU_Listen */
/* */
/* DESCRIPTION */
/* */
/* This function is responsible for indicating that the server is */
/* willing to accept connection requests from clients. */
/* */
/* CALLED FROM */
/* */
/* User Applications */
/* */
/* CALLS */
/* */
/* NU_Obtain_Semaphore */
/* NU_Release_Semaphore */
/* NU_Allocate_Memory */
/* NU_Current_Task_Pointer */
/* NU_Task_Table_Add */
/* */
/* HISTORY */
/* NAME DATE REMARKS */
/* */
/* B. Harwell 10/21/92 Created Initial version. */
/* G. Johnson 10/26/95 Combined 3 calls to allocate memory */
/* into 1 for efficiency. */
/* */
/*************************************************************************/
int16 NU_Listen(int16 socketd, uint16 backlog)
{
int16 return_status = NU_SUCCESS; /* initialize to SUCCESS */
#ifdef PLUS
STATUS status;
NU_TASK *task_id;
#else
int16 status; /* status of memory allocation */
int16 task_id;
#endif
char *return_ptr; /* pointer to memory block */
struct TASK_TABLE_STRUCT
*Task_Entry; /* structure of connections for
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?