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

📄 nameaddr.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $XConsortium: nameaddr.c,v 1.5 91/09/12 13:41:04 rws Exp $ *//*	nameaddr.c - included by Xstreams.c			*//*	Used for System V Release 4.0 networking code		*//* 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 void   *handlep = NULL;static int    family = 0;static char   tcphalf[4];static int    *tcpfamilyp = NULL;static char   **tcpremoteaddrp = NULL;static int    *tcpremoteaddrlenp = NULL;static int SetupNetworkStream();static int BindAndListen();static int BindAndConnect();static int addentry();/* Routines for handling TLI streams */static intInitializeNetPath(){	if(handlep == NULL && (handlep = setnetpath()) == NULL)	{		nc_perror("Cannot set network selection path\n");		return(-1);	}	return(1);}_XsSetupTliStream(display, dummy)    char *display;    void *dummy;{	int	i, n;	int	fd, type;	struct	utsname  machine;	struct listenCall *tmp;	int	nameserver();	static int firstime=1;	PRMSG("Calling SetupTliStream()\n",0,0);	if(InitializeNetPath() < 0)		return(-1);	dispno = display;	if(uname(&machine) < 0){		t_error("Cannot get nodename");		return(-2);		}			bind_req.addr.buf = req_buf;/* pcc */	if (firstime)	{		Network._nnets = X_TLI_STREAM;		firstime = 0;	}/*	type = Network._nnets;	*/	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 = SetupNetworkStream(			machine.nodename, 			atoi(display), 			BindAndListen, 			&type, 			handlep			);		if( fd < 0)	{         		PRMSG("SetupNetworkStream failed\n",0,0);                        return(-1);	}	_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);			exit(1);	    }	    if((tmp->CurrentCall = (struct t_call *) t_alloc(fd,T_CALL,T_ALL)) == NULL)            {			PRMSG( "t_alloc failed\n",0,0);			exit(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);}int_XsCallTliServer(host, idisplay)    char *host;    int	idisplay;{	int	fd, type;	int	nameserver ();	static  char	firsttime = 1;	PRMSG("Calling CallTliServer()\n",0,0);	if(firsttime == 1)	{		firsttime = 0;		SetupNetworkInfo();		Network._nnets = X_TLI_STREAM;	}		sprintf(_dispno, "%d", idisplay);	dispno = _dispno;	if((fd = SetupNetworkStream(			host, 			idisplay, 			BindAndConnect, 			&type, 			handlep)) < 0)	{			PRMSG("SetupNetworkStream failed\n",0,0);			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);	}	PRMSG("A Connection has been established to %s ... \n", host,0);	_XsTypeOfStream[fd] = type;        if (_XsInputBuffer[fd].DataBuffer == NULL)            if ((_XsInputBuffer[fd].DataBuffer = (char *) malloc(BUFFERSIZE)) == NULL)               {	       errno = ENOMEM;               perror("Client can't connect to remote server");               return (-1);               }	_XsInputBuffer[fd].LastBytePtr = 0;	_XsInputBuffer[fd].FirstBytePtr = 0;	PRMSG("CallTliServer() returns success\n",0,0);	return(fd);}int _XMakeStreamsConnection (name, idisplay, retries,			     familyp, serveraddrlenp, serveraddrp)    char	*name;    int	idisplay;    int	retries;    int	*familyp;		/* return */    int	*serveraddrlenp;	/* return */    char **serveraddrp;		/* return */{	char	netype[128], sysname[128], nodname[128];	char	*procname = "Xlib/_XMakeStreamsConnection";	struct	utsname	 machine;	int	fd; 	PRMSG("GetConnectionType(%s)\n", name, 0);        if(uname(&machine) < 0){		t_error("Cannot get nodename");		return(-1);		}	if(		name == NULL || 		strcmp(name, "") == 0 ||		strcmp(name, "unix") == 0 ||		strcmp(name, "local") == 0 ||		strcmp(name, machine.nodename) == 0 	  )	{	    fd = ((*_XsStream[X_NAMED_STREAM].CallTheListener)		  ("unix", idisplay, "local"));	    if (fd >= 0) {		*familyp = FamilyLocal;		*serveraddrlenp = strlen (machine.nodename);		*serveraddrp = (char *) Xmalloc ((*serveraddrlenp) + 1);		if (!*serveraddrp) {		    *serveraddrlenp = 0;		} else {		    strcpy (*serveraddrp, machine.nodename);		}	    }	    return fd;	}	if((handlep = setnetpath()) == NULL)	{         	nc_perror("Cannot set network selection path\n");		return(-1);	}	/* For backward compatibility, we have to pass authorization	   data in global variables.  Ugh. */	tcpfamilyp = familyp;	tcpremoteaddrp = serveraddrp;	tcpremoteaddrlenp = serveraddrlenp;	fd = (*_XsStream[X_TLI_STREAM].CallTheListener)(name, idisplay);	return(fd);}static intSetupNetworkStream(host, dispno, action, typtr, handlep)    char *host;    int	dispno;    int	(*action)();    int	*typtr;    void *handlep;{	int	i;	char	service[MAX_AUTO_BUF_LEN];	int	fd, type;	struct nd_hostserv  nd_hostserv; 	struct netconfig   *netconfigp = NULL;	struct nd_addrlist *nd_addrlistp = NULL;	struct netbuf	   *netbufp = NULL; #ifdef DEBUGfprintf(stderr, "Calling SetupNetworkStream(%s, %d)\n", host, dispno);#endif        sprintf(service , "xserver%d", dispno);	nd_hostserv.h_host = host;	nd_hostserv.h_serv = service;#ifdef DEBUGfprintf(stderr, "Trying to get the binding address for service %s on %s\n", 					service, host);#endif	while((netconfigp = getnetpath(handlep)) != NULL)	{#ifdef DEBUG	  fprintf(stderr, "Trying to bind using %s\n", netconfigp->nc_device);#endif	  if(netdir_getbyname(netconfigp, &nd_hostserv, &nd_addrlistp) == 0)	  {#ifdef DEBUG	  fprintf(stderr, "There are %d ways\n", nd_addrlistp->n_cnt);#endif            netbufp = nd_addrlistp->n_addrs;            for(i=0; i< nd_addrlistp->n_cnt; i++)            {		#ifdef DEBUG		fprintf(stderr, "Address: len %d maxlen %d \n", 			netbufp->len, netbufp->maxlen);#endif		if( strcmp(netconfigp->nc_netid, "starlan") == 0 )		{			register char *from, *to;			int	i, len;			from = to = netbufp->buf;			len = 0;			for(i=0; i< netbufp->len; i++, from++)				if(*from != '.')				{					*to++ = *from;						len++;				}			*to = '\0';			netbufp->len = len;		}		#ifdef DEBUG		fprintf(stderr, "Address: maxlen %d buf ", netbufp->maxlen);		dumpBytes(netbufp->len, netbufp->buf);#endif		if((fd = (*action)(netconfigp->nc_device, netbufp)) < 0)		{			netbufp++;			continue;		}		if(                   strcmp(netconfigp->nc_protofmly, "inet") == 0 &&                   strcmp(netconfigp->nc_proto    , "tcp") == 0          	  )		{			memcpy(tcphalf, netbufp->buf, 4);			if (tcpfamilyp != NULL) {			    *tcpfamilyp = FamilyInternet;			    *tcpremoteaddrlenp = 4;			    *tcpremoteaddrp = Xmalloc(*tcpremoteaddrlenp);			    /* This is a kludge.  What is the right way to get			       this info out? */			    memcpy(*tcpremoteaddrp, netbufp->buf+4,				   *tcpremoteaddrlenp);#ifdef DEBUG			    fprintf(stderr, "tcp remote addr = %0x\n",				    *(long *)*tcpremoteaddrp);#endif			}		}		type = 0;		for(i=X_TLI_STREAM; i< Network._nnets; i++)		 if(strcmp(Network._net[i]->nc_netid, netconfigp->nc_netid) == 0)		 {			type = i;			break;		 }		if(type == 0)		{			Network._net[Network._nnets] = netconfigp;			type = Network._nnets++;		}		*typtr = type;	  	/* free(netconfigp) the right way */		(void) netdir_free((char *)nd_addrlistp, ND_ADDRLIST);		return(fd);            }	    /* free(nd_addrlistp) the right way */	    (void) netdir_free((char *)nd_addrlistp, ND_ADDRLIST);	  }#ifdef DEBUG	  else netdir_perror("netdir_getbyname() failed");#endif	}	return(-1);}static intBindAndListen(clonedev, netbufp)    char	*clonedev;    struct   netbuf *netbufp;{	int	fd;	struct  t_bind bindbuf;	bindbuf.addr.buf = netbufp->buf;        bindbuf.addr.len = netbufp->len;        bindbuf.addr.maxlen = netbufp->maxlen;				if ((fd = t_open(clonedev,  O_RDWR, NULL)) < 0)	{		fprintf(stderr, "Cannot open %s\n", clonedev);		return(-1);	}#ifdef DEBUG	fprintf(stderr, "opening device %s\n", clonedev);#endif	bindbuf.qlen = 8;	if(t_bind (fd, &bindbuf, NULL) < 0)	{		t_error("t_bind failed");		close(fd);		return(-1);	}	return(fd);}static intBindAndConnect(clonedev, netbufp)    char	*clonedev;    struct   netbuf *netbufp;{	int	fd;	struct  t_call callbuf;	callbuf.addr.buf = netbufp->buf;	callbuf.addr.len = netbufp->len;

⌨️ 快捷键说明

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