39f327c32e3efc720fe8cd5a7c1e651b79912df0
[oweals/nmrpflash.git] / main.c
1 #include <getopt.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "nmrpd.h"
5
6 void usage(FILE *fp)
7 {
8         fprintf(fp,
9                         "Usage: nmrpd [OPTIONS...]\n"
10                         "\n"
11                         "Options:\n"
12                         " -a <ipaddr>     IP address to assign to target device\n"
13                         " -f <firmware>   Firmware file\n"
14                         " -i <interface>  Network interface directly connected to device\n"
15                         " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
16                         " -M <netmask>    Subnet mask to assign to target device\n"
17                         " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
18                         " -T <timeout>    Time to wait after successfull TFTP upload\n"
19                         " -p <port>       Port to use for TFTP upload\n"
20                         " -h              Show this screen\n"
21                         "\n"
22                         "Options -a, -i and -f are mandatory!\n"
23                         "\n"
24           );
25 }
26
27 int main(int argc, char **argv)
28 {
29         int c, val, max;
30         struct nmrpd_args args = {
31                 .rx_timeout = 200,
32                 .ul_timeout = 60000,
33                 .filename = NULL,
34                 .ipaddr = NULL,
35                 .ipmask = "255.255.255.0",
36                 .intf = NULL,
37                 .mac = "ff:ff:ff:ff:ff:ff",
38                 .op = NMRP_UPLOAD_FW,
39                 .port = 69,
40                 .force_root = 1
41         };
42
43         opterr = 0;
44
45         while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:")) != -1) {
46                 max = 0xffffffff;
47                 switch (c) {
48                         case 'a':
49                                 args.ipaddr = optarg;
50                                 break;
51                         case 'f':
52                                 args.filename = optarg;
53                                 break;
54                         case 'i':
55                                 args.intf = optarg;
56                                 break;
57                         case 'm':
58                                 args.mac = optarg;
59                                 break;
60                         case 'M':
61                                 args.ipmask = optarg;
62                                 break;
63                         case 'p':
64                                 max = 0xffff;
65                         case 'T':
66                         case 't':
67                                 val = atoi(optarg);
68                                 if (val <= 0 || val > max) {
69                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
70                                         return 1;
71                                 }
72
73                                 if (c == 'p') {
74                                         args.port = val;
75                                 } else if (c == 't') {
76                                         args.rx_timeout = val;
77                                 } else {
78                                         args.ul_timeout = val;
79                                 }
80
81                                 break;
82                         case 'h':
83                                 usage(stdout);
84                                 return 0;
85                         default:
86                                 usage(stderr);
87                                 return 1;
88                 }
89         }
90
91         if (!args.filename || !args.intf || !args.ipaddr) {
92                 usage(stderr);
93                 return 1;
94         }
95
96         return nmrp_do(&args);
97 }