⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sock_bind1.c

📁 SocketApi是在netembryo库上修改而成的
💻 C
字号:
/* *  *  $Id$ *   *  This file is part of NetEmbryo  * * NetEmbryo -- default network wrapper  * *  Copyright (C) 2005 by *  	 *	- Federico Ridolfo	<federico.ridolfo@polito.it> *  *  NetEmbryo is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  NetEmbryo is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with NetEmbryo; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *   *  this piece of code is taken from NeMeSI source code * */#include <wsocket.h>int sock_bind(char *host, char *port, int *sock, sock_type socktype){	int n;	struct addrinfo *res, *ressave;	struct addrinfo hints;#ifdef HAVE_LIBSCTP	struct sctp_initmsg initparams;	struct sctp_event_subscribe subscribe;#endif	memset(&hints, 0, sizeof(struct addrinfo));	if (host == NULL)		hints.ai_flags = AI_PASSIVE;	else		hints.ai_flags = AI_CANONNAME;#ifdef IPV6	hints.ai_family = AF_UNSPEC;#else	hints.ai_family = AF_INET;#endif	switch (socktype) {	case SCTP:#ifndef HAVE_LIBSCTP		net_log(NET_LOG_ERR, "SCTP protocol not compiled in\n");		return WSOCK_ERROR;		break;#endif	// else go down to TCP case (SCTP and TCP are both SOCK_STREAM type)	case TCP:		hints.ai_socktype = SOCK_STREAM;		break;	case UDP:		hints.ai_socktype = SOCK_DGRAM;		break;	default:		net_log(NET_LOG_ERR, "Unknown socket type specified\n");		return WSOCK_ERROR;		break;	}	if ((n = gethostinfo(&res, host, port, &hints)) != 0) {		net_log(NET_LOG_ERR, "%s\n", gai_strerror(n));			return WSOCK_ERRADDR;	}		ressave = res;	do {#ifdef HAVE_LIBSCTP		if (socktype == SCTP)			res->ai_protocol = IPPROTO_SCTP;#endif // TODO: remove this code when SCTP will be supported from getaddrinfo()		if ((*sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)			continue;#ifdef HAVE_LIBSCTP		if (socktype == SCTP) {			// Enable the propagation of packets headers			memset(&subscribe, 0, sizeof(subscribe));			subscribe.sctp_data_io_event = 1;			if (setsockopt(*sock, SOL_SCTP, SCTP_EVENTS, &subscribe,					sizeof(subscribe)) < 0) {				net_log(NET_LOG_ERR, "setsockopts(SCTP_EVENTS) error in sctp_open.\n");				return WSOCK_ERROR;				}			// Setup number of streams to be used for SCTP connection			memset(&initparams, 0, sizeof(initparams));			initparams.sinit_max_instreams = MAX_SCTP_STREAMS;			initparams.sinit_num_ostreams = MAX_SCTP_STREAMS;			if (setsockopt(*sock, SOL_SCTP, SCTP_INITMSG, &initparams,					sizeof(initparams)) < 0) {				net_log(NET_LOG_ERR, "setsockopts(SCTP_INITMSG) error in sctp_open.\n");				return WSOCK_ERROR;				}		}#endif                if (bind(*sock, res->ai_addr, res->ai_addrlen) == 0)			break;		if (close(*sock) < 0)			return WSOCK_ERROR;	} while ((res = res->ai_next) != NULL);	freeaddrinfo(ressave);	if ( !res )		return WSOCK_ERROR;	return 0;}

⌨️ 快捷键说明

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