Ejemplo n.º 1
0
/*
 * Return the name of the network whose address is given.
 * The address is assumed to be that of a net or subnet, not a host.
 */
char *
netname(uint32_t in, uint32_t mask)
{
	char *cp = 0;
	static char line[MAXHOSTNAMELEN];
	uint32_t omask, dmask;
	uint32_t i;

	i = ntohl(in);
	dmask = forgemask(i);
	omask = mask;

	if (cp)
		strncpy(line, cp, sizeof(line) - 1);
	else {
		switch (dmask) {
		case IN_CLASSA_NET:
			if ((i & IN_CLASSA_HOST) == 0) {
				sprintf(line, "%u", C(i >> 24));
				break;
			}
			/* FALLTHROUGH */
		case IN_CLASSB_NET:
			if ((i & IN_CLASSB_HOST) == 0) {
				sprintf(line, "%u.%u",
					C(i >> 24), C(i >> 16));
				break;
			}
			/* FALLTHROUGH */
		case IN_CLASSC_NET:
			if ((i & IN_CLASSC_HOST) == 0) {
				sprintf(line, "%u.%u.%u",
					C(i >> 24), C(i >> 16), C(i >> 8));
				break;
			}
Ejemplo n.º 2
0
Archivo: route.c Proyecto: MarginC/kame
/*
 * Return the name of the network whose address is given.
 * The address is assumed to be that of a net or subnet, not a host.
 */
char *
netname(u_long in, u_long mask)
{
	char *cp = 0;
	static char line[MAXHOSTNAMELEN];
	struct netent *np = 0;
	u_long dmask;
	u_long i;

#define	NSHIFT(m) (							\
	(m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT :			\
	(m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT :			\
	(m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT :			\
	0)

	i = ntohl(in);
	dmask = forgemask(i);
	if (!numeric_addr && i) {
		np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
		if (np == NULL && mask == 0)
			np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
		if (np != NULL) {
			cp = np->n_name;
			trimdomain(cp, strlen(cp));
		}
	}
Ejemplo n.º 3
0
static void
domask(char *dst, uint32_t addr, uint32_t mask)
{
	int b, i;

	if (!mask || (forgemask(addr) == mask)) {
		*dst = '\0';
		return;
	}
	i = 0;
	for (b = 0; b < 32; b++)
		if (mask & (1 << b)) {
			int bb;

			i = b;
			for (bb = b+1; bb < 32; bb++)
				if (!(mask & (1 << bb))) {
					i = -1;	/* noncontig */
					break;
				}
			break;
		}
	if (i == -1)
		sprintf(dst, "&0x%x", mask);
	else
		sprintf(dst, "/%d", 32-i);
}
Ejemplo n.º 4
0
/*
 * Return the name of the network whose address is given.
 * The address is assumed to be that of a net or subnet, not a host.
 */
char *
netname(u_long in, u_long mask)
{
    char *cp = 0;
    static char line[MAXHOSTNAMELEN];
    struct netent *np = 0;
    u_long dmask;
    u_long i;

#define	NSHIFT(m) (							\
	(m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT :			\
	(m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT :			\
	(m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT :			\
	0)

    i = ntohl(in);
    dmask = forgemask(i);
    if (!numeric_addr && i)
    {
        //np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
        //if (np == NULL && mask == 0)
        //	np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
        //if (np != NULL) {
        //	cp = np->n_name;
        //trimdomain(cp, strlen(cp));
        //	printf("LUOYU cp is %s\n", cp);
        //}
    }
#undef NSHIFT
#define C(x)	(unsigned)((x) & 0xff)

    if (cp != NULL)
    {
        strncpy(line, cp, sizeof(line) - 1);
        line[sizeof(line) - 1] = '\0';
    }
    else
    {
        switch (dmask)
        {
        case IN_CLASSA_NET:
            if ((i & IN_CLASSA_HOST) == 0)
            {
                sprintf(line, "%lu", C(i >> 24));
                break;
            }
            /* FALLTHROUGH */
        case IN_CLASSB_NET:
            if ((i & IN_CLASSB_HOST) == 0)
            {
                sprintf(line, "%lu.%lu",
                        C(i >> 24), C(i >> 16));
                break;
            }
            /* FALLTHROUGH */
        case IN_CLASSC_NET:
            if ((i & IN_CLASSC_HOST) == 0)
            {
                sprintf(line, "%lu.%lu.%lu",
                        C(i >> 24), C(i >> 16), C(i >> 8));
                break;
            }