void Setting::ReadConfiguration() { ifreq request; if (!_PrepareRequest(request)) return; BString text = "dummy"; char address[32]; sockaddr_in* inetAddress = NULL; // Obtain IP. if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0) return; inetAddress = (sockaddr_in*)&request.ifr_addr; if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, sizeof(address)) == NULL) { return; } fIP = address; // Obtain netmask. if (ioctl(fSocket, SIOCGIFNETMASK, &request, sizeof(request)) < 0) { return; } inetAddress = (sockaddr_in*)&request.ifr_mask; if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, sizeof(address)) == NULL) { return; } fNetmask = address; // Obtain gateway ifconf config; config.ifc_len = sizeof(config.ifc_value); if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) return; uint32 size = (uint32)config.ifc_value; if (size == 0) return; void *buffer = malloc(size); if (buffer == NULL) return; MemoryDeleter bufferDeleter(buffer); config.ifc_len = size; config.ifc_buf = buffer; if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) return; ifreq *interface = (ifreq *)buffer; ifreq *end = (ifreq *)((uint8 *)buffer + size); while (interface < end) { route_entry& route = interface->ifr_route; if (route.flags & RTF_GATEWAY) { inetAddress = (sockaddr_in*)route.gateway; fGateway = inet_ntoa(inetAddress->sin_addr); } int32 addressSize = 0; if (route.destination != NULL) addressSize += route.destination->sa_len; if (route.mask != NULL) addressSize += route.mask->sa_len; if (route.gateway != NULL) addressSize += route.gateway->sa_len; interface = (ifreq *)((addr_t)interface + IF_NAMESIZE + sizeof(route_entry) + addressSize); } uint32 flags = 0; if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0) flags = request.ifr_flags; fAuto = flags & IFF_AUTO_CONFIGURED; if (ioctl(fSocket, SIOCGIFMEDIA, &request, sizeof(struct ifreq)) == 0) fMedia = request.ifr_media; // read resolv.conf for the dns. fNameservers.MakeEmpty(); #define MATCH(line, name) \ (!strncmp(line, name, sizeof(name) - 1) && \ (line[sizeof(name) - 1] == ' ' || \ line[sizeof(name) - 1] == '\t')) register FILE *fp = fopen("/etc/resolv.conf", "r"); if (fp == NULL) { fprintf(stderr, "failed to open '/etc/resolv.conf' to " "read nameservers: %s\n", strerror(errno)); return; } int nserv = 0; char buf[1024]; register char *cp; //, **pp; // register int n; int MAXNS = 2; // read the config file while (fgets(buf, sizeof(buf), fp) != NULL) { // skip comments if (*buf == ';' || *buf == '#') continue; // read nameservers to query if (MATCH(buf, "nameserver") && nserv < MAXNS) { // char sbuf[2]; cp = buf + sizeof("nameserver") - 1; while (*cp == ' ' || *cp == '\t') cp++; cp[strcspn(cp, ";# \t\n")] = '\0'; if ((*cp != '\0') && (*cp != '\n')) { fNameservers.AddItem(new BString(cp)); nserv++; } } continue; } fclose(fp); }
void Setting::ReadConfiguration() { ifreq request; if (!_PrepareRequest(request)) return; BString text = "dummy"; char address[32]; sockaddr_in* inetAddress = NULL; // Obtain IP. if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0) return; inetAddress = (sockaddr_in*)&request.ifr_addr; if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, sizeof(address)) == NULL) { return; } fIP = address; // Obtain netmask. if (ioctl(fSocket, SIOCGIFNETMASK, &request, sizeof(request)) < 0) { return; } inetAddress = (sockaddr_in*)&request.ifr_mask; if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, sizeof(address)) == NULL) { return; } fNetmask = address; // Obtain gateway ifconf config; config.ifc_len = sizeof(config.ifc_value); if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) return; uint32 size = (uint32)config.ifc_value; if (size == 0) return; void *buffer = malloc(size); if (buffer == NULL) return; MemoryDeleter bufferDeleter(buffer); config.ifc_len = size; config.ifc_buf = buffer; if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) return; ifreq *interface = (ifreq *)buffer; ifreq *end = (ifreq *)((uint8 *)buffer + size); while (interface < end) { route_entry& route = interface->ifr_route; if (route.flags & RTF_GATEWAY) { inetAddress = (sockaddr_in*)route.gateway; fGateway = inet_ntoa(inetAddress->sin_addr); } int32 addressSize = 0; if (route.destination != NULL) addressSize += route.destination->sa_len; if (route.mask != NULL) addressSize += route.mask->sa_len; if (route.gateway != NULL) addressSize += route.gateway->sa_len; interface = (ifreq *)((addr_t)interface + IF_NAMESIZE + sizeof(route_entry) + addressSize); } uint32 flags = 0; if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0) flags = request.ifr_flags; fAuto = (flags & IFF_AUTO_CONFIGURED) != 0; fEnabled = (flags & IFF_UP) != 0; if (ioctl(fSocket, SIOCGIFMEDIA, &request, sizeof(struct ifreq)) == 0) fMedia = request.ifr_media; // read resolv.conf for the dns. fNameservers.MakeEmpty(); res_init(); res_state state = __res_state(); if (state != NULL) { for (int i = 0; i < state->nscount; i++) { fNameservers.AddItem( new BString(inet_ntoa(state->nsaddr_list[i].sin_addr))); } } }
void NetworkStatusView::_ShowConfiguration(BMessage* message) { static const struct information_entry { const char* label; int32 control; } kInformationEntries[] = { { B_TRANSLATE("Address"), SIOCGIFADDR }, { B_TRANSLATE("Broadcast"), SIOCGIFBRDADDR }, { B_TRANSLATE("Netmask"), SIOCGIFNETMASK }, { NULL } }; SocketOpener socket; if (socket.InitCheck() != B_OK) return; const char* name; if (message->FindString("interface", &name) != B_OK) return; ifreq request; if (!_PrepareRequest(request, name)) return; BString text(B_TRANSLATE("%ifaceName information:\n")); text.ReplaceFirst("%ifaceName", name); size_t boldLength = text.Length(); for (int i = 0; kInformationEntries[i].label; i++) { if (ioctl(socket, kInformationEntries[i].control, &request, sizeof(request)) < 0) { continue; } char address[32]; sockaddr_in* inetAddress = NULL; switch (kInformationEntries[i].control) { case SIOCGIFNETMASK: inetAddress = (sockaddr_in*)&request.ifr_mask; break; default: inetAddress = (sockaddr_in*)&request.ifr_addr; break; } if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, sizeof(address)) == NULL) { return; } text << "\n" << kInformationEntries[i].label << ": " << address; } BAlert* alert = new BAlert(name, text.String(), B_TRANSLATE("OK")); BTextView* view = alert->TextView(); BFont font; view->SetStylable(true); view->GetFont(&font); font.SetFace(B_BOLD_FACE); view->SetFontAndColor(0, boldLength, &font); alert->Go(NULL); }