📄 net.c
字号:
void setKickstartNetwork(struct loaderData_s * loaderData, int argc, char ** argv) { char * arg, * bootProto = NULL, * device = NULL, *ethtool = NULL, * class = NULL; char * essid = NULL, * wepkey = NULL, * onboot = NULL; int noDns = 0, noksdev = 0, rc, mtu = 0, noipv4 = 0, noipv6 = 0; poptContext optCon; struct networkDeviceConfig cfg; struct poptOption ksOptions[] = { { "bootproto", '\0', POPT_ARG_STRING, &bootProto, 0, NULL, NULL }, { "device", '\0', POPT_ARG_STRING, &device, 0, NULL, NULL }, { "dhcpclass", '\0', POPT_ARG_STRING, &class, 0, NULL, NULL }, { "gateway", '\0', POPT_ARG_STRING, NULL, 'g', NULL, NULL }, { "ip", '\0', POPT_ARG_STRING, NULL, 'i', NULL, NULL }, { "mtu", '\0', POPT_ARG_INT, &mtu, 0, NULL, NULL }, { "nameserver", '\0', POPT_ARG_STRING, NULL, 'n', NULL, NULL }, { "netmask", '\0', POPT_ARG_STRING, NULL, 'm', NULL, NULL }, { "noipv4", '\0', POPT_ARG_NONE, &noipv4, 0, NULL, NULL }, { "noipv6", '\0', POPT_ARG_NONE, &noipv6, 0, NULL, NULL }, { "nodns", '\0', POPT_ARG_NONE, &noDns, 0, NULL, NULL }, { "hostname", '\0', POPT_ARG_STRING, NULL, 'h', NULL, NULL}, { "ethtool", '\0', POPT_ARG_STRING, ðtool, 0, NULL, NULL }, { "essid", '\0', POPT_ARG_STRING, &essid, 0, NULL, NULL }, { "wepkey", '\0', POPT_ARG_STRING, &wepkey, 0, NULL, NULL }, { "onboot", '\0', POPT_ARG_STRING, &onboot, 0, NULL, NULL }, { "notksdevice", '\0', POPT_ARG_NONE, &noksdev, 0, NULL, NULL }, { 0, 0, 0, 0, 0, 0, 0 } }; optCon = poptGetContext(NULL, argc, (const char **) argv, ksOptions, 0); while ((rc = poptGetNextOpt(optCon)) >= 0) { arg = (char *) poptGetOptArg(optCon); switch (rc) { case 'g': loaderData->gateway = strdup(arg); break; case 'i': loaderData->ipv4 = strdup(arg); break; case 'n': loaderData->dns = strdup(arg); break; case 'm': loaderData->netmask = strdup(arg); break; case 'h': if (loaderData->hostname) free(loaderData->hostname); loaderData->hostname = strdup(arg); break; } } if (rc < -1) { newtWinMessage(_("Kickstart Error"), _("OK"), _("Bad argument to kickstart network command %s: %s"), poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(rc)); } else { poptFreeContext(optCon); } /* if they've specified dhcp/bootp or haven't specified anything, * use dhcp for the interface */ if ((bootProto && (!strncmp(bootProto, "dhcp", 4) || !strncmp(bootProto, "bootp", 4))) || (!bootProto && !loaderData->ipv4)) { loaderData->ipv4 = strdup("dhcp"); loaderData->ipinfo_set = 1; } else if (loaderData->ipv4) { /* JKFIXME: this assumes a bit... */ loaderData->ipinfo_set = 1; } /* now make sure the specified bootproto is valid */ if (bootProto && strcmp(bootProto, "dhcp") && strcmp(bootProto, "bootp") && strcmp(bootProto, "static") && strcmp(bootProto, "query")) { newtWinMessage(_("Kickstart Error"), _("OK"), _("Bad bootproto %s specified in network command"), bootProto); } if (!noksdev) { if (device) { loaderData->netDev = strdup(device); loaderData->netDev_set = 1; } if (class) { loaderData->netCls = strdup(class); loaderData->netCls_set = 1; } if (ethtool) { if (loaderData->ethtool) free(loaderData->ethtool); loaderData->ethtool = strdup(ethtool); free(ethtool); } if (essid) { if (loaderData->essid) free(loaderData->essid); loaderData->essid = strdup(essid); free(essid); } if (wepkey) { if (loaderData->wepkey) free(loaderData->wepkey); loaderData->wepkey = strdup(wepkey); free(wepkey); } if (mtu) { loaderData->mtu = mtu; } if (noipv4) loaderData->noipv4 = 1; if (noipv6) loaderData->noipv6 = 1; } if (noDns) { loaderData->noDns = 1; } /* Make sure the network is always up if there's a network line in the * kickstart file, as %post/%pre scripts might require that. */ if (loaderData->method != METHOD_NFS && loaderData->method != METHOD_FTP && loaderData->method != METHOD_HTTP) { initLoopback(); if (kickstartNetworkUp(loaderData, &cfg)) logMessage(ERROR, "unable to bring up network"); }}/* if multiple interfaces get one to use from user. *//* NOTE - uses kickstart data available in loaderData */int chooseNetworkInterface(struct loaderData_s * loaderData) { int i, rc; unsigned int max = 40; int deviceNums = 0; int deviceNum; char ** devices; char ** deviceNames; int foundDev = 0; struct device ** devs; char * ksMacAddr = NULL; devs = probeDevices(CLASS_NETWORK, BUS_UNSPEC, PROBE_LOADED); if (!devs) { logMessage(ERROR, "no network devices in choose network device!"); return LOADER_ERROR; } for (i = 0; devs[i]; i++); devices = alloca((i + 1) * sizeof(*devices)); deviceNames = alloca((i + 1) * sizeof(*devices)); if (loaderData->netDev && (loaderData->netDev_set) == 1) { if ((loaderData->bootIf && (loaderData->bootIf_set) == 1) && !strcasecmp(loaderData->netDev, "bootif")) { ksMacAddr = strdup(loaderData->bootIf); } else { ksMacAddr = strdup(loaderData->netDev); } ksMacAddr = str2upper(ksMacAddr); } for (i = 0; devs[i]; i++) { if (!devs[i]->device) continue; if (devs[i]->desc) { deviceNames[deviceNums] = alloca(strlen(devs[i]->device) + strlen(devs[i]->desc) + 4); sprintf(deviceNames[deviceNums],"%s - %s", devs[i]->device, devs[i]->desc); if (strlen(deviceNames[deviceNums]) > max) max = strlen(deviceNames[deviceNums]); devices[deviceNums++] = devs[i]->device; } else { devices[deviceNums] = devs[i]->device; deviceNames[deviceNums++] = devs[i]->device; } /* this device has been set and we don't really need to ask * about it again... */ if (loaderData->netDev && (loaderData->netDev_set == 1)) { if (!strcmp(loaderData->netDev, devs[i]->device)) { foundDev = 1; } else if (ksMacAddr != NULL) { /* maybe it's a mac address */ char *devmacaddr = NULL; devmacaddr = netlink_interfaces_mac2str(devs[i]->device); if ((devmacaddr != NULL) && !strcmp(ksMacAddr, devmacaddr)) { foundDev = 1; free(loaderData->netDev); loaderData->netDev = devs[i]->device; if (devmacaddr != NULL) free(devmacaddr); break; } if (devmacaddr != NULL) free(devmacaddr); } } } if (ksMacAddr) free(ksMacAddr); if (foundDev == 1) return LOADER_NOOP; devices[deviceNums] = NULL; deviceNames[deviceNums] = NULL; qsort(devices, deviceNums, sizeof(*devices), simpleStringCmp); qsort(deviceNames, deviceNums, sizeof(*devices), simpleStringCmp); /* ASSERT: we should *ALWAYS* have a network device when we get here */ if (!deviceNums) { logMessage(CRITICAL, "no network device in chooseNetworkInterface"); return LOADER_ERROR; } /* JKFIXME: if we only have one interface and it doesn't have link, * do we go ahead? */ if (deviceNums == 1) { logMessage(INFO, "only have one network device: %s", devices[0]); loaderData->netDev = devices[0]; return LOADER_NOOP; } if ((loaderData->netDev && (loaderData->netDev_set == 1)) && !strcmp(loaderData->netDev, "link")) { logMessage(INFO, "looking for first netDev with link"); for (rc = 0; rc < 5; rc++) { for (i = 0; i < deviceNums; i++) { if (get_link_status(devices[i]) == 1) { loaderData->netDev = devices[i]; logMessage(INFO, "%s has link, using it", devices[i]); return LOADER_NOOP; } } sleep(1); } logMessage(WARNING, "wanted netdev with link, but none present. prompting"); } startNewt(); if (max > 70) max = 70; /* JKFIXME: should display link status */ deviceNum = 0; rc = newtWinMenu(_("Networking Device"), _("You have multiple network devices on this system. " "Which would you like to install through?"), max, 10, 10, deviceNums < 6 ? deviceNums : 6, deviceNames, &deviceNum, _("OK"), _("Back"), NULL); if (rc == 2) return LOADER_BACK; loaderData->netDev = devices[deviceNum]; /* turn off the non-active interface. this should keep things from * breaking when we need the interface to do the install as long as * you keep using that device */ for (i = 0; devs[i]; i++) { if (strcmp(loaderData->netDev, devices[i])) if (!FL_TESTING(flags)) pumpDisableInterface(devs[i]->device); } return LOADER_OK;}/* JKFIXME: bad name. this function brings up networking early on a * kickstart install so that we can do things like grab the ks.cfg from * the network */int kickstartNetworkUp(struct loaderData_s * loaderData, struct networkDeviceConfig *netCfgPtr) { int rc; /* we may have networking already, so return to the caller */ if ((loaderData->ipinfo_set == 1) || (loaderData->ipv6info_set == 1)) return 0; initLoopback(); memset(netCfgPtr, 0, sizeof(*netCfgPtr)); do { do { /* this is smart and does the right thing based on whether or not * we have ksdevice= specified */ rc = chooseNetworkInterface(loaderData); if (rc == LOADER_ERROR) { /* JKFIXME: ask for a driver disk? */ logMessage(ERROR, "no network drivers for doing kickstart"); return -1; } else if (rc == LOADER_BACK) { return -1; } /* insert device into pump structure */ strcpy(netCfgPtr->dev.device, loaderData->netDev); break; } while (1); /* we don't want to end up asking about interface more than once * if we're in a kickstart-ish case (#100724) */ loaderData->netDev_set = 1; /* JKFIXME: this is kind of crufty, we depend on the fact that the * ip is set and then just get the network up. we should probably * add a way to do asking about static here and not be such a hack */ if (!loaderData->ipv4) { loaderData->ipv4 = strdup("dhcp"); } loaderData->ipinfo_set = 1; setupNetworkDeviceConfig(netCfgPtr, loaderData); rc = readNetConfig(loaderData->netDev, netCfgPtr, loaderData->netCls, loaderData->method); if (rc == LOADER_ERROR) { logMessage(ERROR, "unable to setup networking"); return -1; } else if (rc == LOADER_BACK) { /* Going back to the interface selection screen, so unset anything * we set before attempting to bring the incorrect interface up. */ loaderData->netDev_set = 0; free(loaderData->ipv4); loaderData->ipinfo_set = 0; } else break; } while (1); return 0;}void splitHostname (char *str, char **host, char **port){ char *rightbrack = strchr(str, ']'); *host = NULL; *port = NULL; if (*str == '[' && rightbrack) { /* An IPv6 address surrounded by brackets, optionally with a colon and * port number. */ char *colon = strrchr(rightbrack, ':'); if (colon) { *host = strndup(str+1, rightbrack-1-str); *port = strdup(colon+1); } else *host = strndup(str+1, rightbrack-1-str); } else if (strcount(str, ':') > 1) { /* An IPv6 address without brackets. Don't make the user surround the * address with brackets if there's no port number. */ *host = strdup(str); } else { /* An IPv4 address, optionally with a colon and port number. */ char *colon = strrchr(str, ':'); if (colon) { *h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -