-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhub.c
More file actions
189 lines (155 loc) · 3.64 KB
/
hub.c
File metadata and controls
189 lines (155 loc) · 3.64 KB
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
/*
This file (was) part of GNUnet.
Copyright (C) 2018 Christian Grothoff
GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GNUnet 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file hub.c
* @brief Stupidly forwards network traffic between interfaces
* @author Christian Grothoff
*/
#include "glab.h"
/**
* gcc 4.x-ism to pack structures (to be used before structs);
* Using this still causes structs to be unaligned on the stack on Sparc
* (See #670578 from Debian).
*/
_Pragma("pack(push)") _Pragma("pack(1)")
struct EthernetHeader
{
struct MacAddress dst;
struct MacAddress src;
uint16_t tag;
};
_Pragma("pack(pop)")
/**
* Per-interface context.
*/
struct Interface
{
/**
* MAC of interface.
*/
struct MacAddress mac;
/**
* Number of this interface.
*/
uint16_t ifc_num;
};
/**
* Number of available contexts.
*/
static unsigned int num_ifc;
/**
* All the contexts.
*/
static struct Interface *gifc;
#include "print.c"
/**
* Forward @a frame to interface @a dst.
*
* @param dst target interface to send the frame out on
* @param frame the frame to forward
* @param frame_size number of bytes in @a frame
*/
static void
forward_to (struct Interface *dst,
const void *frame,
size_t frame_size)
{
char iob[frame_size + sizeof (struct GLAB_MessageHeader)];
struct GLAB_MessageHeader hdr;
hdr.size = htons (sizeof (iob));
hdr.type = htons (dst->ifc_num);
memcpy (iob,
&hdr,
sizeof (hdr));
memcpy (&iob[sizeof (hdr)],
frame,
frame_size);
write_all (STDOUT_FILENO,
iob,
sizeof (iob));
}
static void
fwd_frame (struct Interface *src_ifc,
const void *frame,
size_t frame_size)
{ // Don't forward frame to the source interface
for (int i = 0; i < num_ifc; ++i) {
if (gifc[i].ifc_num != src_ifc->ifc_num){
forward_to(&gifc[i], frame, frame_size);
}
}
}
/**
* Process frame received from @a interface.
*
* @param interface number of the interface on which we received @a frame
* @param frame the frame
* @param frame_size number of bytes in @a frame
*/
static void
handle_frame (uint16_t interface,
const void *frame,
size_t frame_size)
{
if (interface > num_ifc)
abort ();
fwd_frame (&gifc[interface - 1],
frame,
frame_size);
}
/**
* Handle control message @a cmd.
*
* @param cmd text the user entered
* @param cmd_len length of @a cmd
*/
static void
handle_control (char *cmd,
size_t cmd_len)
{
cmd[cmd_len - 1] = '\0';
print ("Received command `%s' (ignored)\n",
cmd);
}
/**
* Handle MAC information @a mac
*
* @param ifc_num number of the interface with @a mac
* @param mac the MAC address at @a ifc_num
*/
static void
handle_mac (uint16_t ifc_num,
const struct MacAddress *mac)
{
if (ifc_num > num_ifc)
abort ();
gifc[ifc_num - 1].mac = *mac;
}
#include "loop.c"
int
main (int argc,
char **argv)
{
struct Interface ifc[argc - 1];
memset (ifc,
0,
sizeof (ifc));
num_ifc = argc - 1;
gifc = ifc;
for (unsigned int i=1;i<argc;i++)
ifc[i-1].ifc_num = i;
loop ();
return 0;
}