Ticket #5189: network-ipaddress.diff

File network-ipaddress.diff, 2.0 KB (added by andreasf, 14 years ago)

patch

  • src/system/boot/platform/openfirmware/network.cpp

    diff --git a/src/system/boot/platform/openfirmware/network.cpp b/src/system/boot/platform/openfirmware/network.cpp
    index f5b23c6..4c236cf 100644
    a b hex_dump(const void *_data, int length)  
    4949
    5050#define hex_dump(data, length)
    5151
    52 #endif  // !TRACE_NETWORK
     52#endif  // !TRACE_NETWORK
     53
     54
     55static int
     56parse_ip_address_component(const char* sz, int len)
     57{
     58    const int base = 10; // only decimal numbers for now
     59    int r = 0;
     60    for (int i = 0; i < len; i++) {
     61        int x = sz[i] - '0';
     62        for (int j = 1; j < len - i; j++)
     63            x *= base;
     64        r += x;
     65    }
     66    return r;
     67}
     68
     69static ip_addr_t
     70parse_ip_address(const char* sz)
     71{
     72    ip_addr_t addr = 0;
     73    int i = 0;
     74    const char *psz = sz;
     75    // handles only IPv4 addresses for now
     76    char *p = strchr(psz, '.');
     77    while (p != NULL && i < 4) {
     78        int len = p - psz;
     79        addr |= parse_ip_address_component(psz, len) << ((4 - i - 1) * 8);
     80        p = strchr(psz = p + 1, '.');
     81        i++;
     82    }
     83    addr |= parse_ip_address_component(psz, sz + strlen(sz) - psz);
     84    return addr;
     85}
    5386
    5487
    5588class OFEthernetInterface : public EthernetInterface {
    OFEthernetInterface::Init(const char *device)  
    128161    } dhcpResponse;
    129162    bytesRead = of_getprop(gChosen, "dhcp-response", &dhcpResponse,
    130163        sizeof(dhcpResponse));
    131     if (bytesRead != OF_FAILED && bytesRead == (int)sizeof(dhcpResponse))
    132         SetIPAddress(ntohl(dhcpResponse.ip_address));
     164    if (bytesRead != OF_FAILED && bytesRead == (int)sizeof(dhcpResponse)) {
     165        SetIPAddress(ntohl(dhcpResponse.ip_address));
     166    } else {
     167        char saddr[16];
     168        package = of_finddevice("/options");
     169        bytesRead = of_getprop(package, "default-client-ip", saddr, sizeof(saddr));
     170        if (bytesRead != OF_FAILED) {
     171            saddr[bytesRead] = '\0';
     172            printf("default-client-ip: %s\n", saddr);
     173            ip_addr_t addr = parse_ip_address(saddr);
     174            printf("addr = %x\n", (unsigned int)addr);
     175            SetIPAddress(addr);
     176        }
     177    }
    133178
    134179    return B_OK;
    135180}