Fix silly naming clash on MinGW
[oweals/nmrpflash.git] / main.c
1 /**
2  * nmrp-flash - Netgear Unbrick Utility
3  * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
4  *
5  * nmrp-flash is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * nmrp-flash is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with nmrp-flash.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <getopt.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "nmrpd.h"
24 #include "ethsock.h"
25
26 void usage(FILE *fp)
27 {
28         fprintf(fp,
29                         "Usage: nmrp-flash [OPTIONS...]\n"
30                         "\n"
31                         "Options (-a, -i and -f are mandatory):\n"
32                         " -a <ipaddr>     IP address to assign to target device\n"
33                         " -f <firmware>   Firmware file\n"
34                         " -i <interface>  Network interface directly connected to device\n"
35                         " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
36                         " -M <netmask>    Subnet mask to assign to target device\n"
37                         " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
38                         " -T <timeout>    Time to wait after successfull TFTP upload\n"
39                         " -p <port>       Port to use for TFTP upload\n"
40                         " -V              Print version and exit\n"
41                         " -L              List network interfaces\n"
42                         " -h              Show this screen\n"
43                         "\n"
44                         "Example:\n"
45                         "\n"
46                         "$ sudo nmrp-flash -a 192.168.1.254 -i eth0 -f firmware.bin\n"
47                         "\n"
48                         "nmrp-flash v%s, Copyright (C) 2016 Joseph C. Lehner\n"
49                         "nmrp-flash is free software, licensed under the GNU GPLv3.\n"
50                         "Source code at https://github.com/jclehner/nmrp-flash\n"
51                         "\n",
52                         NMRPD_VERSION
53           );
54 }
55
56 int main(int argc, char **argv)
57 {
58         int c, val, max;
59         struct nmrpd_args args = {
60                 .rx_timeout = 200,
61                 .ul_timeout = 60000,
62                 .tftpcmd = NULL,
63                 .filename = NULL,
64                 .ipaddr = NULL,
65                 .ipmask = "255.255.255.0",
66                 .intf = NULL,
67                 .mac = "ff:ff:ff:ff:ff:ff",
68                 .op = NMRP_UPLOAD_FW,
69                 .port = 69,
70                 .force_root = 1
71         };
72
73         opterr = 0;
74
75         while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLV")) != -1) {
76                 max = 0x7fffffff;
77                 switch (c) {
78                         case 'a':
79                                 args.ipaddr = optarg;
80                                 break;
81                         case 'f':
82                                 args.filename = optarg;
83                                 break;
84                         case 'i':
85                                 args.intf = optarg;
86                                 break;
87                         case 'm':
88                                 args.mac = optarg;
89                                 break;
90                         case 'M':
91                                 args.ipmask = optarg;
92                                 break;
93                         case 'p':
94                                 max = 0xffff;
95                         case 'T':
96                         case 't':
97                                 val = atoi(optarg);
98                                 if (val <= 0 || val > max) {
99                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
100                                         return 1;
101                                 }
102
103                                 if (c == 'p') {
104                                         args.port = val;
105                                 } else if (c == 't') {
106                                         args.rx_timeout = val;
107                                 } else {
108                                         args.ul_timeout = val;
109                                 }
110
111                                 break;
112                         case 'V':
113                                 printf("nmrp-flash v%s\n", NMRPD_VERSION);
114                                 return 0;
115                         case 'L':
116                                 return ethsock_list_all();
117                         case 'h':
118                                 usage(stdout);
119                                 return 0;
120                         default:
121                                 usage(stderr);
122                                 return 1;
123                 }
124         }
125
126         if (!args.filename || !args.intf || !args.ipaddr) {
127                 usage(stderr);
128                 return 1;
129         }
130
131         if (geteuid() != 0) {
132                 fprintf(stderr, "This program must be run as root!\n");
133                 return 1;
134         }
135
136         return nmrp_do(&args);
137 }