-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacket.c
237 lines (201 loc) · 5.02 KB
/
packet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* nsdpc : management tool for Netgear switches supporting NSPD protocol
*
* Copyright (C) 2013 <b.galvani@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include "packet.h"
struct packet {
char *data;
int len;
int mlen;
char *ptr;
};
struct packet *packet_new(uint8_t *src, uint8_t *dst, int op)
{
struct packet *packet;
struct msg_h *header;
int size;
srand(time(NULL));
packet = calloc(1, sizeof(struct packet));
if (!packet)
return NULL;
size = MSG_H_LEN + 32;
packet->data = calloc(1, size);
if (!packet->data) {
free(packet);
return NULL;
}
packet->len = MSG_H_LEN;
packet->mlen = size;
header = (struct msg_h *)packet->data;
header->ver = 1;
header->op = op;
header->seq = htons(rand());
memcpy(header->src, src, ETH_ALEN);
memcpy(header->dst, dst, ETH_ALEN);
memcpy(header->sig, SIGN, SIGN_LEN);
return packet;
}
struct packet *packet_new_buffer(char *data, char len)
{
struct packet *packet;
packet = calloc(1, sizeof(struct packet));
if (!packet)
return NULL;
packet->data = malloc(len);
if (!packet->data) {
free(packet);
return NULL;
}
memcpy(packet->data, data, len);
packet->len = len;
packet->mlen = len;
return packet;
}
char *packet_data(struct packet *packet)
{
return packet->data;
}
int packet_len(struct packet *packet)
{
return packet->len;
}
void packet_add_record(struct packet *packet, int type, int len, char *data)
{
struct record_h *header;
int rec_size = len + REC_H_LEN;
int new_size;
if (packet->len + rec_size >= packet->mlen) {
new_size = packet->len + rec_size;
packet->data = realloc(packet->data, new_size);
packet->mlen = new_size;
}
header = (struct record_h *)(packet->data + packet->len);
header->type = htons(type);
header->len = htons(len);
memcpy(packet->data + REC_H_LEN, data, len);
packet->len += rec_size;
}
void packet_add_tail(struct packet *packet)
{
packet_add_record(packet, RT_TAIL, 0, NULL);
}
void packet_free(struct packet *packet)
{
if (packet) {
if (packet->data)
free(packet->data);
free(packet);
}
}
void packet_dump(struct packet *packet, FILE *out, int direction)
{
int i;
printf("%s PACKET: ", direction ? "OUT" : "IN ");
for (i = 0; i < packet->len; i++) {
fprintf(out, "%02X ", packet->data[i]);
}
fprintf(out, "\n");
}
void packet_parse_records(struct packet *packet,
void (*fn)(int type, int len, char *data))
{
char *ptr = packet->data;
struct record_h *record;
int type, len;
ptr += MSG_H_LEN;
while (ptr + REC_H_LEN < packet->data + packet->len) {
record = (struct record_h *)ptr;
type = ntohs(record->type);
len = ntohs(record->len);
if (ptr + REC_H_LEN + len >= packet->data + packet->len)
break;
fn(type, len, ptr + REC_H_LEN);
ptr += REC_H_LEN + len;
}
}
void rec_printer_str(int len, char *data)
{
char *buffer;
buffer = alloca(len + 1);
memcpy(buffer, data, len);
buffer[len] = 0;
printf("%s", buffer);
}
void rec_printer_mac(int len, char *data)
{
if (len == ETH_ALEN)
printf("%s", ether_ntoa((struct ether_addr *)data));
else
printf("malformed address");
}
void rec_printer_ip(int len, char *data)
{
struct in_addr *addr = (struct in_addr *)data;
printf("%s", inet_ntoa(*addr));
}
void rec_printer_int(int len, char *data)
{
if (len == 1)
printf("%d", *data);
}
void rec_printer_boolean(int len, char *data)
{
if (len != 1 || (*data != 0 && *data != 1))
printf("unknown");
else if (*data == 1)
printf("true");
else
printf("false");
}
struct {
int type;
char *desc;
void (*printer)(int len, char *data);
} record_printers[] = {
{ RT_MODEL, "Model", rec_printer_str },
{ RT_NAME, "Name", rec_printer_str },
{ RT_MAC, "MAC", rec_printer_mac },
{ RT_PASSWORD, "Password", rec_printer_str },
{ RT_IP, "IP", rec_printer_ip },
{ RT_DHCP, "DHCP enabled", rec_printer_boolean },
{ RT_FIRMWARE, "Firmware", rec_printer_str},
{ RT_NPORTS, "Num of ports", rec_printer_int }
};
#define NUM_REC_PRINTERS ((sizeof record_printers)/(sizeof record_printers[0]))
void print_record(int type, int len, char *data)
{
int i;
for (i = 0; i < NUM_REC_PRINTERS; i++) {
if (type == record_printers[i].type) {
printf("%-16s: ", record_printers[i].desc);
record_printers[i].printer(len, data);
printf("\n");
break;
}
}
}
void packet_print_records(struct packet *packet)
{
packet_parse_records(packet, print_record);
}