]> git.sur5r.net Git - ngadmin/blob - raw/include/protocol.h
Separated lib in two parts : low level and high level.
[ngadmin] / raw / include / protocol.h
1
2 #ifndef DEF_PROTOCOL
3 #define DEF_PROTOCOL
4
5
6 #include <stdlib.h>
7 #include <arpa/inet.h>
8
9 #include <netinet/ether.h>
10
11 #include "list.h"
12
13
14 #define CLIENT_PORT             63321
15 #define SWITCH_PORT             63322
16
17 #define CODE_READ_REQ           1
18 #define CODE_READ_REP           2
19 #define CODE_WRITE_REQ          3
20 #define CODE_WRITE_REP          4
21
22 #define ERROR_READONLY          3
23 #define ERROR_INVALID_VALUE     5
24 #define ERROR_DENIED            7
25
26
27 struct ng_header {
28         char version;                   /* always 1, maybe version */
29         char code;                      /* request code: read request, read reply, write request, write reply */
30         unsigned char error;            /* error code, 0 when no error */
31         unsigned char unk1;             /* always 0, unknown */
32         unsigned short attr;            /* attribute code which caused error, 0 when no error */
33         char unk2[2];                   /* always 0, unknown */
34         char client_mac[ETH_ALEN];      /* client MAC address */
35         char switch_mac[ETH_ALEN];      /* switch MAC address */
36         unsigned int seqnum;            /* sequence number */
37         char proto_id[4];               /* always "NSDP", maybe short for "Netgear Switch Description Protocol" */
38         char unk3[4];                   /* always 0, unknown */
39         char data[0];
40 } __attribute__((packed));
41
42
43 struct attr_header {
44         unsigned short attr;
45         unsigned short size;
46         char data[0];
47 } __attribute__((packed));
48
49
50 struct ng_packet {
51         union {
52                 char *buffer;
53                 struct ng_header *nh;
54         };
55         int maxlen;
56         struct attr_header *ah;
57 };
58
59
60 struct attr {
61         unsigned short attr;
62         unsigned short size;
63         void *data;
64 };
65
66
67
68 int trim (char *txt, int start);
69
70
71 static inline int min (int a, int b)
72 {
73         return a < b ? a : b;
74 }
75
76
77 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum);
78
79
80 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum);
81
82
83 static inline void initNgPacket (struct ng_packet *np)
84 {
85         np->ah = (struct attr_header*)np->nh->data;
86 }
87
88
89 void addPacketAttr (struct ng_packet *np, struct attr *at);
90
91
92 static inline int getPacketTotalSize (const struct ng_packet *np)
93 {
94         return ((char*)np->ah) - np->buffer;
95 }
96
97
98 struct attr* newAttr (unsigned short attr, unsigned short size, void *data);
99
100
101 static inline struct attr* newEmptyAttr (unsigned short attr)
102 {
103         return newAttr(attr, 0, NULL);
104 }
105
106
107 static inline struct attr* newByteAttr (unsigned short attr, unsigned char value)
108 {
109         char *v = malloc(sizeof(char));
110         
111         *v = value;
112         
113         return newAttr(attr, sizeof(char), v);
114 }
115
116
117 static inline struct attr* newShortAttr (unsigned short attr, short value)
118 {
119         short *v = malloc(sizeof(short));
120         
121         *v = value;
122         
123         return newAttr(attr, sizeof(short), v);
124 }
125
126
127 static inline struct attr* newIntAttr (unsigned short attr, int value)
128 {
129         int *v = malloc(sizeof(int));
130         
131         *v = value;
132         
133         return newAttr(attr, sizeof(int), v);
134 }
135
136
137 static inline struct attr* newAddrAttr (unsigned short attr, struct in_addr value)
138 {
139         struct in_addr *v = malloc(sizeof(struct in_addr));
140         
141         *v = value;
142         
143         return newAttr(attr, sizeof(struct in_addr), v);
144 }
145
146
147 void freeAttr (struct attr *at);
148
149
150 int addPacketAttributes (struct ng_packet *np, const List* attr, unsigned char ports);
151
152
153 int extractPacketAttributes (struct ng_packet *np, unsigned char *error, unsigned short *attr_error, List *attr, unsigned short filter_attr, unsigned char ports);
154
155
156 #endif
157