/[dynamips]/upstream/dynamips-0.2.6-RC3/net_io_filter.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/dynamips-0.2.6-RC3/net_io_filter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (hide annotations)
Sat Oct 6 16:05:34 2007 UTC (14 years, 8 months ago) by dpavlin
Original Path: upstream/dynamips-0.2.6-RC2/net_io_filter.c
File MIME type: text/plain
File size: 4302 byte(s)
dynamips-0.2.6-RC2

1 dpavlin 1 /*
2     * Cisco 7200 (Predator) simulation platform.
3     * Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr)
4     *
5     * NetIO Filtering.
6     */
7    
8     #include <stdio.h>
9     #include <stdlib.h>
10     #include <string.h>
11     #include <stdarg.h>
12     #include <unistd.h>
13     #include <errno.h>
14     #include <signal.h>
15     #include <fcntl.h>
16     #include <ctype.h>
17     #include <time.h>
18     #include <sys/time.h>
19     #include <sys/stat.h>
20     #include <sys/types.h>
21     #include <sys/ioctl.h>
22     #include <sys/socket.h>
23     #include <sys/un.h>
24     #include <sys/wait.h>
25     #include <netinet/in.h>
26     #include <arpa/inet.h>
27     #include <netdb.h>
28     #include <pthread.h>
29    
30     #include "registry.h"
31     #include "net.h"
32     #include "net_io.h"
33     #include "net_io_filter.h"
34    
35     /* Filter list */
36     static netio_pktfilter_t *pf_list = NULL;
37    
38     /* Find a filter */
39     netio_pktfilter_t *netio_filter_find(char *name)
40     {
41     netio_pktfilter_t *pf;
42    
43     for(pf=pf_list;pf;pf=pf->next)
44     if (!strcmp(pf->name,name))
45     return pf;
46    
47     return NULL;
48     }
49    
50     /* Add a new filter */
51     int netio_filter_add(netio_pktfilter_t *pf)
52     {
53     if (netio_filter_find(pf->name) != NULL)
54     return(-1);
55    
56     pf->next = pf_list;
57     pf_list = pf;
58     return(0);
59     }
60    
61     /* Bind a filter to a NIO */
62     int netio_filter_bind(netio_desc_t *nio,int direction,char *pf_name)
63     {
64     netio_pktfilter_t *pf;
65    
66     if (!(pf = netio_filter_find(pf_name)))
67     return(-1);
68    
69     if (direction == NETIO_FILTER_DIR_RX) {
70     nio->rx_filter_data = NULL;
71     nio->rx_filter = pf;
72     } else {
73     nio->tx_filter_data = NULL;
74     nio->tx_filter = pf;
75     }
76    
77     return(0);
78     }
79    
80     /* Unbind a filter from a NIO */
81     int netio_filter_unbind(netio_desc_t *nio,int direction)
82     {
83     netio_pktfilter_t *pf;
84     void **opt;
85    
86     if (direction == NETIO_FILTER_DIR_RX) {
87     opt = &nio->rx_filter_data;
88     pf = nio->rx_filter;
89     } else {
90     opt = &nio->tx_filter_data;
91     pf = nio->tx_filter;
92     }
93    
94     if (!pf)
95     return(-1);
96    
97     pf->free(nio,opt);
98     return(0);
99     }
100    
101     /* Setup a filter */
102     int netio_filter_setup(netio_desc_t *nio,int direction,int argc,char *argv[])
103     {
104     netio_pktfilter_t *pf;
105     void **opt;
106    
107     if (direction == NETIO_FILTER_DIR_RX) {
108     opt = &nio->rx_filter_data;
109     pf = nio->rx_filter;
110     } else {
111     opt = &nio->tx_filter_data;
112     pf = nio->tx_filter;
113     }
114    
115     if (!pf)
116     return(-1);
117    
118     return(pf->setup(nio,opt,argc,argv));
119     }
120    
121     /* ======================================================================== */
122     /* Frequency Dropping ("freq_drop"). */
123     /* ======================================================================== */
124    
125     struct pf_freqdrop_data {
126     int frequency;
127     int current;
128     };
129    
130     /* Setup filter ressources */
131     static int pf_freqdrop_setup(netio_desc_t *nio,void **opt,
132     int argc,char *argv[])
133     {
134     struct pf_freqdrop_data *data = *opt;
135    
136     if (argc != 1)
137     return(-1);
138    
139     if (!data) {
140     if (!(data = malloc(sizeof(*data))))
141     return(-1);
142    
143     *opt = data;
144     }
145    
146     data->current = 0;
147     data->frequency = atoi(argv[0]);
148     return(0);
149     }
150    
151     /* Free ressources used by filter */
152     static void pf_freqdrop_free(netio_desc_t *nio,void **opt)
153     {
154     if (*opt)
155     free(*opt);
156    
157     *opt = NULL;
158     }
159    
160     /* Packet handler: drop 1 out of n packets */
161     static int pf_freqdrop_pkt_handler(netio_desc_t *nio,void *pkt,size_t len,
162     void *opt)
163     {
164     struct pf_freqdrop_data *data = opt;
165    
166     if (data != NULL) {
167     switch(data->frequency) {
168     case -1:
169     return(NETIO_FILTER_ACTION_DROP);
170     case 0:
171     return(NETIO_FILTER_ACTION_PASS);
172     default:
173     data->current++;
174    
175     if (data->current == data->frequency) {
176     data->current = 0;
177     return(NETIO_FILTER_ACTION_DROP);
178     }
179     }
180     }
181    
182     return(NETIO_FILTER_ACTION_PASS);
183     }
184    
185     /* Packet dropping at 1/n frequency */
186     static netio_pktfilter_t pf_freqdrop_def = {
187     "freq_drop",
188     pf_freqdrop_setup,
189     pf_freqdrop_free,
190     pf_freqdrop_pkt_handler,
191     NULL,
192     };
193    
194     /* ======================================================================== */
195     /* Initialization of packet filters. */
196     /* ======================================================================== */
197    
198     void netio_filter_load_all(void)
199     {
200     netio_filter_add(&pf_freqdrop_def);
201     }

  ViewVC Help
Powered by ViewVC 1.1.26