📄 accept.c
字号:
/***********************************************************************//* *//* Module: accept.c *//* Release: 2001.3 *//* Version: 99.0 *//* Purpose: accept() implementation *//* *//*---------------------------------------------------------------------*//* *//* Copyright 1999, Blunk Microsystems *//* ALL RIGHTS RESERVED *//* *//* Licensees have the non-exclusive right to use, modify, or extract *//* this computer program for software development at a single site. *//* This program may be resold or disseminated in executable format *//* only. The source code may not be redistributed or resold. *//* *//***********************************************************************/#include "../tcp_ipp.h"/***********************************************************************//* Global Function Definitions *//***********************************************************************//***********************************************************************//* accept: Accept incoming TCP connection requests *//* *//* Inputs: s = socket identifier *//* *addrlen = sizeof(struct sockaddr_in) *//* *//* Output: If addr not NULL, *addr = address of connected peer *//* *//* Returns: New socket ID or else -1 with errno set to error code *//* *//***********************************************************************/int accept(int s, void *addr, int *addrlen){ SOCKET new, sock = &Socks[s - 1];#if OS_PARM_CHECK /*-------------------------------------------------------------------*/ /* Verify protocol has been initialized. */ /*-------------------------------------------------------------------*/ if (!Net.Initialized) { NetError(NULL, ENETDOWN); return -1; } /*-------------------------------------------------------------------*/ /* Check for valid socket ID. */ /*-------------------------------------------------------------------*/ if (InvalidHandle(s)) { NetError(NULL, ENOTSOCK); return -1; } /*-------------------------------------------------------------------*/ /* Verify that addrlen and *addrlen are appropriate values. */ /*-------------------------------------------------------------------*/ if (addr) { if ((addrlen == NULL) || (*addrlen != sizeof(struct sockaddr_in))) { NetError(sock, EFAULT); return -1; } } /*-------------------------------------------------------------------*/ /* Verify that socket type is TCP. */ /*-------------------------------------------------------------------*/ if (sock->type != SOCK_STREAM) { NetError(sock, EOPNOTSUPP); return -1; } /*-------------------------------------------------------------------*/ /* Verify that socket state is listen. */ /*-------------------------------------------------------------------*/ if (sock->state != SS_LISTEN) { NetError(sock, EINVAL); return -1; }#endif /*-------------------------------------------------------------------*/ /* Gain exclusive socket API access and stack internals access. */ /*-------------------------------------------------------------------*/ if (semPend(sock->api_access, WAIT_FOREVER)) { NetError(NULL, ENOTSOCK); return -1; } semPend(Net.IntSem, WAIT_FOREVER); /*-------------------------------------------------------------------*/ /* Check if there is no pending connection request. */ /*-------------------------------------------------------------------*/ if (sock->lq_head == NULL) { /*-----------------------------------------------------------------*/ /* Return error if socket is non-blocking. */ /*-----------------------------------------------------------------*/ if (sock->flags & SF_NONBLKNG) { NetError(sock, EWOULDBLOCK); goto accept_error; } /*-----------------------------------------------------------------*/ /* Wait until a connection arrives. Return error if closed. */ /*-----------------------------------------------------------------*/ NetPendEvent(sock, SE_ACCEPTED, WAIT_FOREVER); if (sock->lq_head == NULL) { NetError(sock, sock->error); goto accept_error; } } /*-------------------------------------------------------------------*/ /* Extract new socket from listen queue. */ /*-------------------------------------------------------------------*/ new = sock->lq_head; sock->lq_head = sock->lq_head->lq_next; --sock->q_count; /*-------------------------------------------------------------------*/ /* If requested, output connection's remote SockAddr. */ /*-------------------------------------------------------------------*/ if (addr) *(struct sockaddr_in *)addr = new->remote; /*-------------------------------------------------------------------*/ /* Release exclusive API and internals access and return new socket. */ /*-------------------------------------------------------------------*/ semPost(sock->api_access); semPost(Net.IntSem); return (new - &Socks[0]) + 1;accept_error: semPost(sock->api_access); semPost(Net.IntSem); return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -