]> git.sur5r.net Git - ngadmin/blob - raw/include/protocol.h
Add support for password encryption
[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 extern const char passwordKey[];
68
69
70 void passwordEndecode (char *buf, unsigned int len);
71
72
73 int trim (char *txt, int start);
74
75
76 static inline int min (int a, int b)
77 {
78         return a < b ? a : b;
79 }
80
81
82 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum);
83
84
85 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum);
86
87
88 static inline void initNgPacket (struct ng_packet *np)
89 {
90         np->ah = (struct attr_header*)np->nh->data;
91 }
92
93
94 void addPacketAttr (struct ng_packet *np, struct attr *at);
95
96
97 static inline int getPacketTotalSize (const struct ng_packet *np)
98 {
99         return ((char*)np->ah) - np->buffer;
100 }
101
102
103 struct attr* newAttr (unsigned short attr, unsigned short size, void *data);
104
105
106 static inline struct attr* newEmptyAttr (unsigned short attr)
107 {
108         return newAttr(attr, 0, NULL);
109 }
110
111
112 static inline struct attr* newByteAttr (unsigned short attr, unsigned char value)
113 {
114         char *v = malloc(sizeof(char));
115         
116         *v = value;
117         
118         return newAttr(attr, sizeof(char), v);
119 }
120
121
122 static inline struct attr* newShortAttr (unsigned short attr, short value)
123 {
124         short *v = malloc(sizeof(short));
125         
126         *v = value;
127         
128         return newAttr(attr, sizeof(short), v);
129 }
130
131
132 static inline struct attr* newIntAttr (unsigned short attr, int value)
133 {
134         int *v = malloc(sizeof(int));
135         
136         *v = value;
137         
138         return newAttr(attr, sizeof(int), v);
139 }
140
141
142 static inline struct attr* newAddrAttr (unsigned short attr, struct in_addr value)
143 {
144         struct in_addr *v = malloc(sizeof(struct in_addr));
145         
146         *v = value;
147         
148         return newAttr(attr, sizeof(struct in_addr), v);
149 }
150
151
152 void freeAttr (struct attr *at);
153
154
155 int addPacketAttributes (struct ng_packet *np, const List* attr, unsigned char ports);
156
157
158 int extractPacketAttributes (struct ng_packet *np, List *attr, unsigned char ports);
159
160
161 void filterAttributes (List *attr, ...);
162
163
164 #endif
165