Socket infrastructure change and basic udp send.
[akaros.git] / kern / include / net.h
1 /* Copyright (c) 2010 The Regents of the University of California
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * David Zhu <yuzhu@cs.berkeley.edu>
4  * See LICENSE for details.
5  *
6  * Arch independent networking infrastructure */
7
8 #ifndef ROS_KERN_NET_H
9 #define ROS_KERN_NET_H
10
11 #include <bits/netinet.h>
12 #include <stdio.h>
13
14 /* network internal error code */
15 #define ERR_BUF 
16
17 /* A few other useful standard defines.  Note the IP header can change size. */
18 #define ETH_HDR_SZ 14 // without padding, 16 with padding
19 #define UDP_HDR_SZ 8
20 #define IP_ETH_TYPE 0x0800
21 #define IP_HDR_SZ 20
22 /* ROS defaults: They really should be netif specific*/
23 #define DEFAULT_TTL 64
24 #define DEFAULT_MTU 1500
25 // is this network order already?
26 #define IP_ADDR 0x0A000002  //lookout for address order
27
28
29 /* Don't forget the bytes are in network order */
30 struct ethernet_hdr {
31         /* might want to pad to increase access speed? */
32         uint8_t           padding[2];
33         uint8_t                                         dst_mac[6];
34         uint8_t                                         src_mac[6];
35         uint16_t                                        eth_type;
36         /* might be an optional 802.1q tag here */
37 } __attribute__((packed));
38
39
40 /* For the bit-enumerated fields, note that you need to read "backwards" through
41  * the byte (first bits in memory are the "LSB" of the byte).  Can't seem to be
42  * able to do it with flags/fragments (3/13 bits each...). */
43 struct ip_hdr {
44         /* TODO: Are these accesses slower? */
45         unsigned                                        hdr_len : 4;
46         unsigned                                        version : 4;
47         uint8_t                                         tos;
48
49         uint16_t                                        packet_len;
50         /* ip header id is used for fragmentation reassembly */
51         uint16_t                                        id;  // 1 index this?
52         /* flags controlling fragmentation(do not fragment etc) */
53         uint16_t                                        flags_frags;
54         /* statically set to a constatnt right now */
55         uint8_t                                         ttl;
56         uint8_t                                         protocol;
57         uint16_t                                        checksum;
58         uint32_t                                        src_addr;
59         uint32_t                                        dst_addr;
60         /* Options could be here (depends on the hdr length) */
61 } __attribute__((packed));
62
63 struct udp_hdr {
64         uint16_t                                        src_port;
65         uint16_t                                        dst_port;
66         uint16_t                                        length;
67         uint16_t                                        checksum;
68 };
69
70 uint16_t __ip_checksum(void *buf, unsigned int len, uint32_t sum);
71 uint16_t ip_checksum(struct ip_hdr *ip_hdr);
72 uint16_t udp_checksum(struct ip_hdr *ip_hdr, struct udp_hdr *udp_hdr);
73
74 // TODO: Move this to a better location
75 void dumppacket(unsigned char *buff, size_t len);
76 #endif /* ROS_KERN_NET_H */