mmlib/mmat: replace some variables by literal constants
[mmondor.git] / mmsoftware / tap-bridge / tap-bridge.h
CommitLineData
7d1ff53b 1/* $Id: tap-bridge.h,v 1.2 2007/12/20 01:32:19 mmondor Exp $ */
12f87502
MM
2
3/*
4 * Copyright (C) 2007, Matthew Mondor
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Matthew Mondor.
18 * 4. The name of Matthew Mondor may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 * 5. Redistribution of source code may not be released under the terms of
22 * any GNU Public License derivate.
23 *
24 * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
7d1ff53b
MM
36#ifndef TAP_BRIDGE_H
37#define TAP_BRIDGE_H
38
39
12f87502
MM
40#include <time.h>
41
42#include <mmlist.h>
43#include <mmpool.h>
44
45
46#define MTU 1500
47/*
48 * tap(4) provides us with the ethernet header but not the checksum
49 * Bytes 0-5 dstmac, 6-11 srcmac, 12-13 ethertype, and we add our header
50 * 14-15 32-bit padding bytes.
51 */
52#define FRAMESIZE (MTU + 14 + 2)
53#define TIMER_HZ 10
54
55
56typedef uint64_t macaddr_t;
57typedef struct tap tap_t;
58typedef struct frame frame_t;
59typedef struct fnode fnode_t;
60typedef struct module module_t;
61typedef struct modnode modnode_t;
62
63/*
64 * Represents a tap(4) interface.
65 */
66struct tap {
67 char *name;
68 int fd;
69 macaddr_t mac;
70 list_t sendq, schedq;
71};
72
73/*
74 * Holds an ethernet frame. A reference count allows multiple fnode_t objects
75 * to represent this frame_t object.
76 */
77struct frame {
78 pnode_t node;
79 macaddr_t origin, destination;
80 struct timeval received;
81 size_t size;
82 uint16_t type;
83 uint8_t *data;
84 int refcount;
85};
86
87/*
88 * Passed to module receive callback functions so that each may have its own
89 * copy it can queue or discard as it wishes.
90 */
91struct fnode {
92 pnode_t node;
93 frame_t *frame;
94 struct timeval scheduled;
95};
96
97/*
98 * Every filter module must provide this structure, named "module".
99 * <init> must hold the module's initialization code and will be called at
100 * module loading time.
101 * <recv> will we passed an fnode_t representing a frame_t for every received
102 * frame from the tap(4) interface. The module may defer any
103 * processing by destroying the node and returning true. It may drop
104 * the frame_t by destroying the node and returning false. It may
105 * use frame_send() to send it on a tap_t. It also may queue its
106 * fnode_t as wanted via its pnode_t's node_t. The underlaying frame_t
107 * will only ever be destroyed when all fnode_t representing it are
108 * destroyed.
109 * <tick> is optional and may be NULL. If a function is provided it will get
110 * called every tap-bridge scheduling round (at a frequency of
111 * TIMER_HZ).
112 * <exit> invoked when the module is unloaded. Should free any resources it
113 * holds.
114 * XXX I should provide a means for module to be able to keep persistent
115 * resources. For instance, the logging module may want to keep its opened
116 * fifo file open as it doesn't want the reader to receive an EOF.
117 * However, the only safe way to do this using a decent API might be to
118 * provide named keys looked up in a hash table for modules to use.
119 * If every module has a unique persistent identifier this could also be
120 * done... we could use the module name as a key for instance, and provide a
121 * void ** to the init function which it may use however it wishes.
122 * If doing this, we perhaps also want to provide an application global shared
123 * pointer to modules as well which might be good for particular applications.
124 * A general purpose named keys system would allow them to do both...
125 */
126struct module {
127 bool (*init)(tap_t *, tap_t *);
128 bool (*recv_int)(tap_t *, fnode_t *);
129 bool (*recv_ext)(tap_t *, fnode_t *);
130 void (*tick)(struct timeval *);
131 void (*exit)(void);
132};
133
134struct modnode {
135 pnode_t node;
136 void *handle;
137 module_t *module;
138};
139
140extern frame_t *frame_new(void);
141extern fnode_t *fnode_new(frame_t *);
142extern void fnode_destroy(fnode_t *);
143extern size_t macaddr_string(char *, size_t, macaddr_t *);
144extern void frame_send(tap_t *, fnode_t *);
145extern void frame_schedule(tap_t *, fnode_t *,
146 const struct timeval *);
147extern void *time_setcallback(void (*)(const struct timeval *));
148extern void *recv_setcallback(tap_t *,
149 void (*)(tap_t *, frame_t *));
150extern void **key_create(const char *);
151extern void **key_lookup(const char *);
152extern void key_destroy(const char *);
153extern void warning(int, const char *, ...);
7d1ff53b
MM
154
155
156#endif