summaryrefslogtreecommitdiff
path: root/src/pmdas/cisco/telnet.c
blob: bcee1db6381b54b5bf32fbd25a311d3abd653ea0 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * Copyright (c) 2012 Red Hat.
 * Copyright (c) 1995-2004 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <syslog.h>
#include "./cisco.h"

extern int	port;

int
conn_cisco(cisco_t * cp)
{
    __pmFdSet		wfds;
    __pmSockAddr	*myaddr;
    void		*enumIx;
    int			flags = 0;
    int			fd;
    int			ret;

    fd = -1;
    enumIx = NULL;
    for (myaddr = __pmHostEntGetSockAddr(cp->hostinfo, &enumIx);
	 myaddr != NULL;
	 myaddr = __pmHostEntGetSockAddr(cp->hostinfo, &enumIx)) {
	/* Create a socket */
	if (__pmSockAddrIsInet(myaddr))
	    fd = __pmCreateSocket();
	else if (__pmSockAddrIsIPv6(myaddr))
	    fd = __pmCreateIPv6Socket();
	else
	    continue;
	if (fd < 0) {
	    __pmSockAddrFree(myaddr);
	    continue; /* Try the next address */
	}

	/* Attempt to connect */
	flags = __pmConnectTo(fd, myaddr, cp->port);
	__pmSockAddrFree(myaddr);

	if (flags < 0) {
	    /*
	     * Mark failure in case we fall out the end of the loop
	     * and try next address. fd has been closed in __pmConnectTo().
	     */
	    setoserror(ECONNREFUSED);
	    fd = -1;
	    continue;
	}

	/* FNDELAY and we're in progress - wait on select */
	__pmFD_ZERO(&wfds);
	__pmFD_SET(fd, &wfds);
	ret = __pmSelectWrite(fd+1, &wfds, NULL);

	/* Was the connection successful? */
	if (ret == 0)
	    setoserror(ETIMEDOUT);
	else if (ret > 0) {
	    ret = __pmConnectCheckError(fd);
	    if (ret == 0)
		break;
	    setoserror(ret);
	}
	
	/* Unsuccessful connection. */
	__pmCloseSocket(fd);
	fd = -1;
    } /* loop over addresses */

    if (fd == -1) {
	fprintf(stderr, "conn_cisco(%s): connect: %s\n",
		cp->host, netstrerror());
	return -1;
    }

    fd = __pmConnectRestoreFlags(fd, flags);
    if (fd < 0) {
	fprintf(stderr, "conn_cisco(%s): setsockopt: %s\n",
		cp->host, netstrerror());
	return -1;
    }

    return fd;
}

static void
skip2eol(FILE *f)
{
    int		c;

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2)
	fprintf(stderr, "skip2eol:");
#endif

    while ((c = fgetc(f)) != EOF) {
	if (c == '\n')
	    break;
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL2)
	    fprintf(stderr, "%c", c);
#endif
    }
#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2)
	fputc('\n', stderr);
#endif
}

char *
mygetwd(FILE *f, char *prompt)
{
    char	*p;
    int		c;
    static char	buf[1024];
    int		len_prompt = strlen(prompt);
    int		found_prompt = 0;

    p = buf;

    while ((c = fgetc(f)) != EOF) {
	if (c == '\r' || c == '\n' || c == ' ' || c == '\t') {
	    if (p == buf)
		continue;
	    break;
	}
        *p++ = c;
	if (p-buf >= len_prompt && strncmp(&p[-len_prompt], prompt, len_prompt) == 0) {
	    found_prompt = 1;
	    break;
	}
    }
    *p = '\0';

    if (feof(f)) {
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL2)
	    fprintf(stderr, "mygetwd: EOF fd=%d\n", fileno(f));
#endif
	return NULL;
    }

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2)
	fprintf(stderr, "mygetwd: fd=%d wd=\"%s\"%s\n", fileno(f), buf, found_prompt ? " [prompt]" : "");
#endif

    return buf;
}

