-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
170 lines (152 loc) · 3.83 KB
/
Copy pathmain.c
File metadata and controls
170 lines (152 loc) · 3.83 KB
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
/*
* 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 <unistd.h>
#include <string.h>
#include <netinet/ether.h>
#include "net.h"
#include "packet.h"
enum {
CMD_SCAN,
CMD_GET,
CMD_SET
};
struct ctx {
char *ifname;
uint8_t my_mac[ETH_ALEN];
uint8_t dst_mac[ETH_ALEN];
int sock;
int cmd;
char *arg_name;
char *arg_value;
};
void usage(char *prog)
{
printf(
"%s [options] <command> [arguments]\n"
"\n"
" Available commands:\n"
" scan Returns a list of devices on current network\n"
" set <name> <val> Set a property\n"
" get <name> Get a property\n"
"\n"
" Available options:\n"
" -i <ifname> Set network interaface to bind to\n"
" -d <addr> Set destination MAC address\n"
"\n",
prog);
}
void do_cmd(struct ctx *ctx)
{
struct packet *packet;
ctx->sock = init_sock(ctx->ifname);
if (ctx->sock < 0) {
printf("could not initialize socket\n");
return;
}
if (get_if_mac(ctx->ifname, ctx->my_mac)) {
printf("Could not retrieve interface mac\n");
return;
}
if (ctx->cmd == CMD_SCAN) {
packet = packet_new(ctx->my_mac, ctx->dst_mac, OP_READ_REQ);
packet_add_record(packet, RT_MODEL, 0, NULL);
packet_add_record(packet, RT_NAME, 0, NULL);
packet_add_record(packet, RT_MAC, 0, NULL);
packet_add_record(packet, RT_IP, 0, NULL);
packet_add_record(packet, RT_DHCP, 0, NULL);
packet_add_record(packet, RT_FIRMWARE, 0, NULL);
packet_add_record(packet, RT_NPORTS, 0, NULL);
packet_add_tail(packet);
#ifdef DEBUG
packet_dump(packet, stdout, 1);
#endif
net_send_packet(ctx->sock, packet);
packet_free(packet);
packet = net_receive_response(ctx->sock);
if (packet) {
#ifdef DEBUG
packet_dump(packet, stdout, 0);
#endif
printf("---------- Response ----------\n");
packet_print_records(packet);
}
} else {
printf("net implemented yet\n");
}
}
int main(int argc, char **argv)
{
struct ctx ctx;
int opt;
char *addr;
memset(&ctx, 0, sizeof(struct ctx));
while ((opt = getopt(argc, argv, "d:i:h")) != -1) {
switch (opt) {
case 'd':
addr = (char *)ether_aton(optarg);
if (!addr) {
printf("Invalid destination mac %s\n", optarg);
return 1;
}
memcpy(ctx.dst_mac, addr, ETH_ALEN);
break;
case 'i':
ctx.ifname = strdup(optarg);
break;
case 'h':
usage(argv[0]);
return 0;
default:
usage(argv[0]);
return 1;
}
}
if (!argv[optind]) {
printf("Command is required\n");
return 1;
}
if (!ctx.ifname) {
printf("Interface name is required\n");
return 1;
}
if (!strcmp(argv[optind], "scan")) {
ctx.cmd = CMD_SCAN;
} else if (!strcmp(argv[optind], "set")) {
if (optind + 2 >= argc) {
printf("Not enough arguments for set command\n");
return 1;
}
ctx.cmd = CMD_SET;
ctx.arg_name = strdup(argv[optind+1]);
ctx.arg_value = strdup(argv[optind+2]);
} else if (!strcmp(argv[optind], "get")) {
if (optind + 1 >= argc) {
printf("Not enough arguments for get command\n");
return 1;
}
ctx.cmd = CMD_GET;
ctx.arg_name = strdup(argv[optind+1]);
} else {
printf("Unrecognized command '%s'\n", argv[optind]);
usage(argv[0]);
}
do_cmd(&ctx);
return 0;
}