net: arp packet builder and interpreter
[quarnos.git] / resources / net / arp_packet.h
blobe96ecceb9362bedd1744daa454bb648330c2518e
1 #ifndef _ARP_PACKET_H_
2 #define _ARP_PACKET_H_
4 #include "arch/low/general.h"
5 #include "packet_builder.h"
7 namespace net {
8 class arp_packet : public packet_builder {
9 private:
10 struct packet {
11 u16 htype;
12 u16 ptype;
13 u8 hlen;
14 u8 plen;
15 u16 oper;
16 u32 sha0;
17 u16 sha1;
18 u32 spa;
19 u16 tha0;
20 u32 tha1;
21 u32 tpa;
22 } __attribute__((packed));
25 p<packet> pkg;
26 public:
27 arp_packet() : pkg(new packet) { }
28 ~arp_packet() {
29 pkg.dispose();
32 buffer generate() const {
33 return buffer::to_mem(pkg);
36 void add_data(const buffer&) { }
38 enum ptypes {
39 p_ipv4 = 0x800
42 enum htypes {
43 h_ethernet = 1
46 void set_ptype(ptypes pt) {
47 pkg->ptype = to_be16(pt);
48 pkg->plen = ipv4_addr::in_bytes();
51 void set_htype(htypes ht) {
52 pkg->htype = to_be16(ht);
53 pkg->hlen = mac_addr::in_bytes();
56 enum arp_type {
57 arp_request = 1,
58 arp_reply = 2
61 void set_type(arp_type at) {
62 pkg->oper = to_be16(at);
65 void set_source_haddr(const mac_addr &addr) {
66 pkg->sha0 = addr.first_be32();
67 pkg->sha1 = addr.last_be16();
70 void set_source_paddr(const ipv4_addr &addr) {
71 pkg->spa = addr.to_be();
74 void set_target_haddr(const mac_addr &addr) {
75 pkg->tha0 = addr.first_be16();
76 pkg->tha1 = addr.last_be32();
79 void set_target_paddr(const ipv4_addr &addr) {
80 pkg->tpa = addr.to_be();
83 arp_packet(const buffer &x) : pkg(x.cast<packet>()) { }
85 ptypes get_ptype() const {
86 return (ptypes)from_be16(pkg->ptype);
89 htypes get_htype() const {
90 return (htypes)from_be16(pkg->htype);
93 arp_type get_type() const {
94 return (arp_type)from_be16(pkg->oper);
97 mac_addr get_source_haddr() const {
98 return mac_addr::from_be(pkg->sha0, pkg->sha1);
101 ipv4_addr get_source_paddr() const {
102 return ipv4_addr::from_be(pkg->spa);
105 mac_addr get_target_haddr() const {
106 return mac_addr::from_be_r(pkg->tha0, pkg->tha1);
109 ipv4_addr get_target_paddr() const {
110 return ipv4_addr::from_be(pkg->tpa);
115 #endif