netsed is implemented in this single file.
More...
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <getopt.h>
Go to the source code of this file.
|
void | short_usage_hints (const char *why) |
| Display an error message followed by short usage information. More...
|
|
void | usage_hints (const char *why) |
| Display an error message followed by usage information. More...
|
|
void | freetracker (struct tracker_s *conn) |
| Helper function to free a tracker_s item. csa will be freed if needed, sockets will be closed. More...
|
|
void | clean_socks (void) |
| Close all sockets to use before exit. More...
|
|
in_port_t | get_port (struct sockaddr *sa) |
| Extract the port information from a sockaddr for both IPv4 and IPv6. More...
|
|
void | set_port (struct sockaddr *sa, in_port_t port) |
| Set the port information in a sockaddr for both IPv4 and IPv6. More...
|
|
int | is_addr_any (struct sockaddr *sa) |
| Detect if address in the addr_any value for both IPv4 and IPv6. More...
|
|
void | error (const char *reason) |
| Display an error message and exit. More...
|
|
void | shrink_to_binary (struct rule_s *r) |
| Convert the % notation in rules to plain binary data. More...
|
|
void | parse_params (int argc, char *argv[]) |
| parse the command line parameters More...
|
|
void | bind_and_listen (int af, int tcp, const char *portstr) |
| Bind and optionally listen to a socket for netsed server port. More...
|
|
int | sed_the_buffer (int siz, int *live) |
| Applies the rules to global buffer buf. More...
|
|
void | b2server_sed (struct tracker_s *conn, ssize_t rd) |
| Send the content of global buffer b2 to the server as packet or datagram. More...
|
|
void | server2client_sed (struct tracker_s *conn) |
| Receive a packet or datagram from the server, 'sed' it, send it to the client. More...
|
|
void | client2server_sed (struct tracker_s *conn) |
| Receive a packet from the client, 'sed' it, send it to the server. More...
|
|
void | sig_int (int signo) |
| Handle SIGINT signal for clean exit. More...
|
|
int | main (int argc, char *argv[]) |
| This is main... More...
|
|
netsed is implemented in this single file.
- Architecture
- Netsed is implemented as a select socket dispatcher. First a main socket server is created (lsock), each connection to this socket create a context stored in the tracker_s structure and added to the connections list. Each connection has
- a connected socket (tracker_s::csock) returned by the accept() function for tcp, or
- a connection socket address (tracker_s::csa) filled by recvfrom() for udp.
- a dedicated forwarding socket (tracker_s::fsock) connected to the server.
All sockets are added to the select() call and managed by the dispatcher as follows:
- When packets are received from the client, the rules are applied by sed_the_buffer() and the packet is send to the server. This is the role of client2server_sed() function. It is only used for tcp.
- When packets are received from the server, the rules are applied by sed_the_buffer() and the packet is send to the corresponding client. This is the role of server2client_sed() function.
- For udp only, connection from client to netsed are not established so netsed need to lookup existing connections to find the corresponding established link, if any. The lookup is done by comparing tracker_s::csa. Once the connection is found or created, the rules are applied by sed_the_buffer() and the packet is send to the server. This is the role of b2server_sed() function.
- Note
- For tcp tracker_s::csa is NULL and for udp the tracker_s::csock is filled with lsock. This is done in order to share code and avoid discriminating between tcp or udp everywhere, sendto are done on tracker_s::csock with tracker_s::csa only and the actual value of those will reflect the needs.
-
I'm saying packets and connections, but for udp these are actually datagrams and pseudo-connections. The pseudo-connection is defined by the fact that the client uses the same address and port (same tracker_s::csa) with a life time defined by UDP_TIMEOUT to clean the connection list.
- Todo:
- Implements features listed in TODO file.
Definition in file netsed.c.
Disabled debug prints.
Definition at line 143 of file netsed.c.
#define ERR |
( |
|
x... | ) |
fprintf(stderr,x) |
printf to stderr
Definition at line 134 of file netsed.c.
max size for buffers
Definition at line 131 of file netsed.c.
Define to use getopt_long: GNU extension, should check _GNU_SOURCE.
Definition at line 123 of file netsed.c.
Timeout for udp 'connections' in seconds.
Definition at line 147 of file netsed.c.
Current version (recovered by Makefile for several release checks)
Definition at line 129 of file netsed.c.
Connection state.
Enumerator |
---|
UNREPLIED |
udp datagram received by netsed and send to server, no response yet.
|
ESTABLISHED |
tcp accepted connection or udp 'connection' with a response from server.
|
DISCONNECTED |
tcp or udp disconnected (detected by an error on read or send).
- Note
- all values after and including DISCONNECTED are considered as error and the connection will be discarded.
|
TIMEOUT |
udp timeout expired.
|
Definition at line 166 of file netsed.c.
void b2server_sed |
( |
struct tracker_s * |
conn, |
|
|
ssize_t |
rd |
|
) |
| |
Send the content of global buffer b2 to the server as packet or datagram.
- Parameters
-
conn | connection giving the sockets to use. |
rd | size of b2 content. |
Definition at line 669 of file netsed.c.
void bind_and_listen |
( |
int |
af, |
|
|
int |
tcp, |
|
|
const char * |
portstr |
|
) |
| |
Bind and optionally listen to a socket for netsed server port.
- Parameters
-
af | address family. |
tcp | 1 tcp, 0 udp. |
portstr | string representing the port to bind (will be resolved using getaddrinfo()). |
Definition at line 527 of file netsed.c.
void clean_socks |
( |
void |
| ) |
|
Close all sockets to use before exit.
Definition at line 302 of file netsed.c.
void client2server_sed |
( |
struct tracker_s * |
conn | ) |
|
Receive a packet from the client, 'sed' it, send it to the server.
- Parameters
-
conn | connection giving the sockets to use. |
Definition at line 650 of file netsed.c.
void error |
( |
const char * |
reason | ) |
|
Display an error message and exit.
Definition at line 364 of file netsed.c.
Helper function to free a tracker_s item. csa will be freed if needed, sockets will be closed.
- Parameters
-
Definition at line 289 of file netsed.c.
in_port_t get_port |
( |
struct sockaddr * |
sa | ) |
|
Extract the port information from a sockaddr for both IPv4 and IPv6.
- Parameters
-
sa | sockaddr to get port from |
Definition at line 322 of file netsed.c.
int is_addr_any |
( |
struct sockaddr * |
sa | ) |
|
Detect if address in the addr_any value for both IPv4 and IPv6.
- Parameters
-
- Returns
- true if sa in addr_any
Definition at line 351 of file netsed.c.
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
This is main...
Definition at line 689 of file netsed.c.
void parse_params |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
parse the command line parameters
- Parameters
-
argc | number of arguments |
argv | array of string parameters |
Definition at line 445 of file netsed.c.
int sed_the_buffer |
( |
int |
siz, |
|
|
int * |
live |
|
) |
| |
Applies the rules to global buffer buf.
- Parameters
-
siz | useful size of the data in buf. |
live | TTL state of current connection. |
Definition at line 583 of file netsed.c.
void server2client_sed |
( |
struct tracker_s * |
conn | ) |
|
Receive a packet or datagram from the server, 'sed' it, send it to the client.
- Parameters
-
conn | connection giving the sockets to use. |
Definition at line 623 of file netsed.c.
void set_port |
( |
struct sockaddr * |
sa, |
|
|
in_port_t |
port |
|
) |
| |
Set the port information in a sockaddr for both IPv4 and IPv6.
- Parameters
-
sa | sockaddr to update |
port | port value |
Definition at line 336 of file netsed.c.
void short_usage_hints |
( |
const char * |
why | ) |
|
Display an error message followed by short usage information.
- Parameters
-
Definition at line 238 of file netsed.c.
void shrink_to_binary |
( |
struct rule_s * |
r | ) |
|
Convert the % notation in rules to plain binary data.
- Parameters
-
Definition at line 376 of file netsed.c.
void sig_int |
( |
int |
signo | ) |
|
Handle SIGINT signal for clean exit.
Definition at line 682 of file netsed.c.
void usage_hints |
( |
const char * |
why | ) |
|
Display an error message followed by usage information.
- Parameters
-
Definition at line 248 of file netsed.c.
Buffer containing modified packet or datagram.
Definition at line 578 of file netsed.c.
Buffer for receiving a single packet or datagram.
Definition at line 576 of file netsed.c.
List of connections.
Definition at line 231 of file netsed.c.
Address family used for parameter resolution.
Definition at line 209 of file netsed.c.
char hex[] ="0123456789ABCDEF" |
Hex digit to parsing the % notation in rules.
Definition at line 372 of file netsed.c.
Listening socket.
Definition at line 204 of file netsed.c.
Store current time (just after select returned).
Definition at line 201 of file netsed.c.
Array of all rules.
Definition at line 225 of file netsed.c.
TTL part of the rule as a flat array to be able to copy it in tracker_s::live for each connections.
Definition at line 228 of file netsed.c.
Number of rules.
Definition at line 223 of file netsed.c.
True when SIGINT signal was received.
Definition at line 234 of file netsed.c.