📄 amap-lib.c
字号:
/* AMAP - application mapper Copyright (c) 2003 van Hauser and DJ.RevMoon * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */// INCLUDES //#include "amap-inc.h"#include "amap.h"#ifdef OPENSSLRSA *amap_rsa = NULL;#endifint glob;int glob_af_inet = AF_INET;struct sockaddr_in glob_sin;struct in_addr glob_in;struct sockaddr *glob_sockaddr = (struct sockaddr *) &glob_sin;char *glob_addr = (char *) &glob_in;int glob_sockaddr_len = sizeof(glob_sin);int glob_addr_len = sizeof(glob_in);#ifdef AF_INET6struct sockaddr_in6 glob_sin6;struct in6_addr glob_in6;struct addrinfo glob_hints, *glob_result;#endif// AMAP_ERROR - partial rip from vh-lib //void amap_error(char *string, ...) { va_list ap; char *ptr; fprintf(stderr, "Error: "); va_start(ap, string); for (ptr = string; *ptr != '\0'; ptr++) { if (*ptr == '%') { ptr++; switch(*ptr) { case 's': fprintf(stderr, "%s", va_arg(ap, char *)); break; case 'd': fprintf(stderr, "%d", va_arg(ap, int)); break; case 'c': fprintf(stderr, "%c", va_arg(ap, int)); break; default: fprintf(stderr, "%c", *ptr); } } else fprintf(stderr, "%c", *ptr); } fprintf(stderr, "\n"); va_end(ap); exit(-1);}// AMAP_WARN - partial rip from vh-lib //void amap_warn(char *string, ...) { va_list ap; char *ptr; printf("Warning: "); va_start(ap, string); for (ptr = string; *ptr != '\0'; ptr++) { if (*ptr == '%') { ptr++; switch(*ptr) { case 's': printf("%s", va_arg(ap, char *)); break; case 'd': printf("%d", va_arg(ap, int)); break; case 'c': printf("%c", va_arg(ap, int)); break; default: printf("%c", *ptr); } } else printf("%c", *ptr); } printf("\n"); va_end(ap);}// AMAP_GET_DATA_TOKEN - partial rip from vh-lib //char *amap_get_data_token(char *data, char token) { static char vdata[AMAP_MAXTOKENLEN] = ""; char search[4] = "#X:"; char *ptr; search[1] = token; if (strncmp(data, "###", 3) != 0) {// amap_warn("invalid or missing version data: %s", data); return(vdata); } if ((ptr = strstr(data, search)) == NULL) {// amap_warn("missing token in data: %s", data); return(vdata); } memcpy(vdata, ptr + 3, sizeof(vdata)-1); vdata[sizeof(vdata) - 1] = 0; if ((ptr = index(vdata, '#')) == NULL) {// amap_error("invalid or missing version string in webfile: %s", data); strcpy(vdata, ""); return(vdata); } *ptr = 0; return(vdata);}// AMAP_WEBUPDATE_FILE - partial rip from vh-lib //int amap_webupdate_file(char *webfile, char *localfile, int checkversion, int ask) { int len = strlen("http://"); int wlen = strlen(webfile); int port = 80, s, result = 1, datalen = 0, version = -1;int xx = 0; unsigned long int ip; time_t epoch; struct in_addr in; struct hostent *target; struct sockaddr_in addr; struct tm *the_time; char *url, *ptr, *data = NULL, *filedata; char *host = malloc(strlen(webfile)); char *request = malloc(AMAP_WEBBUFLEN + wlen); char datetime[64] = ""; FILE *f; if (strncmp(webfile, "http://", len) != 0) amap_error("webfile location is missing http://: %s", webfile); if ((url = index(webfile + len, '/')) == NULL) amap_error("webfile definition is missing a web file location: %s", webfile); memset(host, 0, wlen); memset(request, 0, AMAP_WEBBUFLEN + wlen); memcpy(host, webfile + len, url - (webfile + len)); if (index(host, '@') != NULL) amap_error("authentication not supported: %s", host); if ((ptr = index(host, ':')) != NULL) { *ptr++ = 0; port = atoi(ptr); if (port < 0 || port > 65535) amap_error("invalid port: %s", ptr); } snprintf(request, AMAP_WEBBUFLEN + wlen, "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: %s %s\r\n\r\n", url, host, port, AMAP_PROGRAM, AMAP_VERSION);//#ifdef AF_INET6//printf("uses pton\n");// if ((xx = inet_pton(AF_INET, host, &in)) < 0) {//printf("failed %d: %d %s\n",xx,AF_INET,host);//#else//printf("uses aton\n");// if ((xx=inet_aton(host, &in)) <= 0) {//printf("failed %d: %d %s\n",xx,AF_INET,host);//#endif if ((target = gethostbyname(host)) != NULL) { memcpy((char*)&ip, (char*)target->h_addr, 4); } else amap_error("could not resolve host: %s", host);// } else//{// memcpy((char*)&ip, (char*)&in.s_addr, 4);//printf("success: %d: %d %s\n", xx, AF_INET, host);//}//// printf("!!! SPECIAL DEBUG OUTPUT !!!\nPlease send the following information in if the web update fails:\n");// printf("Host: %s\n", host);// printf("IP (int): %d\n", ip);// printf("Dump of (in):\n");// amap_dump_string(stdout, (unsigned char*)&in, sizeof(in), 16);// printf("\n"); if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) amap_error("could not get a socket"); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &result, sizeof(result)); addr.sin_port = htons(port); addr.sin_family = AF_INET; memcpy(&addr.sin_addr.s_addr, &ip, 4); if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) amap_error("could not connect to host: %s (%lu)", host, ip); if (send(s, request, strlen(request), 0) < 0) amap_error("web site closed connection"); memset(request, 0, AMAP_WEBBUFLEN + wlen); while ((len = recv(s, request, AMAP_WEBBUFLEN, 0)) > 0) { if (data == NULL) data = malloc(len); else data = realloc(data, datalen + len); memcpy(data + datalen, request, len); datalen += len; memset(request, 0, AMAP_WEBBUFLEN); } close(s); // prevent memory access voilation if no version data is present data = realloc(data, datalen + AMAP_MAXTOKENLEN); memset(data + datalen, 0, AMAP_MAXTOKENLEN); if (strncmp(data, "HTTP/", strlen("HTTP/")) != 0) amap_error("invalid http response: %s", data); if (strncmp(data + strlen("HTTP/1.0 "), "200", 3) != 0) amap_error("file could not be found by web server: %s", data); if ((filedata = strstr(data, "\r\n\r\n")) == NULL) amap_error("no data found in response: %s", data); filedata += 4; datalen = datalen - (filedata - data); // versioncheck > 0 - check version information on file // format: "###V:104#P:1.0#D:1231231231#M:Have fun!###DO NOT EDIT THIS LINE!" if (checkversion) { if ((f = fopen(localfile, "r")) != NULL) { memset(request, 0, AMAP_WEBBUFLEN); fread(request, AMAP_WEBBUFLEN - AMAP_MAXTOKENLEN, 1, f); version = atoi(amap_get_data_token(request, 'V')); fclose(f); } if (version >= atoi(amap_get_data_token(filedata, 'V'))) { printf("No new updates for file %s available\n", localfile); free(request); free(host); free(data); return -1; } } // if ask > 0 - ask if overwrite file if (ask) { printf("Please confirm updating of file %s [YES(default)/no]: ", localfile); result = fgetc(stdin); if (result == 'N' || result == 'n') return -1; } // get other data if ((epoch = strtoul(amap_get_data_token(filedata, 'D'), NULL, 10)) > 1000000000) { the_time = localtime(&epoch); strftime(datetime, sizeof(datetime), " (data from %Y-%m-%d %H:%M:%S)", the_time); } // write file if ((f = fopen(localfile, "w")) == NULL) amap_error("can not write file %s", localfile); fwrite(filedata, datalen, 1, f); fclose(f); printf("File %s successfully updated%s\n", localfile, datetime); // msg check ptr = amap_get_data_token(filedata, 'M'); if (strlen(ptr) > 0) { printf("This update comes with the following message:\n\"%s\"\n", ptr); } // main program version check ptr = amap_get_data_token(filedata, 'P'); if (strlen(ptr) > 2) { if (strcmp(ptr, AMAP_VERSION) != 0) printf("A new version of %s is available! You are using v%s, current is v%s.\nGo and download from %s !\n", AMAP_PROGRAM, AMAP_VERSION, ptr, AMAP_RESOURCE); } free(request); free(host); free(data); return 0;}#ifdef OPENSSL// AMAP_SSL_TEMP_RSA_CB //RSA *amap_ssl_temp_rsa_cb(SSL *ssl, int export, int keylength) { if (amap_rsa == NULL) amap_rsa = RSA_generate_key(512, RSA_F4, NULL, NULL); return amap_rsa;}#endif// AMAP_OPEN_FILE //FILE *amap_open_file(char *fnam, char *type, char *extension, int verbose) { char file_name[256]; FILE *f = NULL; if (fnam != NULL) { strncpy(file_name, fnam, sizeof(file_name) - strlen(extension) - 1); file_name[sizeof(file_name) - strlen(extension) - 1] = 0; strcat(file_name, extension); f = fopen(file_name, "r"); } else { strcpy(file_name, "./"); strcat(file_name, AMAP_DEFAULT_FILENAME); strcat(file_name, extension); if ((f = fopen(file_name, "r")) == NULL) { strcpy(file_name, AMAP_PREFIX); if (file_name[strlen(file_name) - 1] != '/') strcat(file_name, "/"); strcat(file_name, "etc/"); strcat(file_name, AMAP_DEFAULT_FILENAME); strcat(file_name, extension); f = fopen(file_name, "r"); } } if (f == NULL) amap_error("can not open %s file: %s", type, file_name); else if (verbose) printf("Using %s file %s ... ", type, file_name); return f;}// AMAP_STRDUP //char *amap_strdup(char *string) { char *ptr; int count; if (string == NULL) return NULL; count = strlen(string) + 1; if ((ptr = malloc(count)) == NULL) amap_error("malloc failed"); if (count == 1) *ptr = 0; else strcpy(ptr, string); return ptr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -