/[dynamips]/upstream/dynamips-0.2.6-RC2/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

Annotation of /upstream/dynamips-0.2.6-RC2/net.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Sat Oct 6 16:01:44 2007 UTC (14 years, 9 months ago) by dpavlin
Original Path: upstream/dynamips-0.2.5/net.h
File MIME type: text/plain
File size: 6195 byte(s)
import 0.2.5 from upstream

1 dpavlin 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     /* Check for a broadcast/multicast ethernet address */
158     static inline int eth_addr_is_mcast(n_eth_addr_t *addr)
159     {
160     return(addr->eth_addr_byte[0] & 1);
161     }
162    
163     /* Check for Cisco ISL destination address */
164     static inline int eth_addr_is_cisco_isl(n_eth_addr_t *addr)
165     {
166     static const char *isl_addr = "\x01\x00\x0c\x00\x00";
167     return(!memcmp(addr,isl_addr,5)); /* only 40 bits to compare */
168     }
169    
170     /* Check for a SNAP header */
171     static inline int eth_llc_check_snap(n_eth_llc_hdr_t *llc_hdr)
172     {
173     return((llc_hdr->dsap == 0xAA) &&
174     (llc_hdr->ssap == 0xAA) &&
175     (llc_hdr->ctrl == 0x03));
176     }
177    
178     /* Number of bits in a contiguous netmask */
179     static inline int ip_bits_mask(n_ip_addr_t mask)
180     {
181     int prefix = 0;
182    
183     while(mask) {
184     prefix++;
185     mask = mask & (mask - 1);
186     }
187     return(prefix);
188     }
189    
190     /* Initialize IPv6 masks */
191     void ipv6_init_masks(void);
192    
193     /* Convert an IPv4 address into a string */
194     char *n_ip_ntoa(char *buffer,n_ip_addr_t ip_addr);
195    
196     /* Convert in IPv6 address into a string */
197     char *n_ipv6_ntoa(char *buffer,n_ipv6_addr_t *ipv6_addr);
198    
199     /* Convert a string containing an IP address in binary */
200     int n_ip_aton(n_ip_addr_t *ip_addr,char *ip_str);
201    
202     /* Convert an IPv6 address from string into binary */
203     int n_ipv6_aton(n_ipv6_addr_t *ipv6_addr,char *ip_str);
204    
205     /* Parse an IPv4 CIDR prefix */
206     int ip_parse_cidr(char *token,n_ip_addr_t *net_addr,n_ip_addr_t *net_mask);
207    
208     /* Parse an IPv6 CIDR prefix */
209     int ipv6_parse_cidr(char *token,n_ipv6_addr_t *net_addr,u_int *net_mask);
210    
211     /* Parse a MAC address */
212     int parse_mac_addr(n_eth_addr_t *addr,char *str);
213    
214     /* Convert an Ethernet address into a string */
215     char *n_eth_ntoa(char *buffer,n_eth_addr_t *addr,int format);
216    
217     /* Create a new socket to connect to specified host */
218     int udp_connect(int local_port,char *remote_host,int remote_port);
219    
220     /* Listen on the specified port */
221     int ip_listen(int port,int sock_type,int max_fd,int fd_array[]);
222    
223     #endif

  ViewVC Help
Powered by ViewVC 1.1.26