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

📄 nameserver.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $XConsortium: nameserver.c,v 1.3 91/07/23 11:50:04 rws Exp $ *//*	nameserver.c - included by Xstreams.c			*//*	Used for System V Release 3.2 networking code ONLY	*//* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.  * Copyright 1991 Massachusetts Institute of Technology * Copyright 1988, 1989 AT&T, Inc. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby * granted, provided that the above copyright notice appear in all * copies and that both that copyright notice and this permission * notice appear in supporting documentation, and that the name of * AT&T, USL, or MIT not be used in advertising or publicity * pertaining to distribution of the software without specific, * written prior permission.  AT&T, USL, and MIT make no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied * warranty. * * AT&T, USL, AND MIT DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN * NO EVENT SHALL AT&T, USL, OR MIT BE LIABLE FOR ANY SPECIAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */#ifdef XSTREAMS_COMPILE /* magic symbol to avoid lint problems */static int svr4plus = 0;static char clonedev[MAX_AUTO_BUF_LEN];static int OpenAndBind();static int Read();/* Routines for handling TLI streams */_XsSetupTliStream(display, stype)    char *display;    char *stype;{	int	i, n;	int	fd, type;	struct	utsname  machine;	struct listenCall *tmp;	int	nameserver();	static int first=1;	PRMSG("Calling SetupTliStream()\n",0,0);        if(NameServer < 0 &&		 (NameServer = OpenLocalServer(NAME_SERVER_NODE)) < 0)	{                        return(-1);	}	dispno = display;	if(uname(&machine) < 0){		t_error("Cannot get nodename");		return(-2);		}			bind_req.addr.buf = req_buf;	n = strlen(stype) +1;/* pcc */	if(first)	{		Network._nnets = X_TLI_STREAM;		first = 0;	}	type = Network._nnets++;	if((Network._net[type] = malloc(n)) == NULL){             		PRMSG( "malloc failed\n",0,0);			return(-2);			}	bcopy(stype, Network._net[type], n);	bind_ret.addr.buf = ret_buf;	call.addr.buf	  = call_buf;	bind_req.addr.maxlen = MAXLEN;	bind_ret.addr.maxlen = MAXLEN;	call.addr.maxlen     = MAXLEN;	fd = OpenAndBind(machine.nodename, atoi(display), MAXCONNECTIONS, Network._net[type], type);		if( fd < 0){		PRMSG("Cannot OpenAndBind %s", machine.nodename,0);		free(Network._net[type]);		return(-2);		}	_XsTypeOfStream[fd] = type;/* * set up call save list for general network listen service */	for (i = 0; i < LISTEN_QUE_SIZE; ++i) 	{	    if((tmp = (struct listenCall *) malloc(sizeof(struct listenCall))) == NULL)	    {			PRMSG( "malloc failed\n",0,0);			return(-1);	    }	    if((tmp->CurrentCall = (struct t_call *) t_alloc(fd,T_CALL,T_ALL)) == NULL)            {			PRMSG( "t_alloc failed\n",0,0);			return(-1);	    }	    Que(&Network.FreeList[type], tmp, CLEAR);	}	if(Network._npeers > 0 && Network._peer == NULL)	{		register	i;		register	char	*ptr;		int		n;		n =  (Network._npeers + 1) * 			(sizeof(int) + sizeof(char *) + (1 + UNAME_LENGTH));		PRMSG("Allocating %d chars for %d peeers names", 							n, Network._npeers);		if((ptr = malloc(n)) == NULL){			fprintf(stderr,"Cannot malloc space for peers names\n");			exit(1);		}		Network._peerlen = (int *) ptr;		ptr += Network._npeers * sizeof(int);		Network._peer = (char **) ptr; 		ptr += Network._npeers * sizeof(char *);		for(i= 0; i< Network._npeers; i++)		{			Network._peerlen[i] = 0;			Network._peer[i]    = ptr;#ifdef DEBUG			fprintf(stderr, "peer[%d] is %u; peerlen[%d] is %u\n",				i, Network._peer[i], i, &Network._peerlen[i]);#endif			ptr += (1 + UNAME_LENGTH);		}	}	PRMSG("SetupTliStream () (success) fd = %d\n", fd,0);	return(fd);}_XsCallTliServer(host, idisplay, nettype)    char *host;    int	idisplay;    char *nettype;{	int	fd, i, t;	PFV	savef;	int	netlen, type;	char	*retptr, *ptr;	char	first = 1;	char	netbuffer[MAX_AUTO_BUF_LEN];	static	char	firstime = 1;	PRMSG("Calling CallTliServer()\n",0,0);        if(NameServer<0 && (NameServer = OpenLocalServer(NAME_SERVER_NODE)) < 0)	{			return(-1);	}	if(firstime)	{		SetupNetworkInfo();		Network._nnets = X_TLI_STREAM;		firstime = 0;	}		sprintf(_dispno, "%d", idisplay);	dispno = _dispno;	savef = signal (SIGALRM, dummy);/*  * Give up after MAX_TRIES tries or for CONNECT_TIME seconds or an error * occurred which comes first.*/	retptr = NULL;#define MAX_TRIES	3#define CONNECT_TIME    10	alarm (CONNECT_TIME);	if(svr4plus)	{		int	naddrs, n, j;		if(GetNetworkInfo (-1, nettype, ConvertNameToTliCall,                      addheader(host, strlen(host)), &retptr, &naddrs) <= 0)		{                	fprintf(stderr, "Cannot create address for system %s \n", 			host);			t = -1;			goto outofloop;		}                call.opt.len = 0;                call.opt.maxlen = 0;                call.opt.buf = NULL;                call.udata.len = 0;                call.udata.maxlen = 0;                call.udata.buf = NULL;#ifdef DEBUGfprintf(stderr, "We got %d addresses for host %s\n", naddrs, host);fprintf(stderr, "NETPATH sent to daemon is %s\n", nettype);#endif		ptr = retptr;		for(i=0; i< naddrs; i++)		{                        call.addr.len = ((xHostEntry *) ptr)->length;                        call.addr.maxlen = ((xHostEntry *) ptr)->length;                        call.addr.buf = (ptr+sizeof(xHostEntry));			call.addr.buf[call.addr.len] = '\0';#ifdef DEBUG			fprintf(stderr, "ADDRESS LENGTH IS %d\n", call.addr.len);			fprintf(stderr, "Address returned is <%s>\n",call.addr.buf);#endif			ptr += (((sizeof(xHostEntry) + call.addr.len+3) >> 2) << 2);			n =  ((xHostEntry *) ptr)->length;			if(n > 0)                                nettype = (ptr+sizeof(xHostEntry));			else	nettype = NULL;					ptr += (((sizeof(xHostEntry) + n+3) >> 2) << 2);			n = ((xHostEntry *) ptr)->length;			sprintf(clonedev, "%s", (ptr+sizeof(xHostEntry)));			ptr += (((sizeof(xHostEntry) + n+3) >> 2) << 2);#ifdef DEBUG            		fprintf(stderr, "Clonedev is %s\n", clonedev);            		fprintf(stderr, "netid is %s\n", nettype);#endif						fd = OpenAndBind(NULL, -1, 0, nettype, X_TLI_STREAM);			if(fd  < 0)			{				PRMSG("Openandbind failed\n",0,0);				continue;				}			t = -1;	                PRMSG("Connecting to %s ... \n", host, 0);			if((t = t_connect(fd, &call, NULL)) < 0)			{       	          		if(t_errno == TLOOK)       	                 	{       	                  		checkNewEvent(fd);       	                         	t_close(fd);       	                 	}       	                 	else       	                 	{       	                  		t_error("t_connect failed");       	                         	t_close(fd);       	                 	}			} else break;		}	}	else	for(i=0; i < MAX_TRIES;i++)	{			if((fd = OpenAndBind(NULL, -1, 0, nettype, X_TLI_STREAM)) < 0)		{				PRMSG("Openandbind failed\n",0,0);				break;			}	 	if(first)		{			first = 0;	   		if( GetNetworkInfo (-1, nettype, ConvertNameToTliCall,				 addheader(host, strlen(host)),  &retptr, NULL) <= 0)			{				fprintf(stderr,					"Cannot create address for system %s\n",host);				t = -1;				goto outofloop;	   		}			ptr = retptr;	   		call.addr.len = ((xHostEntry *) ptr)->length;	   		call.addr.maxlen = ((xHostEntry *) ptr)->length;			call.addr.buf = (ptr+sizeof(xHostEntry));					call.addr.buf[call.addr.len] = '\0';#ifdef DEBUG			fprintf(stderr, "ADDRESS LENGTH IS %d\n", call.addr.len);			fprintf(stderr, "Address returned is <%s>\n",call.addr.buf);#endif			ptr += (((sizeof(xHostEntry) + call.addr.len+3) >> 2) << 2);			call.opt.len = ((xHostEntry *) ptr)->length;			call.opt.maxlen = ((xHostEntry *) ptr)->length;			if(call.opt.len > 0)				call.opt.buf = (ptr+sizeof(xHostEntry));			else	call.opt.buf = NULL;					ptr += (((sizeof(xHostEntry) + call.opt.len+3) >> 2) << 2);			call.udata.len = ((xHostEntry *) ptr)->length;			call.udata.maxlen = ((xHostEntry *) ptr)->length;			if(call.udata.len > 0){				call.udata.buf = (ptr+sizeof(xHostEntry));#ifdef DEBUG			fprintf(stderr, "ADDRESS LENGTH IS %d\n", call.udata.len);                        fprintf(stderr, "Address returned is <%s>\n",call.udata.buf);#endif			}			else	call.udata.buf = NULL;#ifdef DEBUG			fprintf(stderr, "addrlen %d optlen %d udatalen %d\n",					call.addr.len,					call.opt.len,					call.udata.len);#endif		}		t = -1;		PRMSG("Connecting to %s ... \n", host, 0);		if((t = t_connect(fd, &call, NULL)) < 0)		{			if(t_errno == TLOOK)			{				checkNewEvent(fd);		        	t_close(fd);				continue;			}			else			{				t_error("t_connect failed");				t_close(fd);				break;			}		} else break;	}outofloop:#undef MAX_TRIES#undef CONNECT_TIME	alarm (0);	signal (SIGALRM, savef);	close(NameServer);	NameServer = -1;	netlen = strlen(nettype);	if(netlen > 127)	{		netlen = 127;		nettype[netlen] = '\0';	}	memcpy(netbuffer, nettype, netlen + 1);	if(retptr != NULL)		free(retptr);	if(t < 0)	{		close(fd);		return(-1);	}/*	if (t_rcvconnect (fd, &call) < 0) {		if(t_errno == TLOOK)			checkNewEvent(fd);		t_close(fd);		t_error ("t_rcvconnect failed!");		return(-1);	}*/	if(ioctl(fd, I_POP, "timod") < 0)	{	PRMSG("failed to pop timod\n", 0, 0);	}	if(ioctl(fd, I_PUSH, "tirdwr") < 0)	{         	t_close(fd);		return(-1);	}        if (_XsInputBuffer[fd].DataBuffer == NULL)            if ((_XsInputBuffer[fd].DataBuffer = (char *) malloc(BUFFERSIZE)) == NULL)               {		        errno = ENOMEM;               		perror("Client can't connect to remote server");			close(fd);               		return (-1);               }	_XsInputBuffer[fd].LastBytePtr = 0;	_XsInputBuffer[fd].FirstBytePtr = 0;	type = -1;	for(i= X_TLI_STREAM; i< Network._nnets; i++)	{		if(strcmp(nettype, Network._net[i]) == 0)		{			type = i;

⌨️ 快捷键说明

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