summaryrefslogtreecommitdiff
path: root/pppd/pcap_pcc.c
blob: 770c338eeee7e55f288727aa0300c5206ae720c5 (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
#include <pcap.h>
#include <pcap-bpf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "pppd.h"

int pcap_pre_compiled (char * fname, struct bpf_program *p)
{
    char buf[128];
    int line = 0, size = 0, index=0, ret=1;
    FILE *f = fopen (fname, "r");
    if (!f)
    {
       option_error("error opening precompiled active-filter '%s': %s",
                    fname, strerror (errno));
       return 0;
    }
    while (fgets (buf, 127, f))
    {
       line++;
       if (*buf == '#')
           continue;
       if (size)
       {
           /*
             struct bpf_insn {
             u_short   code;
             u_char    jt;
             u_char    jf;
             bpf_int32 k;
             }
           */
           struct bpf_insn * insn = & p->bf_insns[index];
           unsigned code, jt, jf, k;
           if (sscanf (buf, "%u %u %u %u", &code, &jt, &jf, &k) != 4)
           {
               goto err;
           }
           insn->code = code;
           insn->jt = jt;
           insn->jf = jf;
           insn->k  = k;
           index++;
       }
       else
       {
           if (sscanf (buf, "%u", &size) != 1)
           {
               goto err;
           }
           p->bf_len = size;
           p->bf_insns = (struct bpf_insn *) 
               malloc (size * sizeof (struct bpf_insn));
       }
    } 
    if (size != index)
    {
       option_error("error in precompiled active-filter,"
                    " expected %d expressions, got %dn",
                    size, index);
       ret = 0;
    }
    fclose(f);
    return ret;

err:
  option_error("error in precompiled active-filter"
              " expression line %s:%d (wrong size)\n", 
              fname, line);
  fclose (f);
  return 0;
}