@@ -56,8 +56,9 @@ run_udp_server(fko_srv_options_t *opts, int family)
56
56
int rv = 1 , chk_rm_all = 0 ;
57
57
fd_set sfd_set ;
58
58
struct sockaddr_in saddr , caddr ;
59
+ struct sockaddr_in6 saddr6 , caddr6 ;
59
60
struct timeval tv ;
60
- char sipbuf [MAX_IPV4_STR_LEN ] = {0 };
61
+ char sipbuf [MAX_IPV46_STR_LEN ] = {0 };
61
62
char dgram_msg [MAX_SPA_PACKET_LEN + 1 ] = {0 };
62
63
socklen_t clen ;
63
64
@@ -95,13 +96,29 @@ run_udp_server(fko_srv_options_t *opts, int family)
95
96
}
96
97
97
98
/* Construct local address structure */
98
- memset (& saddr , 0x0 , sizeof (saddr ));
99
- saddr .sin_family = family ; /* Internet address family */
100
- saddr .sin_addr .s_addr = htonl (INADDR_ANY ); /* Any incoming interface */
101
- saddr .sin_port = htons (opts -> udpserv_port ); /* Local port */
99
+ if (family == AF_INET )
100
+ {
101
+ memset (& saddr , 0 , sizeof (saddr ));
102
+ saddr .sin_family = family ; /* Internet address family */
103
+ saddr .sin_addr .s_addr = htonl (INADDR_ANY ); /* Any incoming interface */
104
+ saddr .sin_port = htons (opts -> udpserv_port ); /* Local port */
105
+ } else if (family == AF_INET6 ) {
106
+ memset (& saddr6 , 0 , sizeof (saddr6 ));
107
+ saddr6 .sin6_family = family ; /* Internet address family */
108
+ saddr6 .sin6_addr = in6addr_any ; /* Any incoming interface */
109
+ saddr6 .sin6_port = htons (opts -> udpserv_port ); /* Local port */
110
+ }
111
+ else
112
+ {
113
+ log_msg (LOG_ERR , "run_udp_server: unsupported protocol family (%d)" ,
114
+ family );
115
+ close (s_sock );
116
+ return -1 ;
117
+ }
102
118
103
119
/* Bind to the local address */
104
- if (bind (s_sock , (struct sockaddr * ) & saddr , sizeof (saddr )) < 0 )
120
+ if ((family == AF_INET && bind (s_sock , (struct sockaddr * ) & saddr , sizeof (saddr )) < 0 )
121
+ || (family == AF_INET6 && bind (s_sock , (struct sockaddr * ) & saddr6 , sizeof (saddr6 )) < 0 ))
105
122
{
106
123
log_msg (LOG_ERR , "run_udp_server: bind() failed: %s" ,
107
124
strerror (errno ));
@@ -184,17 +201,27 @@ run_udp_server(fko_srv_options_t *opts, int family)
184
201
185
202
/* If we make it here then there is a datagram to process
186
203
*/
187
- clen = sizeof (caddr );
188
-
189
- pkt_len = recvfrom (s_sock , dgram_msg , MAX_SPA_PACKET_LEN ,
190
- 0 , (struct sockaddr * )& caddr , & clen );
204
+ if (family == AF_INET ) {
205
+ clen = sizeof (caddr );
206
+ pkt_len = recvfrom (s_sock , dgram_msg , MAX_SPA_PACKET_LEN ,
207
+ 0 , (struct sockaddr * )& caddr , & clen );
208
+ }
209
+ else if (family == AF_INET6 )
210
+ {
211
+ clen = sizeof (caddr6 );
212
+ pkt_len = recvfrom (s_sock , dgram_msg , MAX_SPA_PACKET_LEN ,
213
+ 0 , (struct sockaddr * )& caddr6 , & clen );
214
+ }
191
215
192
216
dgram_msg [pkt_len ] = 0x0 ;
193
217
194
218
if (opts -> verbose )
195
219
{
196
- memset (sipbuf , 0x0 , sizeof (sipbuf ));
197
- inet_ntop (family , & (caddr .sin_addr .s_addr ), sipbuf , sizeof (sipbuf ));
220
+ memset (sipbuf , 0 , sizeof (sipbuf ));
221
+ if (family == AF_INET )
222
+ inet_ntop (family , & caddr .sin_addr .s_addr , sipbuf , sizeof (sipbuf ));
223
+ else if (family == AF_INET6 )
224
+ inet_ntop (family , & caddr6 .sin6_addr , sipbuf , sizeof (sipbuf ));
198
225
log_msg (LOG_INFO , "udp_server: Got UDP datagram (%d bytes) from: %s" ,
199
226
pkt_len , sipbuf );
200
227
}
@@ -208,15 +235,26 @@ run_udp_server(fko_srv_options_t *opts, int family)
208
235
strlcpy ((char * )opts -> spa_pkt .packet_data , dgram_msg , pkt_len + 1 );
209
236
opts -> spa_pkt .packet_data_len = pkt_len ;
210
237
opts -> spa_pkt .packet_proto = IPPROTO_UDP ;
211
- opts -> spa_pkt .packet_src_ip = caddr .sin_addr .s_addr ;
212
- opts -> spa_pkt .packet_dst_ip = saddr .sin_addr .s_addr ;
213
- opts -> spa_pkt .packet_src_port = ntohs (caddr .sin_port );
214
- opts -> spa_pkt .packet_dst_port = ntohs (saddr .sin_port );
238
+ opts -> spa_pkt .packet_family = family ;
239
+ if (family == AF_INET )
240
+ {
241
+ opts -> spa_pkt .packet_src_ip = caddr .sin_addr .s_addr ;
242
+ opts -> spa_pkt .packet_dst_ip = saddr .sin_addr .s_addr ;
243
+ opts -> spa_pkt .packet_src_port = ntohs (caddr .sin_port );
244
+ opts -> spa_pkt .packet_dst_port = ntohs (saddr .sin_port );
245
+ }
246
+ else if (family == AF_INET6 )
247
+ {
248
+ opts -> spa_pkt .packet_addr .inet6 .src_ip = caddr6 .sin6_addr ;
249
+ opts -> spa_pkt .packet_addr .inet6 .dst_ip = saddr6 .sin6_addr ;
250
+ opts -> spa_pkt .packet_src_port = ntohs (caddr6 .sin6_port );
251
+ opts -> spa_pkt .packet_dst_port = ntohs (saddr6 .sin6_port );
252
+ }
215
253
216
254
incoming_spa (opts );
217
255
}
218
256
219
- memset (dgram_msg , 0x0 , sizeof (dgram_msg ));
257
+ memset (dgram_msg , 0 , sizeof (dgram_msg ));
220
258
221
259
opts -> packet_ctr += 1 ;
222
260
if (opts -> foreground == 1 && opts -> verbose > 2 )
0 commit comments