| 346 | |
| 347 | |
| 348 | void protect_socket_binddev(int fd, const std::string &remote, bool ipv6) |
| 349 | { |
| 350 | std::string bestdev; |
| 351 | |
| 352 | if (ipv6) |
| 353 | { |
| 354 | IPv6::Addr bestgw; |
| 355 | IP::Route6 hostroute(IPv6::Addr::from_string(remote), 128); |
| 356 | if (openvpn::TunNetlink::SITNL::net_route_best_gw(hostroute, bestgw, bestdev) != 0) |
| 357 | { |
| 358 | throw NetCfgException("Failed retrieving IPv6 gateway for " |
| 359 | + remote + " failed"); |
| 360 | } |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | IPv4::Addr bestgw; |
| 365 | IP::Route4 hostroute(IPv4::Addr::from_string(remote), 32); |
| 366 | if (TunNetlink::SITNL::net_route_best_gw(hostroute, bestgw, bestdev) != 0) |
| 367 | { |
| 368 | throw NetCfgException("Failed retrieving IPv4 gateway for " |
| 369 | + remote + " failed"); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | OPENVPN_LOG("Protecting socket " + std::to_string(fd) |
| 374 | + " to '" + remote + "'(" + (ipv6 ? "inet6" : "inet") |
| 375 | + ") by binding it to '" + bestdev + "'"); |
| 376 | |
| 377 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)bestdev.c_str(), strlen(bestdev.c_str())) < 0) |
| 378 | { |
| 379 | throw NetCfgException("Failed binding (SO_BINDTODEVICE) to " |
| 380 | + bestdev + " failed: " + strerror(errno)); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
| 385 |
no test coverage detected