Use libnl to add/remove IPs on Linux
[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 <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_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
37 #       else
38 #               warning "nmrpflash is not fully supported on this platform"
39 #       endif
40 #else
41 #       warning "nmrpflash is not supported on this platform"
42 #endif
43
44 #ifndef NMRPFLASH_WINDOWS
45 #include <arpa/inet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <net/if.h>
50 #ifndef NMRPFLASH_LINUX
51 #include <net/if_dl.h>
52 #endif
53 #else
54 #include <winsock2.h>
55 #include <iphlpapi.h>
56 #include <ws2tcpip.h>
57 #include <windows.h>
58 #endif
59
60 #ifndef MIN
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 #endif
63
64 #ifndef PACKED
65 #define PACKED __attribute__((packed))
66 #endif
67
68 #define NMRPFLASH_SET_REGION
69
70 struct eth_hdr {
71         uint8_t ether_dhost[6];
72         uint8_t ether_shost[6];
73         uint16_t ether_type;
74 } PACKED;
75
76 enum nmrp_op {
77         NMRP_UPLOAD_FW = 0,
78         NMRP_UPLOAD_ST = 1,
79         NMRP_SET_REGION = 2,
80 };
81
82 struct nmrpd_args {
83         unsigned rx_timeout;
84         unsigned ul_timeout;
85         const char *tftpcmd;
86         const char *file_local;
87         const char *file_remote;
88         const char *ipaddr_intf;
89         const char *ipaddr;
90         const char *ipmask;
91         const char *intf;
92         const char *mac;
93         enum nmrp_op op;
94         uint16_t port;
95         const char *region;
96 };
97
98 const char *leafname(const char *path);
99 int tftp_put(struct nmrpd_args *args);
100 bool tftp_is_valid_filename(const char *filename);
101
102 int nmrp_do(struct nmrpd_args *args);
103
104 int select_fd(int fd, unsigned timeout);
105 const char *mac_to_str(uint8_t *mac);
106
107 #ifdef NMRPFLASH_WINDOWS
108 void win_perror2(const char *msg, DWORD err);
109 void sock_perror(const char *msg);
110 #else
111 #define sock_perror(x) xperror(x)
112 #endif
113
114 extern int verbosity;
115
116 struct ethsock;
117 struct ethsock_arp_undo;
118 struct ethsock_ip_undo;
119
120 struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
121 int ethsock_close(struct ethsock *sock);
122 int ethsock_send(struct ethsock *sock, void *buf, size_t len);
123 ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
124 int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
125 uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
126 int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
127 int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
128 int ethsock_list_all(void);
129
130 struct ethsock_ip_callback_args
131 {
132         struct in_addr *ipaddr;
133         struct in_addr *ipmask;
134         void *arg;
135 };
136
137 typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
138 int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
139                 void *arg);
140
141 int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
142 int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
143
144 time_t time_monotonic();
145 char *lltostr(long long ll, int base);
146 uint32_t bitcount(uint32_t n);
147 uint32_t netmask(uint32_t count);
148 void xperror(const char *msg);
149
150 extern volatile sig_atomic_t g_interrupted;
151 #endif