2 * nmrpflash - Netgear Unbrick Utility
3 * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
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.
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.
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/>.
26 #if defined(_WIN32) || defined(_WIN64)
27 # define NMRPFLASH_WINDOWS
28 #elif defined(__APPLE__) && defined(__MACH__)
29 # define NMRPFLASH_OSX
30 # define NMRPFLASH_BSD
31 #elif defined (__unix__)
32 # define NMRPFLASH_UNIX
33 # if defined(__linux__)
34 # define NMRPFLASH_LINUX
35 # elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
36 # define NMRPFLASH_BSD
38 # warning "nmrpflash is not fully supported on this platform"
41 # warning "nmrpflash is not supported on this platform"
44 #ifndef NMRPFLASH_WINDOWS
45 #include <arpa/inet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #ifndef NMRPFLASH_LINUX
51 #include <net/if_dl.h>
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
65 #define PACKED __attribute__((packed))
69 uint8_t ether_dhost[6];
70 uint8_t ether_shost[6];
84 const char *file_local;
85 const char *file_remote;
86 const char *ipaddr_intf;
96 const char *leafname(const char *path);
97 int tftp_put(struct nmrpd_args *args);
98 bool tftp_is_valid_filename(const char *filename);
100 int nmrp_do(struct nmrpd_args *args);
102 int select_fd(int fd, unsigned timeout);
103 const char *mac_to_str(uint8_t *mac);
105 #ifdef NMRPFLASH_WINDOWS
106 void win_perror2(const char *msg, DWORD err);
107 void sock_perror(const char *msg);
109 #define sock_perror(x) xperror(x)
112 extern int verbosity;
115 struct ethsock_arp_undo;
116 struct ethsock_ip_undo;
118 struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
119 int ethsock_close(struct ethsock *sock);
120 int ethsock_send(struct ethsock *sock, void *buf, size_t len);
121 ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
122 int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
123 uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
124 int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
125 int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
126 int ethsock_list_all(void);
128 struct ethsock_ip_callback_args
130 struct in_addr *ipaddr;
131 struct in_addr *ipmask;
135 typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
136 int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
139 int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
140 int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
142 time_t time_monotonic();
143 char *lltostr(long long ll, int base);
144 uint32_t bitcount(uint32_t n);
145 uint32_t netmask(uint32_t count);
146 void xperror(const char *msg);
148 extern volatile sig_atomic_t g_interrupted;