1 /**
2  * nmrpflash - Netgear Unbrick Utility
3  * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
4  *
5  * nmrpflash 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  * nmrpflash 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 nmrpflash.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef NMRPD_H
21 #define NMRPD_H
22 #include <stdint.h>
23 #include <signal.h>
24 #include <stdbool.h>
25 
26 #if defined(_WIN32) || defined(_WIN64)
27 #	define NMRPFLASH_WINDOWS
28 #elif defined(__APPLE__) && defined(__MACH__)
29 #	define NMRPFLASH_UNIX
30 #	define NMRPFLASH_OSX
31 #	define NMRPFLASH_BSD
32 #elif defined (__unix__)
33 #	define NMRPFLASH_UNIX
34 #	if defined(__linux__)
35 #		define NMRPFLASH_LINUX
36 #	elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
37 #		define NMRPFLASH_BSD
38 #	else
39 #		warning "nmrpflash is not fully supported on this platform"
40 #	endif
41 #else
42 #	warning "nmrpflash is not supported on this platform"
43 #endif
44 
45 #ifndef NMRPFLASH_WINDOWS
46 #include <arpa/inet.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <net/if.h>
51 #ifndef NMRPFLASH_LINUX
52 #include <net/if_dl.h>
53 #endif
54 #else
55 #include <winsock2.h>
56 #include <iphlpapi.h>
57 #include <ws2tcpip.h>
58 #include <windows.h>
59 #include <conio.h>
60 #endif
61 
62 #ifndef MIN
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #endif
65 
66 #ifndef MAX
67 #define MAX(a, b) ((a) > (b) ? (a) : (b))
68 #endif
69 
70 #ifndef PACKED
71 #define PACKED __attribute__((packed))
72 #endif
73 
74 #define NMRPFLASH_SET_REGION
75 
76 struct eth_hdr {
77 	uint8_t ether_dhost[6];
78 	uint8_t ether_shost[6];
79 	uint16_t ether_type;
80 } PACKED;
81 
82 enum nmrp_op {
83 	NMRP_UPLOAD_FW = 0,
84 	NMRP_UPLOAD_ST = 1,
85 	NMRP_SET_REGION = 2,
86 };
87 
88 struct nmrpd_args {
89 	unsigned rx_timeout;
90 	unsigned ul_timeout;
91 	const char *tftpcmd;
92 	const char *file_local;
93 	const char *file_remote;
94 	const char *ipaddr_intf;
95 	const char *ipaddr;
96 	const char *ipmask;
97 	const char *intf;
98 	const char *mac;
99 	enum nmrp_op op;
100 	bool blind;
101 	uint16_t port;
102 	const char *region;
103 	off_t offset;
104 };
105 
106 const char *leafname(const char *path);
107 int tftp_put(struct nmrpd_args *args);
108 bool tftp_is_valid_filename(const char *filename);
109 
110 int nmrp_do(struct nmrpd_args *args);
111 
112 int select_fd(int fd, unsigned timeout);
113 const char *mac_to_str(uint8_t *mac);
114 
115 #ifdef NMRPFLASH_WINDOWS
116 void win_perror2(const char *msg, DWORD err);
117 void sock_perror(const char *msg);
118 #else
119 #define sock_perror(x) xperror(x)
120 #endif
121 
122 extern int verbosity;
123 
124 struct ethsock;
125 struct ethsock_arp_undo;
126 struct ethsock_ip_undo;
127 
128 struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
129 bool ethsock_is_unplugged(struct ethsock *sock);
130 int ethsock_close(struct ethsock *sock);
131 int ethsock_send(struct ethsock *sock, void *buf, size_t len);
132 ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
133 int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
134 uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
135 int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
136 int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
137 int ethsock_list_all(void);
138 
139 struct ethsock_ip_callback_args
140 {
141 	struct in_addr *ipaddr;
142 	struct in_addr *ipmask;
143 	void *arg;
144 };
145 
146 typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
147 int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
148 		void *arg);
149 
150 int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
151 int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
152 
153 time_t time_monotonic();
154 char *lltostr(long long ll, int base);
155 uint32_t bitcount(uint32_t n);
156 uint32_t netmask(uint32_t count);
157 void xperror(const char *msg);
158 
159 extern volatile sig_atomic_t g_interrupted;
160 #endif
161