/*
 * The CISCO "show interface" command output is parsed.
 *
 * See the file Samples for examples.
 *
 * The parser is a Finite State Automaton (FSA) that follows these
 * rules:
 *
 * SHOW_INT style ... uses "show int <interface>" command
 * state	token			next state
 * NOISE	<interface name>	IN_REPORT
 * IN_REPORT	Description:		skip rest of line, IN_REPORT
 * IN_REPORT	<prompt>		DONE
 * IN_REPORT	minute			RATE
 * IN_REPORT	second			RATE
 * IN_REPORT	input,			BYTES_IN
 * IN_REPORT	output,			BYTES_OUT
 * IN_REPORT	BW			BW
 * RATE		input			skip next token, RATE_IN
 * RATE		output			skip next token, RATE_OUT
 * RATE_IN	<number> (rate_in)	IN_REPORT
 * RATE_OUT	<number> (rate_out)	IN_REPORT
 * BYTES_IN	<number> (bytes_in)	IN_REPORT
 * BYTES_OUT	<number> (bytes_out)	IN_REPORT
 * BW		<number> (bandwidth)	IN_REPORT
 *
 * SHOW_FRAME style ... uses "show frame pvc int <interface>" command
 * state		token			next state
 * NOISE		<interface name>	IN_REPORT
 * IN_REPORT		Description:		skip rest of line, IN_REPORT
 * IN_REPORT		<prompt>		DONE
 * IN_REPORT		1st bytes		BYTES_IN
 * IN_REPORT		2nd bytes		BYTES_OUT
 * IN_REPORT		3rd bytes		BYTES_OUT_BCAST
 * BYTES_IN		<number> (bytes_in)	IN_REPORT
 * BYTES_OUT		<number> (bytes_out)	IN_REPORT
 * BYTES_OUT_BCAST	<number> (bytes_out_bcast)	IN_REPORT
 *
 * Note lines are terminated with \r
 */

#define DONE -1
#define NOISE 0
#define IN_REPORT 1
#define RATE 2
#define RATE_IN 3
#define RATE_OUT 4
#define BYTES_IN 5
#define BYTES_OUT 6
#define BW 7
#define BYTES_OUT_BCAST 8

#ifdef PCP_DEBUG
static char *statestr[] = {
    "done", "noise", "in_report",
    "rate", "rate_in", "rate_out", "bytes_in", "bytes_out", "bw",
    "bytes_out_bcast"
};
#endif

int
dousername(cisco_t *cp, char **pw_prompt)
{
    char	*w;
    int		len, done = 0;
    int		len_prompt = strlen(cp->prompt);

    for ( ; ; ) {
	w = mygetwd(cp->fin, cp->prompt);
	if (w == NULL)
	    break;
	if (strlen(w) >= len_prompt && strncmp(&w[strlen(w)-len_prompt], cp->prompt, len_prompt) == 0)
	    break;
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL0)
	    fprintf(stderr, "Username:? got - %s\n", w);
#endif
	if (strcmp(w, USERPROMPT) == 0) {
#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL1) {
		fprintf(stderr, "Send username: %s\n", cp->username);
	    }
#endif
	    fprintf(cp->fout, "%s\n", cp->username);
	    fflush(cp->fout);
	    for ( ; ; ) {
		w = mygetwd(cp->fin, cp->prompt);
		if (w == NULL || strcmp(w, USERPROMPT) == 0)
		    /* closed connection or Username re-prompt */
		    break;
		len = strlen(w);
		if ((len >= len_prompt && strncmp(&w[len-len_prompt], cp->prompt, len_prompt) == 0) ||
		    w[len-1] == ':') {
		    /* command prompt or passwd */
		    if (w[len-1] == ':')
			*pw_prompt = w;
		    done = 1;
		    break;
		}
	    }
	    break;
	}
    }

    if (done == 0) {
	fprintf(stderr, "Error: Cisco username negotiation failed for \"%s\"\n",
		    cp->host);
	fprintf(stderr,
"To check that a username is required, enter the following command:\n"
"   $ telnet %s\n"
"If the prompt \"%s\" does not appear, no username is required.\n"
"Otherwise, enter the username \"%s\" to check that this\n"
"is correct.\n",
cp->host, USERPROMPT, cp->username);
    }

    return done;
}

