The beginnings of automatic configuration of network devices using DHCP; this is
currently only triggered when there is no configuration file - it can't be configured this way yet. All DHCP currently does is to send a UDP broadcast DHCP discover message. More to come. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19437 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2006, Haiku, Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Axel Dörfler, [email protected]
|
||||
*/
|
||||
|
||||
|
||||
#include "AutoconfigLooper.h"
|
||||
#include "DHCPClient.h"
|
||||
#include "NetServer.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if_types.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sockio.h>
|
||||
|
||||
|
||||
static const uint32 kMsgReadyToRun = 'rdyr';
|
||||
|
||||
|
||||
AutoconfigLooper::AutoconfigLooper(BMessenger target, const char* device)
|
||||
: BLooper(device),
|
||||
fTarget(target),
|
||||
fDevice(device)
|
||||
{
|
||||
BMessage ready(kMsgReadyToRun);
|
||||
PostMessage(&ready);
|
||||
}
|
||||
|
||||
|
||||
AutoconfigLooper::~AutoconfigLooper()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AutoconfigLooper::_ReadyToRun()
|
||||
{
|
||||
// start with DHCP
|
||||
|
||||
DHCPClient* client = new DHCPClient(fDevice.String());
|
||||
if (client->InitCheck() == B_OK) {
|
||||
AddHandler(client);
|
||||
return;
|
||||
}
|
||||
puts("DHCP failed miserably!");
|
||||
|
||||
// DHCP obviously didn't work out, take some default values for now
|
||||
// TODO: have a look at zeroconf
|
||||
// TODO: this could also be done add-on based
|
||||
|
||||
BMessage interface;
|
||||
interface.AddString("device", fDevice.String());
|
||||
|
||||
uint8 mac[6];
|
||||
uint8 last = 56;
|
||||
if (get_mac_address(fDevice.String(), mac) == B_OK) {
|
||||
// choose IP address depending on the MAC address, if available
|
||||
last = mac[0] ^ mac[1] ^ mac[2] ^ mac[3] ^ mac[4] ^ mac[5];
|
||||
if (last > 253)
|
||||
last = 253;
|
||||
else if (last == 0)
|
||||
last = 1;
|
||||
}
|
||||
|
||||
char string[64];
|
||||
snprintf(string, sizeof(string), "192.168.0.%u", last);
|
||||
|
||||
BMessage address;
|
||||
address.AddString("family", "inet");
|
||||
address.AddString("address", string);
|
||||
address.AddString("gateway", "192.168.0.254");
|
||||
interface.AddMessage("address", &address);
|
||||
|
||||
fTarget.SendMessage(kMsgConfigureInterface, &interface);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AutoconfigLooper::MessageReceived(BMessage* message)
|
||||
{
|
||||
switch (message->what) {
|
||||
case kMsgReadyToRun:
|
||||
_ReadyToRun();
|
||||
break;
|
||||
|
||||
default:
|
||||
BLooper::MessageReceived(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user