net.h Source File

Back to the index.

net.h
Go to the documentation of this file.
1 #ifndef NET_H
2 #define NET_H
3 
4 /*
5  * Copyright (C) 2004-2010 Anders Gavare. 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 are met:
9  *
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. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *
31  * Emulated network support. (See net.c for more info.)
32  */
33 
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 
38 struct emul;
40 struct remote_net;
41 
42 
43 /* Default emulated "simple" IPv4 network, if nothing else is specified: */
44 #define NET_DEFAULT_IPV4_MASK "10.0.0.0"
45 #define NET_DEFAULT_IPV4_LEN 8
46 
47 
48 /*****************************************************************************/
49 
50 /*
51  * NOTE: These are already defined in <net/ethernet.h> on e.g. FreeBSD,
52  * but they are missing in some systems (at least in Linux).
53  */
54 #define ETHERTYPE_SPRITE 0x0500
55 #define ETHERTYPE_IP 0x0800
56 #define ETHERTYPE_ARP 0x0806
57 #define ETHERTYPE_REVARP 0x8035
58 #define ETHERTYPE_IPV6 0x86DD
59 
60 /*****************************************************************************/
61 
62 /* NOTE: udp_connection and tcp_connection are actually for
63  internal use only. */
65  int in_use;
67 
68  /* Inside: */
69  unsigned char ethernet_address[6];
70  unsigned char inside_ip_address[4];
72 
73  /* TODO: Fragment support for outgoing packets! */
74  int fake_ns;
75 
76  /* Outside: */
77  int udp_id;
78  int socket;
79  unsigned char outside_ip_address[4];
81 };
82 
84  int in_use;
86 
87  /* Inside: */
88  unsigned char ethernet_address[6];
89  unsigned char inside_ip_address[4];
91  uint32_t inside_timestamp;
92 
93  /* TODO: tx and rx buffers? */
94  unsigned char *incoming_buf;
98 
99  uint32_t inside_seqnr;
100  uint32_t inside_acknr;
101  uint32_t outside_seqnr;
102  uint32_t outside_acknr;
103 
104  /* Outside: */
105  int state;
106  int tcp_id;
107  int socket;
108  unsigned char outside_ip_address[4];
111 };
112 
113 /*****************************************************************************/
114 
115 
116 #define MAX_TCP_CONNECTIONS 100
117 #define MAX_UDP_CONNECTIONS 100
118 
119 struct net {
120  /* The emul struct which this net belong to: */
121  struct emul *emul;
122 
123  /* The network's addresses: */
124  struct in_addr netmask_ipv4;
126 
127  /* NICs connected to this network: */
128  int n_nics;
129  void **nic_extra; /* one void * per NIC */
130 
131  /* The "special machine": */
132  unsigned char gateway_ipv4_addr[4];
133  unsigned char gateway_ethernet_addr[6];
134 
135  /* Read from /etc/resolv.conf: */
136  char *domain_name;
138  struct in_addr nameserver_ipv4;
139 
140  int64_t timestamp;
141 
144 
145  struct udp_connection udp_connections[MAX_UDP_CONNECTIONS];
146  struct tcp_connection tcp_connections[MAX_TCP_CONNECTIONS];
147 
148  /* Distributed network: */
152 };
153 
154 /* net_misc.c: */
155 void net_debugaddr(void *addr, int type);
156 void net_generate_unique_mac(struct machine *, unsigned char *macbuf);
157 void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet,
158  size_t len);
159 
160 /* net_ip.c: */
161 void net_ip_checksum(unsigned char *ip_header, int chksumoffset, int len);
162 void net_ip_tcp_checksum(unsigned char *tcp_header, int chksumoffset,
163  int tcp_len, unsigned char *srcaddr, unsigned char *dstaddr,
164  int udpflag);
165 void net_ip_tcp_connectionreply(struct net *net, void *extra,
166  int con_id, int connecting, unsigned char *data, int datalen, int rst);
167 void net_ip_broadcast(struct net *net, void *extra,
168  unsigned char *packet, int len);
169 void net_ip(struct net *net, void *extra, unsigned char *packet, int len);
170 void net_udp_rx_avail(struct net *net, void *extra);
171 void net_tcp_rx_avail(struct net *net, void *extra);
172 
173 /* net.c: */
175  struct net *net, void *extra, size_t len);
176 int net_ethernet_rx_avail(struct net *net, void *extra);
177 int net_ethernet_rx(struct net *net, void *extra,
178  unsigned char **packetp, int *lenp);
179 void net_ethernet_tx(struct net *net, void *extra,
180  unsigned char *packet, int len);
181 void net_dumpinfo(struct net *net);
182 void net_add_nic(struct net *net, void *extra, unsigned char *macaddr);
183 struct net *net_init(struct emul *emul, int init_flags,
184  const char *ipv4addr, int netipv4len, char **remote, int n_remote,
185  int local_port, const char *settings_prefix);
186 
187 /* Flag used to signify that this net should have a gateway: */
188 #define NET_INIT_FLAG_GATEWAY 1
189 
190 
191 /*
192  * This is for internal use in src/net.c:
193  */
197 
198  void *extra;
199  unsigned char *data;
200  int len;
201 };
202 
203 struct remote_net {
204  struct remote_net *next;
205 
206  char *name;
207  struct in_addr ipv4_addr;
208  int portnr;
209 };
210 
211 #define TCP_OUTSIDE_TRYINGTOCONNECT 1
212 #define TCP_OUTSIDE_CONNECTED 2
213 #define TCP_OUTSIDE_DISCONNECTED 3
214 #define TCP_OUTSIDE_DISCONNECTED2 4
215 
216 #define TCP_INCOMING_BUF_LEN 2000
217 
218 #define NET_ADDR_IPV4 1
219 #define NET_ADDR_IPV6 2
220 #define NET_ADDR_ETHERNET 3
221 
222 #endif /* NET_H */
void net_ethernet_tx(struct net *net, void *extra, unsigned char *packet, int len)
Definition: net.cc:371
void net_ip_checksum(unsigned char *ip_header, int chksumoffset, int len)
Definition: net_ip.cc:55
struct remote_net * remote_nets
Definition: net.h:151
uint32_t inside_seqnr
Definition: net.h:99
unsigned char outside_ip_address[4]
Definition: net.h:79
unsigned char * incoming_buf
Definition: net.h:94
int n_nics
Definition: net.h:128
struct emul * emul
Definition: net.h:121
int64_t timestamp
Definition: net.h:140
int local_port_socket
Definition: net.h:150
int inside_udp_port
Definition: net.h:71
int outside_tcp_port
Definition: net.h:109
uint32_t outside_seqnr
Definition: net.h:101
#define MAX_TCP_CONNECTIONS
Definition: net.h:116
unsigned char inside_ip_address[4]
Definition: net.h:70
uint32_t outside_acknr
Definition: net.h:102
int fake_ns
Definition: net.h:74
void net_ip_tcp_checksum(unsigned char *tcp_header, int chksumoffset, int tcp_len, unsigned char *srcaddr, unsigned char *dstaddr, int udpflag)
Definition: net_ip.cc:91
int local_port
Definition: net.h:149
struct ethernet_packet_link * net_allocate_ethernet_packet_link(struct net *net, void *extra, size_t len)
Definition: net.cc:72
void net_ip_broadcast(struct net *net, void *extra, unsigned char *packet, int len)
Definition: net_ip.cc:1133
void net_ip(struct net *net, void *extra, unsigned char *packet, int len)
Definition: net_ip.cc:885
struct ethernet_packet_link * first_ethernet_packet
Definition: net.h:142
unsigned char ethernet_address[6]
Definition: net.h:69
int in_use
Definition: net.h:84
int net_ethernet_rx(struct net *net, void *extra, unsigned char **packetp, int *lenp)
Definition: net.cc:316
void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet, size_t len)
Definition: net_misc.cc:125
void net_udp_rx_avail(struct net *net, void *extra)
Definition: net_ip.cc:1225
void net_add_nic(struct net *net, void *extra, unsigned char *macaddr)
Definition: net.cc:598
int state
Definition: net.h:105
u_short data
Definition: siireg.h:79
int64_t last_used_timestamp
Definition: net.h:66
uint32_t inside_timestamp
Definition: net.h:91
Definition: net.h:119
void net_generate_unique_mac(struct machine *, unsigned char *macbuf)
Definition: net_misc.cc:88
int in_use
Definition: net.h:65
void net_tcp_rx_avail(struct net *net, void *extra)
Definition: net_ip.cc:1384
int tcp_id
Definition: net.h:106
int udp_id
Definition: net.h:77
Definition: emul.h:37
char * domain_name
Definition: net.h:136
int incoming_buf_rounds
Definition: net.h:95
void net_ip_tcp_connectionreply(struct net *net, void *extra, int con_id, int connecting, unsigned char *data, int datalen, int rst)
Definition: net_ip.cc:228
int net_ethernet_rx_avail(struct net *net, void *extra)
Definition: net.cc:253
uint32_t addr
int inside_tcp_port
Definition: net.h:90
char * name
Definition: net.h:206
int64_t last_used_timestamp
Definition: net.h:85
int incoming_buf_len
Definition: net.h:96
int outside_udp_port
Definition: net.h:80
#define MAX_UDP_CONNECTIONS
Definition: net.h:117
struct net * net_init(struct emul *emul, int init_flags, const char *ipv4addr, int netipv4len, char **remote, int n_remote, int local_port, const char *settings_prefix)
Definition: net.cc:720
int netmask_ipv4_len
Definition: net.h:125
int socket
Definition: net.h:107
uint32_t inside_acknr
Definition: net.h:100
void net_debugaddr(void *addr, int type)
Definition: net_misc.cc:50
int socket
Definition: net.h:78
int portnr
Definition: net.h:208
struct remote_net * next
Definition: net.h:204
void net_dumpinfo(struct net *net)
Definition: net.cc:655
uint32_t incoming_buf_seqnr
Definition: net.h:97
void ** nic_extra
Definition: net.h:129
uint32_t outside_timestamp
Definition: net.h:110
int nameserver_known
Definition: net.h:137
struct ethernet_packet_link * last_ethernet_packet
Definition: net.h:143

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13