int
dopasswd(cisco_t *cp, char *pw_prompt)
{
    char	*w;
    int		done = 0;
    int		len_prompt = strlen(cp->prompt);

    for ( ; ; ) {
	if (pw_prompt)	/* dousername may have read passwd prompt */
	    w = pw_prompt;
	else
	    w = mygetwd(cp->fin, cp->prompt);
	pw_prompt = NULL;
	if (w == NULL)
	    break;
	if (strlen(w) >= len_prompt && strncmp(&w[strlen(w)-len_prompt], cp->prompt, len_prompt) == 0)
	    break;
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL0)
	    fprintf(stderr, "Password:? got - %s\n", w);
#endif
	if (strcmp(w, PWPROMPT) == 0) {
#ifdef PCP_DEBUG
		if (pmDebug & DBG_TRACE_APPL1) {
		    fprintf(stderr, "Send passwd: %s\n", cp->passwd);
		}
#endif
	    fprintf(cp->fout, "%s\n", cp->passwd);
	    fflush(cp->fout);
	    for ( ; ; ) {
		w = mygetwd(cp->fin, cp->prompt);
		if (w == NULL || strcmp(w, PWPROMPT) == 0)
		    /* closed connection or user-level password re-prompt */
		    break;
		if (strlen(w) >= len_prompt && strncmp(&w[strlen(w)-len_prompt], cp->prompt, len_prompt) == 0) {
		    /* command prompt */
		    done = 1;
		    break;
		}
	    }
	    break;
	}
    }

    if (done == 0) {
	fprintf(stderr, "Error: Cisco user-level password negotiation failed for \"%s\"\n",
		    cp->host);
	fprintf(stderr,
"To check that a user-level password is required, enter the following command:\n"
"   $ telnet %s\n"
"If the prompt \"%s\" does not appear, no user-level password is required.\n"
"Otherwise, enter the user-level password \"%s\" to check that this\n"
"is correct.\n",
cp->host, PWPROMPT, cp->passwd);
    }

    return done;
}

static int	timeout;

void
onalarm(int dummy)
{
    timeout = 1;
#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	fprintf(stderr, "Alarm timeout!\n");
    }
#endif

}

static int
get_fr_bw(cisco_t *cp, char *interface)
{
    int		state = NOISE;
    int		bandwidth = -1;
    char	*w;
    int		len_prompt = strlen(cp->prompt);

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL1) {
	fprintf(stderr, "Send: s%s\n", interface);
    }
#endif
    fprintf(cp->fout, "show int s%s\n", interface);
    fflush(cp->fout);
#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2)
	fprintf(stderr, "BW Parse:");
#endif
    while (state != DONE) {
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL2)
	    fprintf(stderr, "[%s] ", statestr[state+1]);
#endif
	w = mygetwd(cp->fin, cp->prompt);
	if (w == NULL || timeout) {
	    /*
	     * End of File (telenet timeout?)
	     */
	    alarm(0);
	    return -1;
	}
	switch (state) {

	    case NOISE:
		if (strncmp(w, "Serial", 6) == 0 && strcmp(&w[6], interface) == 0)
		    state = IN_REPORT;
		break;
	    
	    case IN_REPORT:
		if (strcmp(w, "Description:") == 0)
		    skip2eol(cp->fin);
		else if (strlen(w) >= len_prompt && strncmp(&w[strlen(w)-len_prompt], cp->prompt, len_prompt) == 0)
		    state = DONE;
		else if (strcmp(w, "BW") == 0)
		    state = BW;
		break;

	    case BW:
		sscanf(w, "%d", &bandwidth);
		bandwidth *= 1000;		/* Kbit -> bytes/sec */
		bandwidth /= 8;
		state = IN_REPORT;
		break;

	}
    }
    alarm(0);
#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	fprintf(stderr, "Extracted bandwidth: %d bytes/sec\n", bandwidth);
    }
#endif
    return bandwidth;
}

#define SHOW_INT	1
#define SHOW_FRAME	2

int
grab_cisco(intf_t *ip)
{
    int		style;
    int		next_state;
    int		state = NOISE;
    int		skip = 0;
    int		i;
    int		namelen;
    char	*pw_prompt = NULL;
    char	*w;
    int		fd;
    int		fd2;
    int		nval = 0;
    cisco_t	*cp = ip->cp;
    intf_t	tmp;
    int		len_prompt = strlen(cp->prompt);

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	fprintf(stderr, "grab_cisco(%s:%s):\n", cp->host, ip->interface);
    }
