📄 socket.c
字号:
/***********************************************************************//* *//* Module: socket.c *//* Release: 2001.3 *//* Version: 99.0 *//* Purpose: socket() 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"#include "../tcp/tcp.h"/***********************************************************************//* Global Function Definitions *//***********************************************************************//***********************************************************************//* socket: Assign a socket control block *//* *//* Inputs: family = must be AF_INET *//* type = SOCK_DGRAM or SOCK_STREAM *//* protocol = UDP, TCP, or 0 *//* *//* Returns: Socket identifier if successful, else -1 *//* *//***********************************************************************/int socket(int family, int type, int protocol){ SOCKET sock;#if OS_PARM_CHECK /*-------------------------------------------------------------------*/ /* Verify protocol has been initialized. */ /*-------------------------------------------------------------------*/ if (!Net.Initialized) { NetError(NULL, ENETDOWN); return -1; } /*-------------------------------------------------------------------*/ /* Verify the specified protocol family is AF_INET. */ /*-------------------------------------------------------------------*/ if (family != AF_INET) { NetError(NULL, EPFNOSUPPORT); return -1; }#endif /*-------------------------------------------------------------------*/ /* Acquire access to protocol internals. */ /*-------------------------------------------------------------------*/ semPend(Net.IntSem, WAIT_FOREVER); /*-------------------------------------------------------------------*/ /* Verify there is a free socket descriptor. */ /*-------------------------------------------------------------------*/ if (NetSockList.next_bck == &NetSockList) { NetError(NULL, EMFILE); goto error; } /*-------------------------------------------------------------------*/ /* Check if a stream socket is needed. */ /*-------------------------------------------------------------------*/ if (type == SOCK_STREAM) { /*-----------------------------------------------------------------*/ /* If protocol is not zero, ensure it is TCP. */ /*-----------------------------------------------------------------*/ if (protocol && (protocol != TCP)) { NetError(NULL, EPROTOTYPE); goto error; } sock = TcpNewSock(NO_BUFS); } /*-------------------------------------------------------------------*/ /* Check if a datagram socket is needed. */ /*-------------------------------------------------------------------*/ else if (type == SOCK_DGRAM) { /*-----------------------------------------------------------------*/ /* If protocol is not zero, ensure it is UDP. */ /*-----------------------------------------------------------------*/ if (protocol && (protocol != UDP)) { NetError(NULL, EPROTOTYPE); goto error; } /*-----------------------------------------------------------------*/ /* Allocate socket control block, already know one is available. */ /*-----------------------------------------------------------------*/ sock = SockAlloc(SOCK_DGRAM); } /*-------------------------------------------------------------------*/ /* Only datagram and stream sockets are supported. */ /*-------------------------------------------------------------------*/ else { NetError(NULL, ESOCKTNOSUPPORT); goto error; } /*-------------------------------------------------------------------*/ /* Release access to protocol internals and return. */ /*-------------------------------------------------------------------*/ semPost(Net.IntSem); return (sock - &Socks[0]) + 1;error: semPost(Net.IntSem); return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -