nmrp-flash -> nmrpflash
[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/socket.h>
39 #include <net/if.h>
40 #else
41 #include <winsock2.h>
42 #include <iphlpapi.h>
43 #include <ws2tcpip.h>
44 #include <windows.h>
45 #endif
46
47 enum nmrp_op {
48         NMRP_UPLOAD_FW = 0,
49         NMRP_UPLOAD_ST = 1,
50         NMRP_SET_REGION = 2,
51 };
52
53 struct nmrpd_args {
54         unsigned rx_timeout;
55         unsigned ul_timeout;
56         const char *tftpcmd;
57         const char *filename;
58         const char *ipaddr;
59         const char *ipmask;
60         const char *intf;
61         const char *mac;
62         enum nmrp_op op;
63         uint16_t port;
64         int force_root;
65 };
66
67 int tftp_put(struct nmrpd_args *args);
68 int nmrp_do(struct nmrpd_args *args);
69
70 int select_fd(int fd, unsigned timeout);
71 const char *mac_to_str(uint8_t *mac);
72
73 #ifdef NMRPFLASH_WINDOWS
74 void win_perror2(const char *msg, DWORD err);
75 void sock_perror(const char *msg);
76 #else
77 #define sock_perror(x) perror(x)
78 #endif
79
80 extern int verbosity;
81
82 struct ethsock;
83
84 struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
85 int ethsock_close(struct ethsock *sock);
86 int ethsock_send(struct ethsock *sock, void *buf, size_t len);
87 ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
88 int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
89 uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
90 int ethsock_list_all(void);
91
92 #endif