#endif

    tmp.bandwidth = tmp.rate_in = tmp.rate_out = -1;
    tmp.bytes_in = tmp.bytes_out = tmp.bytes_out_bcast = -1;

    if (cp->fin == NULL) {
	fd = conn_cisco(cp);
	if (fd < 0) {
#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL0)
		fprintf(stderr, "grab_cisco(%s:%s): connect failed: %s\n",
			cp->host, ip->interface, netstrerror());
#endif
	    return -1;
	}
	else {
	    cp->fin = fdopen (fd, "r");
	    if ((fd2 = dup(fd)) < 0) {
	    	perror("dup");
		exit(1);
	    }
	    cp->fout = fdopen (fd2, "w");
#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL0) {
		fprintf(stderr, "grab_cisco(%s:%s): connected fin=%d fout=%d",
		    cp->host, ip->interface, fileno(cp->fin), fileno(cp->fout));
		if (cp->username != NULL)
		    fprintf(stderr, " username=%s", cp->username);
		else
		    fprintf(stderr, " NO username");
		if (cp->passwd != NULL)
		    fprintf(stderr, " passwd=%s", cp->passwd);
		else
		    fprintf(stderr, " NO passwd");
		fputc('\n', stderr);
	    }
#endif

	    if (cp->username != NULL) {
		/*
		 * Username stuff ...
		 */
		if (dousername(cp, &pw_prompt) == 0) {
		    fclose(cp->fin);
		    fclose(cp->fout);
		    cp->fin = cp->fout = NULL;
		    return -1;
		}
	    }
	    if (cp->passwd != NULL) {
		/*
		 * User-level password stuff ...
		 */
		if (dopasswd(cp, pw_prompt) == 0) {
		    fclose(cp->fin);
		    fclose(cp->fout);
		    cp->fin = cp->fout = NULL;
		    return -1;
		}
	    }
#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL1) {
		fprintf(stderr, "Send: \n");
	    }
#endif
	    fprintf(cp->fout, "\n");
	    fflush(cp->fout);
	}
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL1) {
	    fprintf(stderr, "Send: terminal length 0\n");
	}
#endif
	fprintf(cp->fout, "terminal length 0\n");
	fflush(cp->fout);
    }

    timeout = 0;
    signal(SIGALRM, onalarm);
    /*
     * Choice of timeout here is somewhat arbitrary ... for a long
     * time this was 5 (seconds), but then testing with an entry
     * level Model 800 ADSL router revealed that up to 20 seconds
     * was required to generate the expected output.
     */
    alarm(20);

    style = SHOW_INT;			/* default Cisco command */
    if (ip->interface[0] == 's' && strchr(ip->interface, '.') != NULL) {
	/*
	 * Frame-relay PVC on subinterface for s2/3.7 style interface name
	 */
	style = SHOW_FRAME;
	if (ip->bandwidth == -2) {
	    /*
	     * one-trip initialzation ... need show int s2/3.7 to
	     * get bandwidth
	     */
	    ip->bandwidth = get_fr_bw(cp, &ip->interface[1]);
	}
	tmp.bandwidth = ip->bandwidth;
	if (tmp.bandwidth != -1)
	    nval++;
    }
    if (style == SHOW_FRAME) {
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL1) {
	    fprintf(stderr, "Send: show frame pvc int s%s\n", &ip->interface[1]);
	}
#endif
	fprintf(cp->fout, "show frame pvc int s%s\n", &ip->interface[1]);
	next_state = BYTES_IN;
    }
    else {
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL1) {
	    fprintf(stderr, "Send: show int %s\n", ip->interface);
	}
#endif
	fprintf(cp->fout, "show int %s\n", ip->interface);
    }
    fflush(cp->fout);
    state = NOISE;
#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2) {
	fprintf(stderr, "Parse:");
	fflush(stderr);
    }
#endif
    while (state != DONE) {
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL2) {
	    fprintf(stderr, "[%s] ", statestr[state+1]);
	    fflush(stderr);
	}
