Annotation of embedaddon/trafshow/show_if.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *     Copyright (c) 2004 Rinet Corp., Novosibirsk, Russia
        !             3:  *
        !             4:  * Redistribution and use in source forms, with and without modification,
        !             5:  * are permitted provided that this entire comment appears intact.
        !             6:  *
        !             7:  * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
        !             8:  */
        !             9: 
        !            10: #ifdef HAVE_CONFIG_H
        !            11: #include <config.h>
        !            12: #endif
        !            13: 
        !            14: #include <stdio.h>
        !            15: #include <string.h>
        !            16: #include <pcap.h>
        !            17: 
        !            18: #include "show_if.h"
        !            19: #include "trafshow.h"
        !            20: #include "screen.h"
        !            21: #include "selector.h"
        !            22: #include "addrtoname.h"
        !            23: 
        !            24: #ifdef notdef
        !            25: static void addr2str(const pcap_addr_t *ap, char *addr_buf, int addr_len);
        !            26: #endif
        !            27: 
        !            28: static void
        !            29: scale_size(name, addr, descr)
        !            30:        int *name, *addr, *descr;
        !            31: {
        !            32:        *name   = line_factor * (double)SHOW_IF_NAME;
        !            33:        *addr   = line_factor * (double)SHOW_IF_ADDR;
        !            34:        *descr  = line_factor * (double)SHOW_IF_DESCR;
        !            35: }
        !            36: 
        !            37: static int
        !            38: show_if_header(dst, size, unused)
        !            39:        char *dst;
        !            40:        int size;
        !            41:        const void *unused;
        !            42: {
        !            43:        int name_sz, addr_sz, desc_sz;
        !            44: 
        !            45:        /* sanity check */
        !            46:        if (!dst || size < 1 || unused)
        !            47:                return 0;
        !            48: 
        !            49:        scale_size(&name_sz, &addr_sz, &desc_sz);
        !            50: 
        !            51:        snprintf(dst, size,
        !            52:                 "%-*.*s %-*.*s %-*.*s",
        !            53:                 name_sz, name_sz,      "Interface",
        !            54:                 addr_sz, addr_sz,      "Address",
        !            55:                 desc_sz, desc_sz,      "Description");
        !            56:        return 0;
        !            57: }
        !            58: 
        !            59: #ifdef notdef
        !            60: /* extract list of interface addr/mask pairs */
        !            61: static void
        !            62: addr2str(ap, addr_buf, addr_len)
        !            63:        const pcap_addr_t *ap;
        !            64:        char *addr_buf;
        !            65:        int addr_len;
        !            66: {
        !            67:        int i, rest;
        !            68:        char *cp;
        !            69: 
        !            70:        *addr_buf = '\0';
        !            71:        cp = addr_buf;
        !            72:        rest = addr_len - 1;
        !            73:        for (; ap && rest > 0; ap = ap->next) {
        !            74:                if (ap->addr) {
        !            75:                        if (*addr_buf) {
        !            76:                                *cp++ = ' ';
        !            77:                                *cp = '\0';
        !            78:                                rest--;
        !            79:                        }
        !            80:                        if (satoa(ap->addr, cp, rest)) {
        !            81:                                i = strlen(cp);
        !            82:                                cp += i;
        !            83:                                rest -= i;
        !            84:                        }
        !            85:                }
        !            86:                if (ap->netmask) {
        !            87:                        if (*addr_buf) {
        !            88:                                *cp++ = ' ';
        !            89:                                *cp = '\0';
        !            90:                                rest--;
        !            91:                        }
        !            92:                        if (satoa(ap->netmask, cp, rest)) {
        !            93:                                i = strlen(cp);
        !            94:                                cp += i;
        !            95:                                rest -= i;
        !            96:                        }
        !            97:                }
        !            98:        }
        !            99: }
        !           100: #endif
        !           101: 
        !           102: static int
        !           103: show_if_line(dst, size, ph, idx)
        !           104:        char *dst;
        !           105:        int size;
        !           106:        const PCAP_HANDLER *ph;
        !           107:        int idx;
        !           108: {
        !           109:        int i, name_sz, addr_sz, desc_sz;
        !           110: 
        !           111:        /* sanity check */
        !           112:        if (!dst || size < 1)
        !           113:                return 0;
        !           114: 
        !           115:        *dst = '\0';
        !           116:        for (i = 0; ph; ph = ph->next) {
        !           117:                if (i++ == idx) break;
        !           118:        }
        !           119:        if (!ph) return 0;
        !           120: 
        !           121:        scale_size(&name_sz, &addr_sz, &desc_sz);
        !           122:        snprintf(dst, size,
        !           123:                 "%-*.*s %-*.*s %-*.*s",
        !           124:                 name_sz, name_sz,      ph->name,
        !           125:                 addr_sz, addr_sz,      ph->addrstr,
        !           126:                 desc_sz, desc_sz,      ph->descr ? ph->descr : "");
        !           127:        return 0;
        !           128: }
        !           129: 
        !           130: static int
        !           131: show_if_footer(dst, size, unused)
        !           132:        char *dst;
        !           133:        int size;
        !           134:        const void *unused;
        !           135: {
        !           136:        /* sanity check */
        !           137:        if (!dst || size < 1 || unused)
        !           138:                return 0;
        !           139: 
        !           140:        (void)strncpy(dst, hostname, size);
        !           141:        dst[size-1] = '\0';
        !           142:        return 0;
        !           143: }
        !           144: 
        !           145: SELECTOR *
        !           146: show_if_selector()
        !           147: {
        !           148:        static SELECTOR *sp = 0;
        !           149:        if (!sp && (sp = selector_init()) != 0) {
        !           150:                sp->get_header = show_if_header;
        !           151:                sp->get_line = show_if_line;
        !           152:                sp->get_footer = show_if_footer;
        !           153:        }
        !           154:        return sp;
        !           155: }
        !           156: 
        !           157: int
        !           158: show_if_search(ph, str)
        !           159:        PCAP_HANDLER *ph;
        !           160:        const char *str;
        !           161: {
        !           162:        int idx;
        !           163: 
        !           164:        /* sanity check */
        !           165:        if (!str || *str == '\0')
        !           166:                return -1;
        !           167: 
        !           168:        for (idx = 0; ph; ph = ph->next, idx++) {
        !           169:                if (strstr(ph->name, str))
        !           170:                        return idx;
        !           171:                if (strstr(ph->addrstr, str))
        !           172:                        return idx;
        !           173:        }
        !           174:        return -1;
        !           175: }
        !           176: 
        !           177: SELECTOR *
        !           178: show_if_list(ph)
        !           179:        PCAP_HANDLER *ph;
        !           180: {
        !           181:        SELECTOR *sp;
        !           182: 
        !           183:        /* sanity check */
        !           184:        if (!ph || (sp = show_if_selector()) == 0)
        !           185:                return 0;
        !           186: 
        !           187:        sp->header = 0; /* unused */
        !           188:        sp->footer = 0; /* unused */
        !           189: 
        !           190:        sp->list = ph;
        !           191:        sp->items = 0;
        !           192:        for (; ph; ph = ph->next)
        !           193:                sp->items++;
        !           194: 
        !           195:        return sp;
        !           196: }
        !           197: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>