File: show_if.c

package info (click to toggle)
netdiag 1.2-1
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 3,268 kB
  • sloc: ansic: 23,895; sh: 5,544; makefile: 328; awk: 130
file content (197 lines) | stat: -rw-r--r-- 3,428 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 *	Copyright (c) 2004 Rinet Corp., Novosibirsk, Russia
 *
 * Redistribution and use in source forms, with and without modification,
 * are permitted provided that this entire comment appears intact.
 *
 * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
 */

#ifdef	HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <pcap.h>

#include "show_if.h"
#include "trafshow.h"
#include "screen.h"
#include "selector.h"
#include "addrtoname.h"

#ifdef	notdef
static void addr2str(const pcap_addr_t *ap, char *addr_buf, int addr_len);
#endif

static void
scale_size(name, addr, descr)
	int *name, *addr, *descr;
{
	*name	= line_factor * (double)SHOW_IF_NAME;
	*addr	= line_factor * (double)SHOW_IF_ADDR;
	*descr	= line_factor * (double)SHOW_IF_DESCR;
}

static int
show_if_header(dst, size, unused)
	char *dst;
	int size;
	const void *unused;
{
	int name_sz, addr_sz, desc_sz;

	/* sanity check */
	if (!dst || size < 1 || unused)
		return 0;

	scale_size(&name_sz, &addr_sz, &desc_sz);

	snprintf(dst, size,
		 "%-*.*s %-*.*s %-*.*s",
		 name_sz, name_sz,	"Interface",
		 addr_sz, addr_sz,	"Address",
		 desc_sz, desc_sz,	"Description");
	return 0;
}

#ifdef	notdef
/* extract list of interface addr/mask pairs */
static void
addr2str(ap, addr_buf, addr_len)
	const pcap_addr_t *ap;
	char *addr_buf;
	int addr_len;
{
	int i, rest;
	char *cp;

	*addr_buf = '\0';
	cp = addr_buf;
	rest = addr_len - 1;
	for (; ap && rest > 0; ap = ap->next) {
		if (ap->addr) {
			if (*addr_buf) {
				*cp++ = ' ';
				*cp = '\0';
				rest--;
			}
			if (satoa(ap->addr, cp, rest)) {
				i = strlen(cp);
				cp += i;
				rest -= i;
			}
		}
		if (ap->netmask) {
			if (*addr_buf) {
				*cp++ = ' ';
				*cp = '\0';
				rest--;
			}
			if (satoa(ap->netmask, cp, rest)) {
				i = strlen(cp);
				cp += i;
				rest -= i;
			}
		}
	}
}
#endif

static int
show_if_line(dst, size, ph, idx)
	char *dst;
	int size;
	const PCAP_HANDLER *ph;
	int idx;
{
	int i, name_sz, addr_sz, desc_sz;

	/* sanity check */
	if (!dst || size < 1)
		return 0;

	*dst = '\0';
	for (i = 0; ph; ph = ph->next) {
		if (i++ == idx) break;
	}
	if (!ph) return 0;

	scale_size(&name_sz, &addr_sz, &desc_sz);
	snprintf(dst, size,
		 "%-*.*s %-*.*s %-*.*s",
		 name_sz, name_sz,	ph->name,
		 addr_sz, addr_sz,	ph->addrstr,
		 desc_sz, desc_sz,	ph->descr ? ph->descr : "");
	return 0;
}

static int
show_if_footer(dst, size, unused)
	char *dst;
	int size;
	const void *unused;
{
	/* sanity check */
	if (!dst || size < 1 || unused)
		return 0;

	(void)strncpy(dst, hostname, size);
	dst[size-1] = '\0';
	return 0;
}

SELECTOR *
show_if_selector()
{
	static SELECTOR *sp = 0;
	if (!sp && (sp = selector_init()) != 0) {
		sp->get_header = show_if_header;
		sp->get_line = show_if_line;
		sp->get_footer = show_if_footer;
	}
	return sp;
}

int
show_if_search(ph, str)
	PCAP_HANDLER *ph;
	const char *str;
{
	int idx;

	/* sanity check */
	if (!str || *str == '\0')
		return -1;

	for (idx = 0; ph; ph = ph->next, idx++) {
		if (strstr(ph->name, str))
			return idx;
		if (strstr(ph->addrstr, str))
			return idx;
	}
	return -1;
}

SELECTOR *
show_if_list(ph)
	PCAP_HANDLER *ph;
{
	SELECTOR *sp;

	/* sanity check */
	if (!ph || (sp = show_if_selector()) == 0)
		return 0;

	sp->header = 0; /* unused */
	sp->footer = 0; /* unused */

	sp->list = ph;
	sp->items = 0;
	for (; ph; ph = ph->next)
		sp->items++;

	return sp;
}