/[dynamips]/trunk/net.h
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/net.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sat Oct 6 16:45:40 2007 UTC (11 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 7077 byte(s)
make working copy

1 /*
2 * Copyright (c) 2006 Christophe Fillot.
3 * E-mail: cf@utc.fr
4 *
5 * net.h: Protocol Headers and Constants Definitions.
6 */
7
8 #ifndef __NET_H__
9 #define __NET_H__ 1
10
11 #include "utils.h"
12
13 #define N_IP_ADDR_LEN 4
14 #define N_IP_ADDR_BITS 32
15
16 #define N_IPV6_ADDR_LEN 16
17 #define N_IPV6_ADDR_BITS 128
18
19 /* IPv4 Address definition */
20 typedef m_uint32_t n_ip_addr_t;
21
22 /* IP Network definition */
23 typedef struct {
24 n_ip_addr_t net_addr;
25 n_ip_addr_t net_mask;
26 }n_ip_network_t;
27
28 /* IPv6 Address definition */
29 typedef struct {
30 union {
31 m_uint32_t u6_addr32[4];
32 m_uint16_t u6_addr16[8];
33 m_uint8_t u6_addr8[16];
34 }ip6;
35 }n_ipv6_addr_t;
36
37 /* IPv6 Network definition */
38 typedef struct {
39 n_ipv6_addr_t net_addr;
40 u_int net_mask;
41 }n_ipv6_network_t;
42
43 /* IP: Common Protocols */
44 #define N_IP_PROTO_ICMP 1
45 #define N_IP_PROTO_IGMP 2
46 #define N_IP_PROTO_TCP 6
47 #define N_IP_PROTO_UDP 17
48 #define N_IP_PROTO_IPV6 41
49 #define N_IP_PROTO_GRE 47
50 #define N_IP_PROTO_ESP 50
51 #define N_IP_PROTO_AH 51
52 #define N_IP_PROTO_ICMPV6 58
53 #define N_IP_PROTO_EIGRP 88
54 #define N_IP_PROTO_OSPF 89
55 #define N_IP_PROTO_PIM 103
56 #define N_IP_PROTO_SCTP 132
57 #define N_IP_PROTO_MAX 256
58
59 #define N_IP_OFFMASK 0x1fff
60
61 /* Maximum number of ports */
62 #define N_IP_PORT_MAX 65536
63
64 /* TCP: Header Flags */
65 #define N_TCP_FIN 0x01
66 #define N_TCP_SYN 0x02
67 #define N_TCP_RST 0x04
68 #define N_TCP_PUSH 0x08
69 #define N_TCP_ACK 0x10
70 #define N_TCP_URG 0x20
71
72 #define N_TCP_FLAGMASK 0x3F
73
74 /* IPv6 Header Codes */
75 #define N_IPV6_PROTO_ICMP 58
76 #define N_IPV6_OPT_HOP_BY_HOP 0 /* Hop-by-Hop header */
77 #define N_IPV6_OPT_DST 60 /* Destination Options Header */
78 #define N_IPV6_OPT_ROUTE 43 /* Routing header */
79 #define N_IPV6_OPT_FRAG 44 /* Fragment Header */
80 #define N_IPV6_OPT_AH 51 /* Authentication Header */
81 #define N_IPV6_OPT_ESP 50 /* Encryption Security Payload */
82 #define N_IPV6_OPT_COMP 108 /* Payload Compression Protocol */
83 #define N_IPV6_OPT_END 59 /* No more headers */
84
85 /* Standard Ethernet MTU */
86 #define N_ETH_MTU 1500
87
88 /* Ethernet Constants */
89 #define N_ETH_ALEN 6
90 #define N_ETH_HLEN sizeof(n_eth_hdr_t)
91
92 /* Minimum size for ethernet payload */
93 #define N_ETH_MIN_DATA_LEN 46
94 #define N_ETH_MIN_FRAME_LEN (N_ETH_MIN_DATA_LEN + N_ETH_HLEN)
95
96 #define N_ETH_PROTO_IP 0x0800
97 #define N_ETH_PROTO_IPV6 0x86DD
98 #define N_ETH_PROTO_ARP 0x0806
99 #define N_ETH_PROTO_DOT1Q 0x8100
100 #define N_ETH_PROTO_DOT1Q_2 0x9100
101 #define N_ETH_PROTO_DOT1Q_3 0x9200
102 #define N_ETH_PROTO_MPLS 0x8847
103 #define N_ETH_PROTO_MPLS_MC 0x8848
104 #define N_ETH_PROTO_LOOP 0x9000
105
106 /* size needed for a string buffer */
107 #define N_ETH_SLEN (N_ETH_ALEN*3)
108
109 /* ARP opcodes */
110 #define N_ARP_REQUEST 0x1
111 #define N_ARP_REPLY 0x2
112
113 /* Ethernet Address */
114 typedef struct {
115 m_uint8_t eth_addr_byte[N_ETH_ALEN];
116 } __attribute__ ((__packed__)) n_eth_addr_t;
117
118 /* Ethernet Header */
119 typedef struct {
120 n_eth_addr_t daddr; /* destination eth addr */
121 n_eth_addr_t saddr; /* source ether addr */
122 m_uint16_t type; /* packet type ID field */
123 } __attribute__ ((__packed__)) n_eth_hdr_t;
124
125 /* 802.1Q Ethernet Header */
126 typedef struct {
127 n_eth_addr_t daddr; /* destination eth addr */
128 n_eth_addr_t saddr; /* source ether addr */
129 m_uint16_t type; /* packet type ID field (0x8100) */
130 m_uint16_t vlan_id; /* VLAN id + CoS */
131 } __attribute__ ((__packed__)) n_eth_dot1q_hdr_t;
132
133 /* LLC header */
134 typedef struct {
135 m_uint8_t dsap;
136 m_uint8_t ssap;
137 m_uint8_t ctrl;
138 } __attribute__ ((__packed__)) n_eth_llc_hdr_t;
139
140 /* SNAP header */
141 typedef struct {
142 m_uint8_t oui[3];
143 m_uint16_t type;
144 } __attribute__ ((__packed__)) n_eth_snap_hdr_t;
145
146 /* Cisco ISL header */
147 typedef struct {
148 m_uint16_t hsa1; /* High bits of source MAC address */
149 m_uint8_t hsa2; /* (in theory: 0x00-00-0c) */
150 m_uint16_t vlan; /* VLAN + BPDU */
151 m_uint16_t index; /* Index port of source */
152 m_uint16_t res; /* Reserved for TokenRing and FDDI */
153 } __attribute__ ((__packed__)) n_eth_isl_hdr_t;
154
155 #define N_ISL_HDR_SIZE (sizeof(n_eth_llc_hdr_t) + sizeof(n_eth_isl_hdr_t))
156
157 /* Cisco SCP/RBCP header */
158 typedef struct {
159 m_uint8_t sa; /* Source Address */
160 m_uint8_t da; /* Destination Address */
161 m_uint16_t len; /* Data Length */
162 m_uint8_t dsap; /* Destination Service Access Point */
163 m_uint8_t ssap; /* Source Service Access Point */
164 m_uint16_t opcode; /* Opcode */
165 m_uint16_t seqno; /* Sequence Number */
166 m_uint8_t flags; /* Flags: command/response */
167 m_uint8_t unk1; /* Unknown */
168 m_uint16_t unk2; /* Unknown */
169 m_uint16_t unk3; /* Unknown */
170 } __attribute__ ((__packed__)) n_scp_hdr_t;
171
172 /* Check for a broadcast ethernet address */
173 static inline int eth_addr_is_bcast(n_eth_addr_t *addr)
174 {
175 static const char *bcast_addr = "\xff\xff\xff\xff\xff\xff";
176 return(!memcmp(addr,bcast_addr,6));
177
178 }
179
180 /* Check for a broadcast/multicast ethernet address */
181 static inline int eth_addr_is_mcast(n_eth_addr_t *addr)
182 {
183 return(addr->eth_addr_byte[0] & 1);
184 }
185
186 /* Check for Cisco ISL destination address */
187 static inline int eth_addr_is_cisco_isl(n_eth_addr_t *addr)
188 {
189 static const char *isl_addr = "\x01\x00\x0c\x00\x00";
190 return(!memcmp(addr,isl_addr,5)); /* only 40 bits to compare */
191 }
192
193 /* Check for a SNAP header */
194 static inline int eth_llc_check_snap(n_eth_llc_hdr_t *llc_hdr)
195 {
196 return((llc_hdr->dsap == 0xAA) &&
197 (llc_hdr->ssap == 0xAA) &&
198 (llc_hdr->ctrl == 0x03));
199 }
200
201 /* Number of bits in a contiguous netmask */
202 static inline int ip_bits_mask(n_ip_addr_t mask)
203 {
204 int prefix = 0;
205
206 while(mask) {
207 prefix++;
208 mask = mask & (mask - 1);
209 }
210 return(prefix);
211 }
212
213 /* Initialize IPv6 masks */
214 void ipv6_init_masks(void);
215
216 /* Convert an IPv4 address into a string */
217 char *n_ip_ntoa(char *buffer,n_ip_addr_t ip_addr);
218
219 /* Convert in IPv6 address into a string */
220 char *n_ipv6_ntoa(char *buffer,n_ipv6_addr_t *ipv6_addr);
221
222 /* Convert a string containing an IP address in binary */
223 int n_ip_aton(n_ip_addr_t *ip_addr,char *ip_str);
224
225 /* Convert an IPv6 address from string into binary */
226 int n_ipv6_aton(n_ipv6_addr_t *ipv6_addr,char *ip_str);
227
228 /* Parse an IPv4 CIDR prefix */
229 int ip_parse_cidr(char *token,n_ip_addr_t *net_addr,n_ip_addr_t *net_mask);
230
231 /* Parse an IPv6 CIDR prefix */
232 int ipv6_parse_cidr(char *token,n_ipv6_addr_t *net_addr,u_int *net_mask);
233
234 /* Parse a MAC address */
235 int parse_mac_addr(n_eth_addr_t *addr,char *str);
236
237 /* Convert an Ethernet address into a string */
238 char *n_eth_ntoa(char *buffer,n_eth_addr_t *addr,int format);
239
240 /* Create a new socket to connect to specified host */
241 int udp_connect(int local_port,char *remote_host,int remote_port);
242
243 /* Listen on the specified port */
244 int ip_listen(char *ip_addr,int port,int sock_type,int max_fd,int fd_array[]);
245
246 /* ISL rewrite */
247 void cisco_isl_rewrite(m_uint8_t *pkt,m_uint32_t tot_len);
248
249 #endif

  ViewVC Help
Powered by ViewVC 1.1.26