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

📄 get_myaddress.c

📁 操作系统SunOS 4.1.3版本的源码
💻 C
字号:
#if !defined(lint) && defined(SCCSIDS)static char sccsid[] = "@(#)get_myaddress.c 1.1 92/07/30 Copyr 1990 Sun Micro";#endif/* * get_myaddress.c * * Get client's IP address via ioctl.  This avoids using the NIS. * Copyright (C) 1990, Sun Microsystems, Inc. */#include <rpc/types.h>#include <rpc/pmap_prot.h>#include <sys/socket.h>#include <stdio.h>#include <net/if.h>#include <sys/ioctl.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/syslog.h>/* * don't use gethostbyname, which would invoke NIS */get_myaddress(addr)	struct sockaddr_in *addr;{	int s;	char buf[BUFSIZ*8];	struct ifconf ifc;	struct ifreq ifreq, *ifr;	int len;	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {	    (void) syslog(LOG_ERR, "get_myaddress: socket: %m");	    exit(1);	}	ifc.ifc_len = sizeof (buf);	ifc.ifc_buf = buf;	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {		(void) syslog(LOG_ERR,		    "get_myaddress: ioctl (get interface configuration): %m");		exit(1);	}	ifr = ifc.ifc_req;	for (len = ifc.ifc_len; len; len -= sizeof ifreq) {		ifreq = *ifr;		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {			(void) syslog(LOG_ERR, "get_myaddress: ioctl: %m");			exit(1);		}		if ((ifreq.ifr_flags & IFF_UP) &&		    ifr->ifr_addr.sa_family == AF_INET) {			*addr = *((struct sockaddr_in *)&ifr->ifr_addr);			addr->sin_port = htons(PMAPPORT);			break;		}		ifr++;	}	(void) close(s);}

⌨️ 快捷键说明

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