#endif
	w = mygetwd(cp->fin, cp->prompt);
	if (w == NULL || timeout) {
	    /*
	     * End of File (telenet timeout?)
	     * ... mark as closed, and try again at next request
	     */
#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL0)
		fprintf(stderr, "grab_cisco(%s:%s): forced disconnect fin=%d\n",
		    cp->host, ip->interface, fileno(cp->fin));
#endif
	    fclose(cp->fin);
	    fclose(cp->fout);
	    cp->fin = cp->fout = NULL;
	    alarm(0);
	    return -1;
	}
	switch (state) {

	    case NOISE:
		for (i = 0; i < num_intf_tab; i++) {
		    namelen = strlen(intf_tab[i].name);
		    if (strncmp(w, intf_tab[i].name, namelen) == 0) {
			state = IN_REPORT;
			break;
		    }
		}
		break;
	    
	    case IN_REPORT:
		if (strcmp(w, "Description:") == 0)
		    skip2eol(cp->fin);
		if (strlen(w) >= len_prompt && strncmp(&w[strlen(w)-len_prompt], cp->prompt, len_prompt) == 0)
		    state = DONE;
		else if (style == SHOW_INT) {
		    if (strcmp(w, "minute") == 0 || strcmp(w, "second") == 0)
			state = RATE;
		    else if (strcmp(w, "input,") == 0)
			state = BYTES_IN;
		    else if (strcmp(w, "output,") == 0)
			state = BYTES_OUT;
		    else if (strcmp(w, "BW") == 0)
			state = BW;
		}
		else if (style == SHOW_FRAME) {
		    if (strcmp(w, "bytes") == 0) {
			if (next_state == BYTES_IN) {
			    state = BYTES_IN;
			    next_state = BYTES_OUT;
			}
			else if (next_state == BYTES_OUT) {
			    state = BYTES_OUT;
			    next_state = BYTES_OUT_BCAST;
			}
			else if (next_state == BYTES_OUT_BCAST) {
			    state = BYTES_OUT_BCAST;
			    next_state = IN_REPORT;
			}
			else
			    state = next_state;
		    }
		}
		break;

	    case RATE:
		if (strcmp(w, "input") == 0) {
		    skip = 1;
		    state = RATE_IN;
		}
		else if (strcmp(w, "output") == 0) {
		    skip = 1;
		    state = RATE_OUT;
		}
		break;

	    case RATE_IN:
		if (skip-- == 0) {
		    tmp.rate_in = atol(w) / 8;
		    nval++;
		    state = IN_REPORT;
		}
		break;

	    case RATE_OUT:
		if (skip-- == 0) {
		    tmp.rate_out = atol(w) / 8;
		    nval++;
		    state = IN_REPORT;
		}
		break;

	    case BYTES_IN:
		tmp.bytes_in = strtoull(w, NULL, 10);
		nval++;
		state = IN_REPORT;
		break;

	    case BYTES_OUT:
		tmp.bytes_out = strtoull(w, NULL, 10);
		nval++;
		state = IN_REPORT;
		break;

	    case BYTES_OUT_BCAST:
		tmp.bytes_out_bcast = strtoull(w, NULL, 10);
		nval++;
		state = IN_REPORT;
		break;

	    case BW:
		sscanf(w, "%d", &tmp.bandwidth);
		tmp.bandwidth *= 1000;		/* Kbit -> bytes/sec */
		tmp.bandwidth /= 8;
		nval++;
		state = IN_REPORT;
		break;

	}
    }
    alarm(0);
#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	fprintf(stderr, "Extracted %d values ...\n", nval);
	if (tmp.bandwidth != 0xffffffff)
	    fprintf(stderr, "bandwidth: %d bytes/sec\n", tmp.bandwidth);
	else
	    fprintf(stderr, "bandwidth: ? bytes/sec\n");
	fprintf(stderr, "recent rate (bytes/sec):");
	if (tmp.rate_in != 0xffffffff)
	    fprintf(stderr, " %d in", tmp.rate_in);
	else
	    fprintf(stderr, " ? in");
	if (tmp.rate_out != 0xffffffff)
	    fprintf(stderr, " %d out", tmp.rate_out);
	else
	    fprintf(stderr, " ? out");
	fprintf(stderr, "\ntotal bytes:");
	if (tmp.bytes_in != 0xffffffffffffffffLL)
	    fprintf(stderr, " %llu in", (unsigned long long)tmp.bytes_in);
	else
	    fprintf(stderr, " ? in");
	if (tmp.bytes_out != 0xffffffffffffffffLL)
	    fprintf(stderr, " %llu out", (unsigned long long)tmp.bytes_out);
	else
	    fprintf(stderr, " ? out");
	if (tmp.bytes_out_bcast != 0xffffffffffffffffLL)
	    fprintf(stderr, " %llu out_bcast", (unsigned long long)tmp.bytes_out_bcast);
	else
	    fprintf(stderr, " ? out_bcast");
	fprintf(stderr, "\n\n");
    }
#endif

    /* pretend this is atomic */
    ip->bandwidth = tmp.bandwidth;
    ip->rate_in = tmp.rate_in;
    ip->rate_out = tmp.rate_out;
    ip->bytes_in = tmp.bytes_in;
    ip->bytes_out = tmp.bytes_out;
    ip->bytes_out_bcast = tmp.bytes_out_bcast;

    return nval;
}