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