mini_snmpd: Fix parallel build
[openwrt/svn-archive/archive.git] / net / mini_snmpd / patches / 104-ipv6-support.patch
1 --- a/globals.c
2 +++ b/globals.c
3 @@ -27,6 +27,9 @@
4 * Global variables
5 */
6
7 +#ifndef IPV6
8 +const struct in_addr inaddr_any = { INADDR_ANY };
9 +#endif
10 in_port_t g_udp_port = 161;
11 in_port_t g_tcp_port = 161;
12 int g_timeout = 1;
13 --- a/mini_snmpd.c
14 +++ b/mini_snmpd.c
15 @@ -90,9 +90,10 @@ static void handle_signal(int signo)
16
17 static void handle_udp_client(void)
18 {
19 - struct sockaddr_in sockaddr;
20 + struct my_sockaddr_t sockaddr;
21 socklen_t socklen;
22 int rv;
23 + char straddr[my_inet_addrstrlen];
24
25 /* Read the whole UDP packet from the socket at once */
26 socklen = sizeof (sockaddr);
27 @@ -105,22 +106,23 @@ static void handle_udp_client(void)
28 }
29 g_udp_client.timestamp = time(NULL);
30 g_udp_client.sockfd = g_udp_sockfd;
31 - g_udp_client.addr = sockaddr.sin_addr.s_addr;
32 - g_udp_client.port = sockaddr.sin_port;
33 + g_udp_client.addr = sockaddr.my_sin_addr;
34 + g_udp_client.port = sockaddr.my_sin_port;
35 g_udp_client.size = rv;
36 g_udp_client.outgoing = 0;
37 #ifdef DEBUG
38 dump_packet(&g_udp_client);
39 #endif
40 + inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
41
42 /* Call the protocol handler which will prepare the response packet */
43 if (snmp(&g_udp_client) == -1) {
44 lprintf(LOG_WARNING, "could not handle packet from UDP client %s:%d: %m\n",
45 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
46 + straddr, sockaddr.my_sin_port);
47 return;
48 } else if (g_udp_client.size == 0) {
49 lprintf(LOG_WARNING, "could not handle packet from UDP client %s:%d: ignored\n",
50 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
51 + straddr, sockaddr.my_sin_port);
52 return;
53 }
54 g_udp_client.outgoing = 1;
55 @@ -130,11 +132,11 @@ static void handle_udp_client(void)
56 MSG_DONTWAIT, (struct sockaddr *)&sockaddr, socklen);
57 if (rv == -1) {
58 lprintf(LOG_WARNING, "could not send packet to UDP client %s:%d: %m\n",
59 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
60 + straddr, sockaddr.my_sin_port);
61 } else if (rv != g_udp_client.size) {
62 lprintf(LOG_WARNING, "could not send packet to UDP client %s:%d: "
63 - "only %d of %d bytes written\n", inet_ntoa(sockaddr.sin_addr),
64 - sockaddr.sin_port, rv, (int) g_udp_client.size);
65 + "only %d of %d bytes written\n", straddr,
66 + sockaddr.my_sin_port, rv, (int) g_udp_client.size);
67 }
68 #ifdef DEBUG
69 dump_packet(&g_udp_client);
70 @@ -143,11 +145,12 @@ static void handle_udp_client(void)
71
72 static void handle_tcp_connect(void)
73 {
74 - struct sockaddr_in tmp_sockaddr;
75 - struct sockaddr_in sockaddr;
76 + struct my_sockaddr_t tmp_sockaddr;
77 + struct my_sockaddr_t sockaddr;
78 socklen_t socklen;
79 client_t *client;
80 int rv;
81 + char straddr[my_inet_addrstrlen];
82
83 /* Accept the new connection (remember the client's IP address and port) */
84 socklen = sizeof (sockaddr);
85 @@ -168,10 +171,11 @@ static void handle_tcp_connect(void)
86 lprintf(LOG_ERR, "could not accept TCP connection: internal error");
87 exit(EXIT_SYSCALL);
88 }
89 - tmp_sockaddr.sin_addr.s_addr = client->addr;
90 - tmp_sockaddr.sin_port = client->port;
91 + tmp_sockaddr.my_sin_addr = client->addr;
92 + tmp_sockaddr.my_sin_port = client->port;
93 + inet_ntop(my_af_inet, &tmp_sockaddr.my_sin_addr, straddr, sizeof(straddr));
94 lprintf(LOG_WARNING, "maximum number of %d clients reached, kicking out %s:%d\n",
95 - MAX_NR_CLIENTS, inet_ntoa(tmp_sockaddr.sin_addr), tmp_sockaddr.sin_port);
96 + MAX_NR_CLIENTS, straddr, tmp_sockaddr.my_sin_port);
97 close(client->sockfd);
98 } else {
99 client = malloc(sizeof (client_t));
100 @@ -183,35 +187,38 @@ static void handle_tcp_connect(void)
101 }
102
103 /* Now fill out the client control structure values */
104 + inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
105 lprintf(LOG_DEBUG, "connected TCP client %s:%d\n",
106 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
107 + straddr, sockaddr.my_sin_port);
108 client->timestamp = time(NULL);
109 client->sockfd = rv;
110 - client->addr = sockaddr.sin_addr.s_addr;
111 - client->port = sockaddr.sin_port;
112 + client->addr = sockaddr.my_sin_addr;
113 + client->port = sockaddr.my_sin_port;
114 client->size = 0;
115 client->outgoing = 0;
116 }
117
118 static void handle_tcp_client_write(client_t *client)
119 {
120 - struct sockaddr_in sockaddr;
121 + struct my_sockaddr_t sockaddr;
122 int rv;
123 + char straddr[my_inet_addrstrlen];
124
125 /* Send the packet atomically and close socket if that did not work */
126 - sockaddr.sin_addr.s_addr = client->addr;
127 - sockaddr.sin_port = client->port;
128 + sockaddr.my_sin_addr = client->addr;
129 + sockaddr.my_sin_port = client->port;
130 rv = send(client->sockfd, client->packet, client->size, 0);
131 + inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
132 if (rv == -1) {
133 lprintf(LOG_WARNING, "could not send packet to TCP client %s:%d: %m\n",
134 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
135 + straddr, sockaddr.my_sin_port);
136 close(client->sockfd);
137 client->sockfd = -1;
138 return;
139 } else if (rv != client->size) {
140 lprintf(LOG_WARNING, "could not send packet to TCP client %s:%d: "
141 - "only %d of %d bytes written\n", inet_ntoa(sockaddr.sin_addr),
142 - sockaddr.sin_port, rv, (int) client->size);
143 + "only %d of %d bytes written\n", straddr,
144 + sockaddr.my_sin_port, rv, (int) client->size);
145 close(client->sockfd);
146 client->sockfd = -1;
147 return;
148 @@ -227,23 +234,25 @@ static void handle_tcp_client_write(clie
149
150 static void handle_tcp_client_read(client_t *client)
151 {
152 - struct sockaddr_in sockaddr;
153 + struct my_sockaddr_t sockaddr;
154 int rv;
155 + char straddr[my_inet_addrstrlen];
156
157 /* Read from the socket what arrived and put it into the buffer */
158 - sockaddr.sin_addr.s_addr = client->addr;
159 - sockaddr.sin_port = client->port;
160 + sockaddr.my_sin_addr = client->addr;
161 + sockaddr.my_sin_port = client->port;
162 rv = read(client->sockfd, client->packet + client->size,
163 sizeof (client->packet) - client->size);
164 + inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
165 if (rv == -1) {
166 lprintf(LOG_WARNING, "could not read packet from TCP client %s:%d: %m\n",
167 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
168 + straddr, sockaddr.my_sin_port);
169 close(client->sockfd);
170 client->sockfd = -1;
171 return;
172 } else if (rv == 0) {
173 lprintf(LOG_DEBUG, "disconnected TCP client %s:%d\n",
174 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
175 + straddr, sockaddr.my_sin_port);
176 close(client->sockfd);
177 client->sockfd = -1;
178 return;
179 @@ -255,7 +264,7 @@ static void handle_tcp_client_read(clien
180 rv = snmp_packet_complete(client);
181 if (rv == -1) {
182 lprintf(LOG_WARNING, "could not handle packet from TCP client %s:%d: %m\n",
183 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
184 + straddr, sockaddr.my_sin_port);
185 close(client->sockfd);
186 client->sockfd = -1;
187 return;
188 @@ -270,13 +279,13 @@ static void handle_tcp_client_read(clien
189 /* Call the protocol handler which will prepare the response packet */
190 if (snmp(client) == -1) {
191 lprintf(LOG_WARNING, "could not handle packet from TCP client %s:%d: %m\n",
192 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
193 + straddr, sockaddr.my_sin_port);
194 close(client->sockfd);
195 client->sockfd = -1;
196 return;
197 } else if (client->size == 0) {
198 lprintf(LOG_WARNING, "could not handle packet from TCP client %s:%d: ignored\n",
199 - inet_ntoa(sockaddr.sin_addr), sockaddr.sin_port);
200 + straddr, sockaddr.my_sin_port);
201 close(client->sockfd);
202 client->sockfd = -1;
203 return;
204 @@ -313,7 +322,7 @@ int main(int argc, char *argv[])
205 int option_index = 1;
206 int c;
207
208 - struct sockaddr_in sockaddr;
209 + struct my_sockaddr_t sockaddr;
210 socklen_t socklen;
211 fd_set rfds;
212 fd_set wfds;
213 @@ -399,14 +408,14 @@ int main(int argc, char *argv[])
214 #endif
215
216 /* Open the server's UDP port and prepare it for listening */
217 - g_udp_sockfd = socket(PF_INET, SOCK_DGRAM, 0);
218 + g_udp_sockfd = socket(my_pf_inet, SOCK_DGRAM, 0);
219 if (g_udp_sockfd == -1) {
220 lprintf(LOG_ERR, "could not create UDP socket: %m\n");
221 exit(EXIT_SYSCALL);
222 }
223 - sockaddr.sin_family = AF_INET;
224 - sockaddr.sin_port = htons(g_udp_port);
225 - sockaddr.sin_addr.s_addr = INADDR_ANY;
226 + sockaddr.my_sin_family = my_af_inet;
227 + sockaddr.my_sin_port = htons(g_udp_port);
228 + sockaddr.my_sin_addr = my_inaddr_any;
229 socklen = sizeof (sockaddr);
230 if (bind(g_udp_sockfd, (struct sockaddr *)&sockaddr, socklen) == -1) {
231 lprintf(LOG_ERR, "could not bind UDP socket to port %d: %m\n", g_udp_port);
232 @@ -414,7 +423,7 @@ int main(int argc, char *argv[])
233 }
234
235 /* Open the server's TCP port and prepare it for listening */
236 - g_tcp_sockfd = socket(PF_INET, SOCK_STREAM, 0);
237 + g_tcp_sockfd = socket(my_pf_inet, SOCK_STREAM, 0);
238 if (g_tcp_sockfd == -1) {
239 lprintf(LOG_ERR, "could not create TCP socket: %m\n");
240 exit(EXIT_SYSCALL);
241 @@ -424,9 +433,9 @@ int main(int argc, char *argv[])
242 lprintf(LOG_WARNING, "could not set SO_REUSEADDR on TCP socket: %m\n");
243 exit(EXIT_SYSCALL);
244 }
245 - sockaddr.sin_family = AF_INET;
246 - sockaddr.sin_port = htons(g_tcp_port);
247 - sockaddr.sin_addr.s_addr = INADDR_ANY;
248 + sockaddr.my_sin_family = my_af_inet;
249 + sockaddr.my_sin_port = htons(g_tcp_port);
250 + sockaddr.my_sin_addr = my_inaddr_any;
251 socklen = sizeof (sockaddr);
252 if (bind(g_tcp_sockfd, (struct sockaddr *)&sockaddr, socklen) == -1) {
253 lprintf(LOG_ERR, "could not bind TCP socket to port %d: %m\n", g_tcp_port);
254 --- a/mini_snmpd.h
255 +++ b/mini_snmpd.h
256 @@ -120,6 +120,27 @@
257 } while (0)
258 #endif
259
260 +#ifdef IPV6
261 +#define my_sockaddr_t sockaddr_in6
262 +#define my_sin_addr sin6_addr
263 +#define my_sin_port sin6_port
264 +#define my_sin_family sin6_family
265 +#define my_af_inet AF_INET6
266 +#define my_pf_inet PF_INET6
267 +#define my_in_addr_t in6_addr
268 +#define my_inaddr_any in6addr_any
269 +#define my_inet_addrstrlen INET6_ADDRSTRLEN
270 +#else
271 +#define my_sockaddr_t sockaddr_in
272 +#define my_sin_addr sin_addr
273 +#define my_sin_port sin_port
274 +#define my_sin_family sin_family
275 +#define my_af_inet AF_INET
276 +#define my_pf_inet PF_INET
277 +#define my_in_addr_t in_addr
278 +#define my_inaddr_any inaddr_any
279 +#define my_inet_addrstrlen INET_ADDRSTRLEN
280 +#endif
281
282
283 /* -----------------------------------------------------------------------------
284 @@ -129,7 +150,7 @@
285 typedef struct client_s {
286 time_t timestamp;
287 int sockfd;
288 - in_addr_t addr;
289 + struct my_in_addr_t addr;
290 in_port_t port;
291 unsigned char packet[MAX_PACKET_SIZE];
292 size_t size;
293 @@ -236,6 +257,9 @@ extern char *g_description;
294 extern char *g_vendor;
295 extern char *g_location;
296 extern char *g_contact;
297 +#ifndef IPV6
298 +extern const struct in_addr inaddr_any;
299 +#endif
300 extern char *g_disk_list[MAX_NR_DISKS];
301 extern int g_disk_list_length;
302 extern char *g_interface_list[MAX_NR_INTERFACES];
303 --- a/utils.c
304 +++ b/utils.c
305 @@ -91,12 +91,13 @@ void read_values(const char *buffer, con
306
307 void dump_packet(const client_t *client)
308 {
309 - struct in_addr client_addr;
310 + struct my_in_addr_t client_addr;
311 + char straddr[my_inet_addrstrlen];
312 char buffer[BUFSIZ];
313 int len;
314 int i;
315
316 - client_addr.s_addr = client->addr;
317 + client_addr = client->addr;
318 len = 0;
319 for (i = 0; i < client->size; i++) {
320 len += snprintf(buffer + len, sizeof (buffer) - len,
321 @@ -105,9 +106,10 @@ void dump_packet(const client_t *client)
322 break;
323 }
324 }
325 + inet_ntop(my_af_inet, &client_addr, straddr, sizeof(straddr));
326 lprintf(LOG_DEBUG, "%s %u bytes %s %s:%d (%s)\n",
327 client->outgoing ? "transmitted" : "received", (int) client->size,
328 - client->outgoing ? "to" : "from", inet_ntoa(client_addr),
329 + client->outgoing ? "to" : "from", straddr,
330 ntohs(client->port), buffer);
331 }
332