From 289c0791bb80ae30c988a02652043fdb4ae0f201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 1 Apr 2007 12:10:20 +0000 Subject: [PATCH] Added 'route get' support to the route command (via SIOCGETRT), patch by Hugo Santos. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20495 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/bin/network/route/route.cpp | 55 +++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/bin/network/route/route.cpp b/src/bin/network/route/route.cpp index e9d973a96c..b558a36abe 100644 --- a/src/bin/network/route/route.cpp +++ b/src/bin/network/route/route.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Copyright 2006-2007, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -32,10 +32,10 @@ enum modes { RTM_LIST = 0, RTM_DELETE, RTM_ADD, - + RTM_GET, + // TODO: RTM_CHANGE, - RTM_GET, RTM_FLUSH, }; @@ -295,6 +295,45 @@ add_route(int socket, const char *interface, route_entry &route) } +void +get_route(int socket, route_entry &route) +{ + union { + route_entry request; + uint8 buffer[512]; + }; + + request = route; + + if (ioctl(socket, SIOCGETRT, buffer, sizeof(buffer)) < 0) { + fprintf(stderr, "%s: Could not get route: %s\n", + kProgramName, strerror(errno)); + return; + } + + // find family + const address_family *family = NULL; + for (int32 i = 0; kFamilies[i].family >= 0; i++) { + if (route.destination->sa_family == kFamilies[i].family) { + family = &kFamilies[i]; + break; + } + } + + if (family != NULL) { + printf("%s ", family->address_to_string(request.destination)); + printf("mask %s ", family->address_to_string(request.mask)); + + if (request.flags & RTF_GATEWAY) + printf("gateway %s ", family->address_to_string(request.gateway)); + + printf("source %s\n", family->address_to_string(request.source)); + } else { + printf("unknown family "); + } +} + + // #pragma mark - @@ -322,6 +361,12 @@ main(int argc, char** argv) usage(1); mode = RTM_ADD; + } else if (!strcmp(argv[1], "get")) { + // get route for destination + if (argc < 3) + usage(1); + + mode = RTM_GET; } } @@ -469,6 +514,10 @@ main(int argc, char** argv) } } break; + + case RTM_GET: + get_route(socket, route); + break; } close(socket);