Don't require user to specify -a anymore
[oweals/nmrpflash.git] / nmrpd.h
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 <stdbool.h>
24
25 #if defined(_WIN32) || defined(_WIN64)
26 #define NMRPFLASH_WINDOWS
27 #elif defined(__linux__)
28 #define NMRPFLASH_LINUX
29 #elif defined(__APPLE__) && defined(__MACH__)
30 #define NMRPFLASH_OSX
31 #elif defined(__unix__)
32 #define NMRPFLASH_UNIX
33 #warning "nmrpflash is not fully supported on your operating system"
34 #endif
35
36 #ifndef NMRPFLASH_WINDOWS
37 #include <arpa/inet.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <net/if.h>
42 #ifndef NMRPFLASH_LINUX
43 #include <net/if_dl.h>
44 #endif
45 #else
46 #include <winsock2.h>
47 #include <iphlpapi.h>
48 #include <ws2tcpip.h>
49 #include <windows.h>
50 #endif
51
52 #ifndef MIN
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #endif
55
56 #ifndef PACKED
57 #define PACKED __attribute__((packed))
58 #endif
59
60 struct eth_hdr {
61         uint8_t ether_dhost[6];
62         uint8_t ether_shost[6];
63         uint16_t ether_type;
64 } PACKED;
65
66 enum nmrp_op {
67         NMRP_UPLOAD_FW = 0,
68         NMRP_UPLOAD_ST = 1,
69         NMRP_SET_REGION = 2,
70 };
71
72 struct nmrpd_args {
73         unsigned rx_timeout;
74         unsigned ul_timeout;
75         const char *tftpcmd;
76         const char *file_local;
77         const char *file_remote;
78         const char *ipaddr_intf;
79         const char *ipaddr;
80         const char *ipmask;
81         const char *intf;
82         const char *mac;
83         enum nmrp_op op;
84         uint16_t port;
85         const char *region;
86 };
87
88 const char *leafname(const char *path);
89 int tftp_put(struct nmrpd_args *args);
90 bool tftp_is_valid_filename(const char *filename);
91
92 int nmrp_do(struct nmrpd_args *args);
93
94 int select_fd(int fd, unsigned timeout);
95 const char *mac_to_str(uint8_t *mac);
96
97 #ifdef NMRPFLASH_WINDOWS
98 void win_perror2(const char *msg, DWORD err);
99 void sock_perror(const char *msg);
100 #else
101 #define sock_perror(x) perror(x)
102 #endif
103
104 extern int verbosity;
105
106 struct ethsock;
107 struct ethsock_ip_undo;
108
109 struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
110 int ethsock_close(struct ethsock *sock);
111 int ethsock_send(struct ethsock *sock, void *buf, size_t len);
112 ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
113 int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
114 uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
115 int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr);
116 int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr);
117 int ethsock_list_all(void);
118
119 struct ethsock_ip_callback_args
120 {
121         struct in_addr *ipaddr;
122         struct in_addr *ipmask;
123         void *arg;
124 };
125
126 typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
127 int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
128                 void *arg);
129
130 int ethsock_set_ip(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
131 int ethsock_del_ip(struct ethsock *sock, struct ethsock_ip_undo **undo);
132 #endif