Remove ethsock.h
[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
25 int verbosity = 0;
26
27 void usage(FILE *fp)
28 {
29         fprintf(fp,
30                         "Usage: nmrp-flash [OPTIONS...]\n"
31                         "\n"
32                         "Options (-a, -i and -f are mandatory):\n"
33                         " -a <ipaddr>     IP address to assign to target device\n"
34                         " -f <firmware>   Firmware file\n"
35                         " -i <interface>  Network interface directly connected to device\n"
36                         " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
37                         " -M <netmask>    Subnet mask to assign to target device\n"
38                         " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
39                         " -T <timeout>    Time to wait after successfull TFTP upload\n"
40                         " -p <port>       Port to use for TFTP upload\n"
41                         " -v              Be verbose\n"
42                         " -V              Print version and exit\n"
43                         " -L              List network interfaces\n"
44                         " -h              Show this screen\n"
45                         "\n"
46                         "Example:\n"
47                         "\n"
48 #ifndef NMRPFLASH_WINDOWS
49                         "$ sudo nmrp-flash -i eth0 -a 192.168.1.254 -f firmware.bin\n"
50 #else
51                         "C:\\> nmrp-flash.exe -i net0 -a 192.168.1.254 -f firmware.bin\n"
52 #endif
53                         "\n"
54                         "nmrp-flash v%s, Copyright (C) 2016 Joseph C. Lehner\n"
55                         "nmrp-flash is free software, licensed under the GNU GPLv3.\n"
56                         "Source code at https://github.com/jclehner/nmrp-flash\n"
57                         "\n",
58                         NMRPD_VERSION
59           );
60 }
61
62 int main(int argc, char **argv)
63 {
64         int c, val, max;
65         struct nmrpd_args args = {
66                 .rx_timeout = 200,
67                 .ul_timeout = 60000,
68                 .tftpcmd = NULL,
69                 .filename = NULL,
70                 .ipaddr = NULL,
71                 .ipmask = "255.255.255.0",
72                 .intf = NULL,
73                 .mac = "ff:ff:ff:ff:ff:ff",
74                 .op = NMRP_UPLOAD_FW,
75                 .port = 69,
76                 .force_root = 1
77         };
78 #ifdef NMRPFLASH_WINDOWS
79         WSADATA wsa;
80
81         val = WSAStartup(MAKEWORD(2, 2), &wsa);
82         if (val != 0) {
83                 win_perror2("WSAStartup", val);
84                 return 1;
85         }
86 #endif
87
88         opterr = 0;
89
90         while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLVv")) != -1) {
91                 max = 0x7fffffff;
92                 switch (c) {
93                         case 'a':
94                                 args.ipaddr = optarg;
95                                 break;
96                         case 'f':
97                                 args.filename = optarg;
98                                 break;
99                         case 'i':
100                                 args.intf = optarg;
101                                 break;
102                         case 'm':
103                                 args.mac = optarg;
104                                 break;
105                         case 'M':
106                                 args.ipmask = optarg;
107                                 break;
108                         case 'p':
109                                 max = 0xffff;
110                         case 'T':
111                         case 't':
112                                 val = atoi(optarg);
113                                 if (val <= 0 || val > max) {
114                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
115                                         return 1;
116                                 }
117
118                                 if (c == 'p') {
119                                         args.port = val;
120                                 } else if (c == 't') {
121                                         args.rx_timeout = val;
122                                 } else {
123                                         args.ul_timeout = val;
124                                 }
125
126                                 break;
127                         case 'V':
128                                 printf("nmrp-flash v%s\n", NMRPD_VERSION);
129                                 return 0;
130                         case 'v':
131                                 ++verbosity;
132                                 break;
133                         case 'L':
134                                 return ethsock_list_all();
135                         case 'h':
136                                 usage(stdout);
137                                 return 0;
138                         default:
139                                 usage(stderr);
140                                 return 1;
141                 }
142         }
143
144         if (!args.filename || !args.intf || !args.ipaddr) {
145                 usage(stderr);
146                 return 1;
147         }
148
149 #ifndef NMRPFLASH_WINDOWS
150         if (geteuid() != 0) {
151                 fprintf(stderr, "This program must be run as root!\n");
152                 return 1;
153         }
154 #endif
155
156         val = nmrp_do(&args);
157 #ifdef NMRPFLASH_WINDOWS
158         WSACleanup();
159 #endif
160         return val;
161 }