📄 urls.c
字号:
/* * urls.c - url handling code * * Erik Troan <ewt@redhat.com> * Matt Wilson <msw@redhat.com> * Michael Fulbright <msf@redhat.com> * Jeremy Katz <katzj@redhat.com> * * Copyright 1997 - 2002 Red Hat, Inc. * * This software may be freely redistributed under the terms of the GNU * General Public License. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <arpa/inet.h>#include <ctype.h>#include <fcntl.h>#include <netinet/in.h>#include <newt.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <netdb.h>#include "../isys/dns.h"#include "ftp.h"#include "lang.h"#include "loader.h"#include "loadermisc.h"#include "urls.h"#include "log.h"#include "windows.h"#include "net.h"/* boot flags */extern int flags;/* convert a url (ftp or http) to a ui */int convertURLToUI(char *url, struct iurlinfo *ui) { char *chptr; memset(ui, 0, sizeof(*ui)); if (!strncmp("ftp://", url, 6)) { ui->protocol = URL_METHOD_FTP; url += 6; /* There could be a username/password on here */ if ((chptr = strchr(url, '@'))) { if ((chptr = strchr(url, ':'))) { *chptr = '\0'; ui->login = strdup(url); url = chptr + 1; chptr = strchr(url, '@'); *chptr = '\0'; ui->password = strdup(url); url = chptr + 1; } else { *chptr = '\0'; ui->login = strdup(url); url = chptr + 1; } } } else if (!strncmp("http://", url, 7)) { ui->protocol = URL_METHOD_HTTP; url += 7; } else { logMessage(ERROR, "unknown url protocol '%s'", url); return -1; } /* url is left pointing at the hostname */ chptr = strchr(url, '/'); if (chptr != NULL) { *chptr = '\0'; ui->address = strdup(url); url = chptr; *url = '/'; ui->prefix = strdup(url); } else { ui->address = strdup(url); ui->prefix = strdup("/"); } logMessage(DEBUGLVL, "url address %s", ui->address); logMessage(DEBUGLVL, "url prefix %s", ui->prefix); return 0;}static char * getLoginName(char * login, struct iurlinfo *ui) { int i; i = 0; /* password w/o login isn't useful */ if (ui->login && strlen(ui->login)) { i += strlen(ui->login) + 5; if (strlen(ui->password)) i += 3*strlen(ui->password) + 5; if (ui->login || ui->password) { login = malloc(i); strcpy(login, ui->login); if (ui->password) { char * chptr; char code[4]; strcat(login, ":"); for (chptr = ui->password; *chptr; chptr++) { sprintf(code, "%%%2x", *chptr); strcat(login, code); } strcat(login, "@"); } } } return login;}/* convert a UI to a URL, returns allocated string */char *convertUIToURL(struct iurlinfo *ui) { char * login; char * finalPrefix; char * url; if (!strcmp(ui->prefix, "/")) finalPrefix = "/."; else finalPrefix = ui->prefix; login = ""; login = getLoginName(login, ui); url = malloc(strlen(finalPrefix) + 25 + strlen(ui->address) + strlen(login)); sprintf(url, "%s://%s%s/%s", ui->protocol == URL_METHOD_FTP ? "ftp" : "http", login, ui->address, finalPrefix); return url;}/* extraHeaders only applicable for http and used for pulling ks from http *//* see ftp.c:httpGetFileDesc() for details *//* set to NULL if not needed */int urlinstStartTransfer(struct iurlinfo * ui, char * filename, char *extraHeaders) { char * buf; int fd, port; int family = -1; char * finalPrefix; struct in_addr addr; struct in6_addr addr6; char *hostname, *portstr; if (!strcmp(ui->prefix, "/")) finalPrefix = ""; else finalPrefix = ui->prefix; buf = alloca(strlen(finalPrefix) + strlen(filename) + 20); if (*filename == '/') sprintf(buf, "%s%s", finalPrefix, filename); else sprintf(buf, "%s/%s", finalPrefix, filename); logMessage(INFO, "transferring %s://%s%s to a fd", ui->protocol == URL_METHOD_FTP ? "ftp" : "http", ui->address, buf); splitHostname(ui->address, &hostname, &portstr); if (portstr == NULL) port = -1; else port = atoi(portstr); if (inet_pton(AF_INET, hostname, &addr) >= 1) family = AF_INET; else if (inet_pton(AF_INET6, hostname, &addr6) >= 1) family = AF_INET6; else { if (mygethostbyname(hostname, &addr, AF_INET) == 0) { family = AF_INET; } else if (mygethostbyname(hostname, &addr6, AF_INET6) == 0) { family = AF_INET6; } else { logMessage(ERROR, "cannot determine address family of %s", hostname); } } if (ui->protocol == URL_METHOD_FTP) { ui->ftpPort = ftpOpen(hostname, family, ui->login ? ui->login : "anonymous", ui->password ? ui->password : "rhinstall@", NULL, port); if (ui->ftpPort < 0) return -2; fd = ftpGetFileDesc(ui->ftpPort, addr6, family, buf); if (fd < 0) { close(ui->ftpPort); return -1; } } else { fd = httpGetFileDesc(hostname, port, buf, extraHeaders); if (fd < 0) return -1; } if (!FL_CMDLINE(flags)) winStatus(70, 3, _("Retrieving"), "%s %s...", _("Retrieving"), filename); return fd;}int urlinstFinishTransfer(struct iurlinfo * ui, int fd) { if (ui->protocol == URL_METHOD_FTP) close(ui->ftpPort); close(fd); if (!FL_CMDLINE(flags)) newtPopWindow(); return 0;}char * addrToIp(char * hostname) { struct in_addr ad; struct in6_addr ad6; char *ret; if ((ret = malloc(48)) == NULL) return hostname; if (inet_ntop(AF_INET, &ad, ret, INET_ADDRSTRLEN) != NULL) return ret; else if (inet_ntop(AF_INET6, &ad6, ret, INET6_ADDRSTRLEN) != NULL) return ret; else if (mygethostbyname(hostname, &ad, AF_INET) == 0) return hostname; else if (mygethostbyname(hostname, &ad6, AF_INET6) == 0) return hostname; else return NULL;}int urlMainSetupPanel(struct iurlinfo * ui, urlprotocol protocol, char * doSecondarySetup) { newtComponent form, okay, cancel, siteEntry, dirEntry; newtComponent answer, text; newtComponent cb = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -