Traceroute: use new SIOCGETRT to obtain the source address to be used when
communicating with the destination, patch by Hugo Santos. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20496 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
289c0791bb
commit
f18d4e1e45
|
@ -17,7 +17,7 @@ SubDirCcFlags $(defines) ;
|
|||
SubDirC++Flags $(defines) ;
|
||||
|
||||
BinCommand traceroute :
|
||||
findsaddr-generic.c
|
||||
findsaddr-haiku.c
|
||||
ifaddrlist.c
|
||||
traceroute.c
|
||||
version.c
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright 2007, Haiku Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Hugo Santos <hugosantos@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sockio.h>
|
||||
|
||||
#include "findsaddr.h"
|
||||
// is not self containing...
|
||||
|
||||
|
||||
const char *
|
||||
findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
|
||||
{
|
||||
uint8_t buffer[256];
|
||||
struct route_entry *request = (struct route_entry *)buffer;
|
||||
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0)
|
||||
return "socket() failed";
|
||||
|
||||
memset(request, 0, sizeof(struct route_entry));
|
||||
request->destination = (struct sockaddr *)to;
|
||||
|
||||
if (ioctl(sock, SIOCGETRT, buffer, sizeof(buffer)) < 0) {
|
||||
close(sock);
|
||||
return "ioctl(SIOCGETRT) failed";
|
||||
}
|
||||
|
||||
if (request->source != NULL && request->source->sa_family == AF_INET)
|
||||
memcpy(from, request->source, sizeof(struct sockaddr_in));
|
||||
|
||||
close(sock);
|
||||
|
||||
if (request->source == NULL || request->source->sa_family != AF_INET)
|
||||
return "No address available";
|
||||
|
||||
return NULL;
|
||||
}
|
Loading…
Reference in New Issue