a83236af2eee76c9c1603875189840f074efbc25
[packages.git] / net / mini_snmpd / patches / 100-dualstack.patch
1 --- a/globals.c
2 +++ b/globals.c
3 @@ -31,6 +31,8 @@
4  const struct in_addr inaddr_any = { INADDR_ANY };
5  #endif
6  
7 +int g_family = AF_INET;
8 +
9  in_port_t g_udp_port = 161;
10  in_port_t g_tcp_port = 161;
11  int g_timeout = 100;
12 --- a/mini_snmpd.c
13 +++ b/mini_snmpd.c
14 @@ -54,6 +54,10 @@ static void print_help(void)
15         fprintf(stderr, "-d, --disks nnn        set the disks to monitor (/)\n");
16         fprintf(stderr, "-i, --interfaces nnn   set the network interfaces to monitor (lo)\n");
17         fprintf(stderr, "-I, --listen nnn       set the network interface to listen (all)\n");
18 +#ifdef __IPV6__
19 +       fprintf(stderr, "-4, --ipv4             use IPv4 (default)\n");
20 +       fprintf(stderr, "-6, --ipv6             use IPv6\n");
21 +#endif
22         fprintf(stderr, "-t, --timeout nnn      set the timeout for MIB updates (1 second)\n");
23         fprintf(stderr, "-a, --auth             require authentication (thus SNMP version 2c)\n");
24         fprintf(stderr, "-v, --verbose          verbose syslog messages \n");
25 @@ -117,7 +121,7 @@ static void handle_udp_client(void)
26  #endif
27  
28         /* Call the protocol handler which will prepare the response packet */
29 -       inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
30 +       inet_ntop(g_family, &sockaddr.my_sin_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                         straddr, sockaddr.my_sin_port);
34 @@ -132,7 +136,7 @@ static void handle_udp_client(void)
35         /* Send the whole UDP packet to the socket at once */
36         rv = sendto(g_udp_sockfd, g_udp_client.packet, g_udp_client.size,
37                 MSG_DONTWAIT, (struct sockaddr *)&sockaddr, socklen);
38 -       inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
39 +       inet_ntop(g_family, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
40         if (rv == -1) {
41                 lprintf(LOG_WARNING, "could not send packet to UDP client %s:%d: %m\n",
42                         straddr, sockaddr.my_sin_port);
43 @@ -176,7 +180,7 @@ static void handle_tcp_connect(void)
44                 }
45                 tmp_sockaddr.my_sin_addr = client->addr;
46                 tmp_sockaddr.my_sin_port = client->port;
47 -               inet_ntop(my_af_inet, &tmp_sockaddr.my_sin_addr, straddr, sizeof(straddr));
48 +               inet_ntop(g_family, &tmp_sockaddr.my_sin_addr, straddr, sizeof(straddr));
49                 lprintf(LOG_WARNING, "maximum number of %d clients reached, kicking out %s:%d\n",
50                         MAX_NR_CLIENTS, straddr, tmp_sockaddr.my_sin_port);
51                 close(client->sockfd);
52 @@ -190,7 +194,7 @@ static void handle_tcp_connect(void)
53         }
54  
55         /* Now fill out the client control structure values */
56 -       inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
57 +       inet_ntop(g_family, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
58         lprintf(LOG_DEBUG, "connected TCP client %s:%d\n",
59                 straddr, sockaddr.my_sin_port);
60         client->timestamp = time(NULL);
61 @@ -211,7 +215,7 @@ static void handle_tcp_client_write(clie
62         sockaddr.my_sin_addr = client->addr;
63         sockaddr.my_sin_port = client->port;
64         rv = send(client->sockfd, client->packet, client->size, 0);
65 -       inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
66 +       inet_ntop(g_family, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
67         if (rv == -1) {
68                 lprintf(LOG_WARNING, "could not send packet to TCP client %s:%d: %m\n",
69                         straddr, sockaddr.my_sin_port);
70 @@ -246,7 +250,7 @@ static void handle_tcp_client_read(clien
71         sockaddr.my_sin_port = client->port;
72         rv = read(client->sockfd, client->packet + client->size,
73                 sizeof (client->packet) - client->size);
74 -       inet_ntop(my_af_inet, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
75 +       inet_ntop(g_family, &sockaddr.my_sin_addr, straddr, sizeof(straddr));
76         if (rv == -1) {
77                 lprintf(LOG_WARNING, "could not read packet from TCP client %s:%d: %m\n",
78                         straddr, sockaddr.my_sin_port);
79 @@ -304,7 +308,11 @@ static void handle_tcp_client_read(clien
80  
81  int main(int argc, char *argv[])
82  {
83 +#ifdef __IPV6__
84 +       static const char short_options[] = "p:P:c:D:V:L:C:d:i:I:t:T:avl46h";
85 +#else
86         static const char short_options[] = "p:P:c:D:V:L:C:d:i:I:t:T:avlh";
87 +#endif
88         static const struct option long_options[] = {
89                 { "udp-port", 1, 0, 'p' },
90                 { "tcp-port", 1, 0, 'P' },
91 @@ -316,6 +324,10 @@ int main(int argc, char *argv[])
92                 { "disks", 1, 0, 'd' },
93                 { "interfaces", 1, 0, 'i' },
94                 { "listen", 1, 0, 'I' },
95 +#ifdef __IPV6__
96 +               { "ipv4", 0, 0, '4' },
97 +               { "ipv6", 0, 0, '6' },
98 +#endif
99                 { "timeout", 1, 0, 't' },
100                 { "traps", 1, 0, 'T' },
101                 { "auth", 0, 0, 'a' },
102 @@ -396,6 +408,14 @@ int main(int argc, char *argv[])
103                         case 'v':
104                                 g_verbose = 1;
105                                 break;
106 +#ifdef __IPV6__
107 +                       case '4':
108 +                               g_family = AF_INET;
109 +                               break;
110 +                       case '6':
111 +                               g_family = AF_INET6;
112 +                               break;
113 +#endif
114                         case 'l':
115                                 print_version();
116                                 exit(EXIT_ARGS);
117 @@ -435,12 +455,12 @@ int main(int argc, char *argv[])
118  #endif
119  
120         /* Open the server's UDP port and prepare it for listening */
121 -       g_udp_sockfd = socket(my_pf_inet, SOCK_DGRAM, 0);
122 +       g_udp_sockfd = socket((g_family == AF_INET) ? PF_INET : PF_INET6, SOCK_DGRAM, 0);
123         if (g_udp_sockfd == -1) {
124                 lprintf(LOG_ERR, "could not create UDP socket: %m\n");
125                 exit(EXIT_SYSCALL);
126         }
127 -       sockaddr.my_sin_family = my_af_inet;
128 +       sockaddr.my_sin_family = g_family;
129         sockaddr.my_sin_port = htons(g_udp_port);
130         sockaddr.my_sin_addr = my_inaddr_any;
131         socklen = sizeof (sockaddr);
132 @@ -457,7 +477,7 @@ int main(int argc, char *argv[])
133         }
134  
135         /* Open the server's TCP port and prepare it for listening */
136 -       g_tcp_sockfd = socket(my_pf_inet, SOCK_STREAM, 0);
137 +       g_tcp_sockfd = socket((g_family == AF_INET) ? PF_INET : PF_INET6, SOCK_STREAM, 0);
138         if (g_tcp_sockfd == -1) {
139                 lprintf(LOG_ERR, "could not create TCP socket: %m\n");
140                 exit(EXIT_SYSCALL);
141 @@ -474,7 +494,7 @@ int main(int argc, char *argv[])
142                 lprintf(LOG_WARNING, "could not set SO_REUSEADDR on TCP socket: %m\n");
143                 exit(EXIT_SYSCALL);
144         }
145 -       sockaddr.my_sin_family = my_af_inet;
146 +       sockaddr.my_sin_family = g_family;
147         sockaddr.my_sin_port = htons(g_tcp_port);
148         sockaddr.my_sin_addr = my_inaddr_any;
149         socklen = sizeof (sockaddr);
150 --- a/mini_snmpd.h
151 +++ b/mini_snmpd.h
152 @@ -255,6 +255,7 @@ typedef struct demoinfo_s {
153  
154  extern in_port_t g_udp_port;
155  extern in_port_t g_tcp_port;
156 +extern int g_family;
157  extern int g_timeout;
158  extern int g_auth;
159  extern int g_verbose;