📄 options.cc
字号:
/* * PXE daemon - enable the remote booting of PXE enabled machines. * Copyright (C) 2000 Tim Hurman (kano@kano.org.uk) * * 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. * *//****************************************************************************** * options.cc - read the config options from the file * ******************************************************************************/#include "options.h"// global vars CSA_t CSA_types[CSA_MAX_TYPES] ={ {0, "X86PC"}, {1, "PC98"}, {2, "IA64PC"}, {3, "DEC"}, {4, "ArcX86"}, {5, "ILC"}, {(uint16_t)-1, NULL}};/****************************************************************************** * Options - constructor - read from the default file * ******************************************************************************/Options::Options(LogFile *_log){ this->log = _log; ReadFile(PXECONFIGFILE);}/****************************************************************************** * Options - constructor - read from the specified file * ******************************************************************************/Options::Options(LogFile *_log, const char *filename){ this->log = _log; ReadFile(filename);}/****************************************************************************** * Options - Deconstuctor * ******************************************************************************/Options::~Options(){ services_t *serv_ptr, *serv_prev; if(interface != NULL) delete[] interface; if(prompt != NULL) delete[] prompt; if(domain != NULL) delete[] domain; if(tftpdbase != NULL) delete[] tftpdbase; port = 0; // scan and delete options for(serv_ptr=serv_prev=serv_head; serv_ptr != NULL; ) { serv_prev = serv_ptr; serv_ptr = serv_ptr->next; delete[] serv_prev->filebase; delete[] serv_prev->menu_text; delete serv_prev; } serv_head = NULL;}/****************************************************************************** * ReadFile - read the config file * ******************************************************************************/voidOptions::ReadFile(const char *filename){ std::fstream *fp; int len; char *key,*val; struct in_addr t_addr; int key_id = 1; char *buf; // initalise vars buf = new char[1024]; tftpdbase = new char[strlen(TFTPD_BASE)+1]; strcpy(tftpdbase, TFTPD_BASE); domain = new char[strlen(DEF_DOMAIN)+1]; strcpy(domain, DEF_DOMAIN); prompt = new char[strlen(DEF_PROMPT)+1]; strcpy(prompt, DEF_PROMPT); interface = NULL; multicast_address = DEF_MULTI_BOOT; mtftp_address = DEF_MTFTP_ADDR; mtftp_cport = DEF_MTFTP_CPORT; mtftp_sport = DEF_MTFTP_SPORT; port = DEF_PORT; prompt_timeout = DEF_PROMPT_TIMEOUT; default_address = 0; use_multicast = use_broadcast = 1; serv_head = NULL; fp = new std::fstream(filename, std::ios::in); if(fp == NULL) throw new SysException(errno, "Options::ReadFile:fopen()"); while(fp->getline(buf, 1024)) { len = strlen(buf)-1; if(-1 == len) goto Options_ReadFile_next; for(; len > 0; len--) if((buf[len] != '\n') && (buf[len] != '\r')) { len++; break; } buf[len] = 0; if((buf[0] == ' ') || (buf[0] == '#') || (buf[0] == 0)) goto Options_ReadFile_next; // examine the string contents key = strtok(buf, "="); if(key == NULL) goto Options_ReadFile_next; val = strtok(NULL, "="); if(val == NULL) goto Options_ReadFile_next; // examine key if(strcmp("interface", key) == 0) { if (NULL != interface) delete interface; interface = new char[strlen(val)+1]; strcpy(interface, val); } else if(strcmp("prompt", key) == 0) { delete prompt; prompt = new char[strlen(val)+1]; strcpy(prompt, val); } else if(strcmp("listen_port", key) == 0) { port = atoi(val); } else if(strcmp("use_multicast", key) == 0) { use_multicast = atoi(val); } else if(strcmp("use_broadcast", key) == 0) { use_broadcast = atoi(val); } else if(strcmp("multicast_address", key) == 0) { t_addr.s_addr = 0;#ifdef HAVE_INET_ATON if(0 == inet_aton(val, &t_addr))#else t_addr.s_addr = inet_addr(val); if((unsigned)-1 == t_addr.s_addr)#endif // HAVE_INET_ATON t_addr.s_addr = DEF_MTFTP_ADDR; multicast_address = ntohl(t_addr.s_addr); } else if(strcmp("domain", key) == 0) { delete domain; domain = new char[strlen(val)+1]; strcpy(domain, val); } else if(strcmp("tftpdbase", key) == 0) { delete tftpdbase; tftpdbase = new char[strlen(val)+1]; strcpy(tftpdbase, val); } else if(strcmp("service", key) == 0) { AddService(val, &key_id); } else if(strcmp("mtftp_address", key) == 0) { t_addr.s_addr = 0;#ifdef HAVE_INET_ATON if(0 == inet_aton(val, &t_addr))#else t_addr.s_addr = inet_addr(val); if((unsigned)-1 == t_addr.s_addr)#endif // HAVE_INET_ATON t_addr.s_addr = htonl(DEF_MTFTP_ADDR); mtftp_address = ntohl(t_addr.s_addr); } else if(strcmp("mtftp_client_port", key) == 0) { mtftp_cport = atoi(val); if(mtftp_cport == 0) mtftp_cport = DEF_MTFTP_CPORT; } else if(strcmp("mtftp_server_port", key) == 0) { mtftp_sport = atoi(val); if(mtftp_sport == 0) mtftp_sport = DEF_MTFTP_SPORT; } else if(strcmp("prompt_timeout", key) == 0) { prompt_timeout = atoi(val); if((0 == prompt_timeout) && (EINVAL == errno)) prompt_timeout = DEF_PROMPT_TIMEOUT; } else if(strcmp("default_address", key) == 0) { t_addr.s_addr = 0;#ifdef HAVE_INET_ATON if(0 == inet_aton(val, &t_addr))#else t_addr.s_addr = inet_addr(val); if((unsigned)-1 == t_addr.s_addr)#endif // HAVE_INET_ATON t_addr.s_addr = DEF_MTFTP_ADDR; default_address = ntohl(t_addr.s_addr); } else { log->Event(LEVEL_ERR, "Options::ReadFile", 2, "Unknown key:", key); } Options_ReadFile_next: fp = fp; } fp->close(); delete[] buf;}/****************************************************************************** * GetInterface - get the interface name * ******************************************************************************/char *Options::GetInterface(){ return(interface);}/****************************************************************************** * GetPort - return the port number * ******************************************************************************/uint16_tOptions::GetPort(){ return(port);}/****************************************************************************** * UseMulticast - shall we use multicast discovery * ******************************************************************************/intOptions::UseMulticast(){ return(use_multicast);}/****************************************************************************** * UseBroadcast - shall we use broadcast discovery * ******************************************************************************/intOptions::UseBroadcast(){ return(use_broadcast);}/****************************************************************************** * GetMulticast - return the multicast address specified * ******************************************************************************/uint32_tOptions::GetMulticast(){ return(multicast_address);}/****************************************************************************** * AddService - add a service to the list * ******************************************************************************/voidOptions::AddService(char *serviceinfo, int *key_id){ services_t *serv_ptr, *serv_tail, *serv_prev; char *ptr; int i; ptr = strtok(serviceinfo, ","); if(ptr == NULL) { log->Event(LEVEL_ERR, "AddService:parse", 1, "Invalid service declaration"); return; } // see if it is a recognised CSA for(i=0; ((CSA_types[i].arch_name != NULL) && (strcmp(CSA_types[i].arch_name, ptr) != 0)); i++);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -