3131set_and_get_bool_opt (int socket_fd , int level , int optname , int val )
3232{
3333 int bool_opt = val ;
34+ int ret = -1 ;
3435 socklen_t opt_len = sizeof (bool_opt );
35- setsockopt (socket_fd , level , optname , & bool_opt , sizeof (bool_opt ));
36+
37+ ret = setsockopt (socket_fd , level , optname , & bool_opt , sizeof (bool_opt ));
38+ if (ret != 0 )
39+ return !val ;
40+
3641 bool_opt = !bool_opt ;
37- getsockopt (socket_fd , level , optname , & bool_opt , & opt_len );
42+ ret = getsockopt (socket_fd , level , optname , & bool_opt , & opt_len );
43+ if (ret != 0 )
44+ return !val ;
45+
3846 return bool_opt ;
3947}
4048
@@ -197,11 +205,13 @@ main(int argc, char *argv[])
197205
198206 // IP_TTL
199207 ttl = 8 ;
200- setsockopt (tcp_socket_fd , IPPROTO_IP , IP_TTL , & ttl , sizeof (ttl ));
208+ result = setsockopt (tcp_socket_fd , IPPROTO_IP , IP_TTL , & ttl , sizeof (ttl ));
209+ OPTION_ASSERT (result , 0 , "IP_TIL" );
201210 ttl = 0 ;
202211 opt_len = sizeof (ttl );
203- getsockopt (tcp_socket_fd , IPPROTO_IP , IP_TTL , & ttl , & opt_len );
212+ result = getsockopt (tcp_socket_fd , IPPROTO_IP , IP_TTL , & ttl , & opt_len );
204213 OPTION_ASSERT (ttl , 8 , "IP_TTL" );
214+ OPTION_ASSERT (result , 0 , "IP_TIL" );
205215
206216 // IPV6_V6ONLY
207217 OPTION_ASSERT (
@@ -233,11 +243,15 @@ main(int argc, char *argv[])
233243
234244 // IP_MULTICAST_TTL
235245 ttl = 8 ;
236- setsockopt (udp_socket_fd , IPPROTO_IP , IP_MULTICAST_TTL , & ttl , sizeof (ttl ));
246+ result = setsockopt (udp_socket_fd , IPPROTO_IP , IP_MULTICAST_TTL , & ttl ,
247+ sizeof (ttl ));
248+ OPTION_ASSERT (result , 0 , "IP_MULTICAST_TTL" );
237249 ttl = 0 ;
238250 opt_len = sizeof (ttl );
239- getsockopt (udp_socket_fd , IPPROTO_IP , IP_MULTICAST_TTL , & ttl , & opt_len );
251+ result =
252+ getsockopt (udp_socket_fd , IPPROTO_IP , IP_MULTICAST_TTL , & ttl , & opt_len );
240253 OPTION_ASSERT (ttl , 8 , "IP_MULTICAST_TTL" );
254+ OPTION_ASSERT (result , 0 , "IP_MULTICAST_TTL" );
241255
242256 // IPV6_MULTICAST_LOOP
243257 OPTION_ASSERT (set_and_get_bool_opt (udp_ipv6_socket_fd , IPPROTO_IPV6 ,
@@ -248,12 +262,14 @@ main(int argc, char *argv[])
248262 0 , "IPV6_MULTICAST_LOOP disabled" );
249263
250264 // IPV6_JOIN_GROUP
251- setsockopt (udp_ipv6_socket_fd , IPPROTO_IPV6 , IPV6_JOIN_GROUP , & mcast_ipv6 ,
252- sizeof (mcast_ipv6 ));
265+ result = setsockopt (udp_ipv6_socket_fd , IPPROTO_IPV6 , IPV6_JOIN_GROUP ,
266+ & mcast_ipv6 , sizeof (mcast_ipv6 ));
267+ OPTION_ASSERT (result , 0 , "IPV6_JOIN_GROUP" );
253268
254269 // IPV6_LEAVE_GROUP
255- setsockopt (udp_ipv6_socket_fd , IPPROTO_IPV6 , IPV6_LEAVE_GROUP , & mcast_ipv6 ,
256- sizeof (mcast_ipv6 ));
270+ result = setsockopt (udp_ipv6_socket_fd , IPPROTO_IPV6 , IPV6_LEAVE_GROUP ,
271+ & mcast_ipv6 , sizeof (mcast_ipv6 ));
272+ OPTION_ASSERT (result , 0 , "IPV6_JOIN_GROUP" );
257273
258274 printf ("[Client] Close sockets\n" );
259275 close (tcp_socket_fd );
0 commit comments