* Replaced parse_ip_address_component() with strtol() which is already

available in the boot loader.
* Simplified parse_ip_address() and use style conforming identifiers.
* Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34870 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2010-01-03 13:33:20 +00:00
parent 65a4a2c2a4
commit e0d09e23a7
@@ -3,6 +3,7 @@
* All rights reserved. Distributed under the terms of the MIT License. * All rights reserved. Distributed under the terms of the MIT License.
*/ */
#include <new> #include <new>
#include <stdio.h> #include <stdio.h>
@@ -25,6 +26,27 @@
#endif #endif
class OFEthernetInterface : public EthernetInterface {
public:
OFEthernetInterface();
virtual ~OFEthernetInterface();
status_t Init(const char *device);
virtual mac_addr_t MACAddress() const;
virtual void *AllocateSendReceiveBuffer(size_t size);
virtual void FreeSendReceiveBuffer(void *buffer);
virtual ssize_t Send(const void *buffer, size_t size);
virtual ssize_t Receive(void *buffer, size_t size);
private:
int fHandle;
mac_addr_t fMACAddress;
};
#ifdef TRACE_NETWORK #ifdef TRACE_NETWORK
static void static void
@@ -53,77 +75,47 @@ hex_dump(const void *_data, int length)
#endif // !TRACE_NETWORK #endif // !TRACE_NETWORK
static int
parse_ip_address_component(const char *sz, int len)
{
const int base = 10;
// only decimal numbers for now
int r = 0;
for (int i = 0; i < len; i++) {
int x = sz[i] - '0';
for (int j = 1; j < len - i; j++)
x *= base;
r += x;
}
return r;
}
static ip_addr_t static ip_addr_t
parse_ip_address(const char *sz) parse_ip_address(const char *string)
{ {
ip_addr_t addr = 0; ip_addr_t address = 0;
int i = 0; int components = 0;
const char *psz = sz;
// TODO: Handles only IPv4 addresses for now. // TODO: Handles only IPv4 addresses for now.
char *p = strchr(psz, '.'); while (components < 4) {
while (p != NULL && i < 4) { address |= strtol(string, NULL, 0) << ((4 - components - 1) * 8);
int len = p - psz;
addr |= parse_ip_address_component(psz, len) << ((4 - i - 1) * 8); const char *dot = strchr(string, '.');
p = strchr(psz = p + 1, '.'); if (dot == NULL)
i++; break;
string = dot + 1;
components++;
} }
addr |= parse_ip_address_component(psz, sz + strlen(sz) - psz);
return addr; return address;
} }
class OFEthernetInterface : public EthernetInterface { // #pragma mark -
public:
OFEthernetInterface();
virtual ~OFEthernetInterface();
status_t Init(const char *device);
virtual mac_addr_t MACAddress() const;
virtual void *AllocateSendReceiveBuffer(size_t size);
virtual void FreeSendReceiveBuffer(void *buffer);
virtual ssize_t Send(const void *buffer, size_t size);
virtual ssize_t Receive(void *buffer, size_t size);
private:
int fHandle;
mac_addr_t fMACAddress;
};
// constructor
OFEthernetInterface::OFEthernetInterface() OFEthernetInterface::OFEthernetInterface()
: EthernetInterface(), :
fHandle(OF_FAILED), EthernetInterface(),
fMACAddress(kNoMACAddress) fHandle(OF_FAILED),
fMACAddress(kNoMACAddress)
{ {
} }
// destructor
OFEthernetInterface::~OFEthernetInterface() OFEthernetInterface::~OFEthernetInterface()
{ {
if (fHandle != OF_FAILED) if (fHandle != OF_FAILED)
of_close(fHandle); of_close(fHandle);
} }
// Init
status_t status_t
OFEthernetInterface::Init(const char *device) OFEthernetInterface::Init(const char *device)
{ {
@@ -154,9 +146,10 @@ OFEthernetInterface::Init(const char *device)
} }
// get IP address // get IP address
// Note: This is a non-standardized way. On my Mac mini the response of the // Note: This is a non-standardized way. On my Mac mini the response of the
// DHCP server is stored as property of /chosen. We try to get that it and // DHCP server is stored as property of /chosen. We try to get it and use
// use the IP address we find in there. // the IP address we find in there.
struct { struct {
uint8 irrelevant[16]; uint8 irrelevant[16];
uint32 ip_address; uint32 ip_address;
@@ -169,7 +162,8 @@ OFEthernetInterface::Init(const char *device)
} else { } else {
char saddr[16]; char saddr[16];
package = of_finddevice("/options"); package = of_finddevice("/options");
bytesRead = of_getprop(package, "default-client-ip", saddr, sizeof(saddr)); bytesRead = of_getprop(package, "default-client-ip", saddr,
sizeof(saddr));
if (bytesRead != OF_FAILED) { if (bytesRead != OF_FAILED) {
saddr[bytesRead] = '\0'; saddr[bytesRead] = '\0';
printf("default-client-ip: %s\n", saddr); printf("default-client-ip: %s\n", saddr);
@@ -182,14 +176,14 @@ OFEthernetInterface::Init(const char *device)
return B_OK; return B_OK;
} }
// MACAddress
mac_addr_t mac_addr_t
OFEthernetInterface::MACAddress() const OFEthernetInterface::MACAddress() const
{ {
return fMACAddress; return fMACAddress;
} }
// AllocateSendReceiveBuffer
void * void *
OFEthernetInterface::AllocateSendReceiveBuffer(size_t size) OFEthernetInterface::AllocateSendReceiveBuffer(size_t size)
{ {
@@ -201,7 +195,7 @@ OFEthernetInterface::AllocateSendReceiveBuffer(size_t size)
return dmaMemory; return dmaMemory;
} }
// FreeSendReceiveBuffer
void void
OFEthernetInterface::FreeSendReceiveBuffer(void *buffer) OFEthernetInterface::FreeSendReceiveBuffer(void *buffer)
{ {
@@ -209,7 +203,7 @@ OFEthernetInterface::FreeSendReceiveBuffer(void *buffer)
of_call_method(fHandle, "dma-free", 1, 0, buffer); of_call_method(fHandle, "dma-free", 1, 0, buffer);
} }
// Send
ssize_t ssize_t
OFEthernetInterface::Send(const void *buffer, size_t size) OFEthernetInterface::Send(const void *buffer, size_t size)
{ {
@@ -224,7 +218,7 @@ OFEthernetInterface::Send(const void *buffer, size_t size)
return (result == OF_FAILED ? B_ERROR : result); return (result == OF_FAILED ? B_ERROR : result);
} }
// Receive
ssize_t ssize_t
OFEthernetInterface::Receive(void *buffer, size_t size) OFEthernetInterface::Receive(void *buffer, size_t size)
{ {
@@ -245,7 +239,7 @@ OFEthernetInterface::Receive(void *buffer, size_t size)
// #pragma mark - // #pragma mark -
// platform_net_stack_init
status_t status_t
platform_net_stack_init() platform_net_stack_init()
{ {