Re: NAT-PMP Broadcast Address?
Re: NAT-PMP Broadcast Address?
- Subject: Re: NAT-PMP Broadcast Address?
- From: Matt Slot <email@hidden>
- Date: Mon, 10 Sep 2007 14:35:55 -0400
On Sep 10, 2007, at 1:56 PM, email@hidden wrote:
I am just wondering if I am missing something, like maybe I have to
send a special discovery packet to get the address of the router,
and then pass that to NAT-PMP so it can query the router address
directly. Or, do I look in the system somewhere for the router
address as seen in system prefs?
Here's the code I use to discover the address of my upstream router
using BSD.
#include <sys/socket.h>
#include <sys/sockio.h>
#include <net/route.h>
#include <netinet/in.h>
static int seq = 0;
unsigned long remote_ip = htonl(0x80000001); /* 128.0.0.1 */
unsigned long gateway_ip = 0;
int s = 0, bytes;
unsigned char buffer[1024];
struct rt_msghdr *rtm;
struct sockaddr_in *sa, *gw;
struct sockaddr_in address;
pid_t pid = getpid();
/* Create a route control socket to query against */
REQUIRE(s = socket(PF_ROUTE, SOCK_RAW, AF_INET), "Failed to open
control socket");
bzero(buffer, sizeof(buffer));
rtm = (struct rt_msghdr *) buffer;
rtm->rtm_version = RTM_VERSION;
rtm->rtm_type = RTM_GET;
rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY;
rtm->rtm_seq = ++seq;
sa = (struct sockaddr_in *) (rtm + 1);
sa->sin_len = sizeof(struct sockaddr_in);
sa->sin_family = AF_INET;
sa->sin_addr.s_addr = remote_ip;
sa = sa + 1;
gw = sa;
sa = sa + 1;
rtm->rtm_msglen = (unsigned char *) sa - buffer;
REQUIRE(write(s, buffer, rtm->rtm_msglen) < 0,
"Unable to write to control socket");
while((bytes = read(s, buffer, sizeof(buffer))) > 0)
{
REQUIRE(bytes >= sizeof(struct rt_msghdr),
"Invalid control response size");
if (rtm->rtm_type != RTM_GET) continue;
if (rtm->rtm_pid != pid) continue;
if (rtm->rtm_seq != seq) continue;
REQUIRE(rtm->rtm_errno == 0,
"Control request returned an error");
break;
}
gateway_ip = gw->sin_addr.s_addr;
close(s);
For OpenTransport, OTInetGetInterfaceInfo() will do the trick.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc. -- http://
www.AmbrosiaSW.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden