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

📄 inet.c

📁 一个基于linux的TCP/IP协议栈的实现
💻 C
字号:
/* inet.c * linqianghe@163.com * 2006-10-20 */#include "net-support.h"#include "util.h"#include "af_inet.h"#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <errno.h>#include <netdb.h>#include <stdlib.h>#include <string.h>struct addr {	struct sockaddr_in addr;	char *name;	int host;	struct addr *next;};static struct addr *MYINET_nn = NULL;static int MYINET_rresolve(char *name, size_t len, struct sockaddr_in *sin,             int numeric, unsigned int netmask){	struct hostent *ent;	struct netent *np;	struct addr *pn;	unsigned long ad, host_ad;	int host = 0;	if (sin->sin_family != MY_AF_INET) {		fprintf(stderr, "rresolve: unsupport address family %d !\n", sin->sin_family);		errno = EAFNOSUPPORT;		return (-1);	}	ad = (unsigned long) sin->sin_addr.s_addr;	if( ad == INADDR_ANY ){		if( (numeric & 0x0FFF) == 0 ){			if( numeric & 0x8000 )				safe_strncpy( name, "default", len );			else				safe_strncpy(name, "*", len);			return (0);		}	}	if( numeric & 0x0FFF ){		safe_strncpy( name, inet_ntoa(sin->sin_addr), len );		return (0);	}	if ((ad & (~netmask)) != 0 || (numeric & 0x4000))		host = 1;	pn = MYINET_nn;	while (pn != NULL) {		if( pn->addr.sin_addr.s_addr == ad && pn->host == host ){			safe_strncpy(name, pn->name, len);			fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);			return (0);		}		pn = pn->next;	}	host_ad = ntohl(ad);	np = NULL;	ent = NULL;	if( host ){		fprintf( stderr, "gethostbyaddr (%08lx)\n", ad );		ent = gethostbyaddr((char *) & ad, 4, AF_INET);		if( ent != NULL )			safe_strncpy(name, ent->h_name, len);	}else{		fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);		np = getnetbyaddr(host_ad, MY_AF_INET);		if( np != NULL )			safe_strncpy(name, np->n_name, len);	}	if( (ent == NULL) && (np == NULL) )		safe_strncpy(name, inet_ntoa(sin->sin_addr), len);	pn = (struct addr *) malloc(sizeof(struct addr));	pn->addr = *sin;	pn->next = MYINET_nn;	pn->host = host;	pn->name = (char *) malloc(strlen(name) + 1);	strcpy(pn->name, name);	MYINET_nn = pn;	return (0);}static char *MYINET_print(unsigned char *ptr){	return ( inet_ntoa((*(struct in_addr *) ptr)) );}static char *MYINET_sprint(struct sockaddr *sap, int numeric){	static char buff[128];	if( sap->sa_family == 0xFFFF || sap->sa_family == 0 )		return safe_strncpy(buff, "[NONE SET]", sizeof(buff));	if( MYINET_rresolve(buff, sizeof(buff), (struct sockaddr_in *)sap,							numeric, 0xffffff00) != 0)		return (NULL);	return (buff);}static int MYINET_resolve(char *name, struct sockaddr_in *sin, int hostfirst){	struct hostent *hp;	struct netent *np;	sin->sin_family = MY_AF_INET;	sin->sin_port = 0;	if( !strcmp(name, "default") ){		sin->sin_addr.s_addr = INADDR_ANY;		return (1);	}	if( inet_aton(name, &sin->sin_addr) )		return 0;	if( hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL ){		memcpy( (char *) &sin->sin_addr, (char *) hp->h_addr_list[0], sizeof(struct in_addr) );		return 0;	}	if( (np = getnetbyname(name)) != (struct netent *) NULL) {		sin->sin_addr.s_addr = htonl(np->n_net);		return 1;	}	if( hostfirst ){    	errno = h_errno;    	return -1;    }	if( (hp = gethostbyname(name)) == (struct hostent *) NULL ){		errno = h_errno;		return -1;	}	memcpy( (char *) &sin->sin_addr, (char *) hp->h_addr_list[0],					sizeof(struct in_addr));	return 0;}static int MYINET_getsock(char *bufp, struct sockaddr *sap){	return 0;}static int MYINET_input(int type, char *bufp, struct sockaddr *sap){	switch( type ){		case 1:			return ( MYINET_getsock(bufp, sap) );		case 256:			return ( MYINET_resolve(bufp, (struct sockaddr_in *) sap, 1) );		default:			return ( MYINET_resolve(bufp, (struct sockaddr_in *) sap, 0) );	}}static void MYINET_reserror(char *text){	herror(text);}static int MYINET_getnetmask(char *adr, struct sockaddr *m, char *name){	struct sockaddr_in *mask = (struct sockaddr_in *) m;	char *slash, *end;	int prefix;	if( (slash = strchr(adr, '/')) == NULL )		return 0;	*slash++ = '\0';	prefix = strtoul( slash, &end, 0 );	if( *end != '\0' )		return -1;	if( name ){		sprintf(name, "/%d", prefix);	}	mask->sin_family = MY_AF_INET;	mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));	return 1;}struct aftype myinet_aftype ={	"myinet", NULL, MY_AF_INET, sizeof(unsigned long),	MYINET_print, MYINET_sprint, MYINET_input, MYINET_reserror,	NULL, NULL,	MYINET_getnetmask,	-1,	NULL};

⌨️ 快捷键说明

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