📄 global.cc
字号:
// Larbin// Sebastien Ailleret// 29-11-99 -> 08-03-00#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include <errno.h>#include <fcntl.h>#include <iostream.h>#include <string.h>#include <adns.h>#include <netdb.h>#include <sys/socket.h>#include "types.h"#include "global.h"#include "xutils/text.h"#include "xutils/Fifo.h"#include "xutils/Site.h"#include "xutils/debug.h"#include "xutils/MaxedSizedFifo.h"#include "xutils/PersistentFifo.h"#include "xutils/ConstantSizedFifo.h"#include "xutils/ConstantSizedFifoPriority.h"///////////////////////////////////////////////////////////// Struct global///////////////////////////////////////////////////////////// define all the static variableshashTable *global::seen;GenericFifo<url> *global::URLsInput;GenericFifo<url> *global::URLsInternal;Site *global::siteList;GenericFifo<Site> *global::okSites;GenericFifo<Site> *global::dnsSites;Connexion **global::connexions;adns_state global::ads;ConstantSizedFifoPriority<Connexion> *global::freeConns;ConstantSizedFifo<Connexion> *global::userConns;Interval *global::inter;uint global::depthInSite;time_t global::waitDuration;char *global::userAgent;char *global::sender;char *global::headers;sockaddr_in *global::proxyAddr;bool global::isSpecific;char *global::contentType;char *global::privilegedExt;Vector<char> *global::domains;Vector<char> global::forbExt;char *global::firstUrl;uint global::nb_conn;uint global::dnsConn;unsigned short int global::httpPort;unsigned short int global::inputPort;/** Constructor : initialize allmost everything * Everything is read from the config file (larbin.conf by default) */global::global (int argc, char *argv[]) { char *configFile = "larbin.conf"; bool reload = false; // verification of arguments int pos = 1; while (pos < argc) { if (!strcmp(argv[pos], "-c") && argc > pos+1) { configFile = argv[pos+1]; pos += 2; } else if (!strcmp(argv[pos], "-reload")) { reload = true; pos++; } else { break; } } if (pos != argc) { cerr << "usage : " << argv[0]; cerr << " [-c configFile] [-reload]\n"; exit(1); } // Standard values waitDuration = 60; depthInSite = 5; userAgent = "larbin"; sender = "larbin@somewhere.com"; firstUrl = "http://localhost/"; nb_conn = 20; dnsConn = 3; httpPort = 8081; inputPort = 1976; proxyAddr = NULL; isSpecific = false; domains = NULL; // Read the configuration file crash("Read the configuration file"); parseFile(configFile); // Initialize everything crash("Create global values"); // Headers String strtmp; strtmp.addString("\r\nUser-Agent: "); strtmp.addString(userAgent); strtmp.addString(" "); strtmp.addString(sender); strtmp.addString("\r\nAccept: text/html\r\n\r\n"); headers = strtmp.giveString(); // FIFOs URLsInternal = new PersistentFifo<url>(fifoFile, reload, this); URLsInput = new Fifo<url>; inter = new Interval(ramUrls); siteList = new Site[siteListSize]; okSites = new Fifo<Site>; dnsSites = new Fifo<Site>; seen = new hashTable(!reload); userConns = new ConstantSizedFifo<Connexion>(nb_conn); freeConns = new ConstantSizedFifoPriority<Connexion>(nb_conn); connexions = new Connexion *[nb_conn]; for (uint i=0; i<nb_conn; i++) { connexions[i] = new Connexion; freeConns->put(connexions[i]); } // init non blocking dns calls crash("Start adns"); adns_initflags flags = adns_initflags (adns_if_nosigpipe | adns_if_noerrprint); // adns_initflags (adns_if_nosigpipe); adns_init(&ads, flags, NULL);}/** Destructor : never used because the program should never end ! */global::~global () { cerr << "Why he hell do you want to delete global !\n";}/** parse configuration file */void global::parseFile (char *file) { int fds = open(file, O_RDONLY); if (fds < 0) { cerr << "cannot open config file\n"; exit(1); } char *tmp = readfile(fds); close(fds); // suppress commentary bool eff = false; for (int i=0; tmp[i] != 0; i++) { switch (tmp[i]) { case '\n': eff = false; break; case '#': eff = true; // no break !!! default: if (eff) tmp[i] = ' '; } } String content; content.addString(tmp); delete [] tmp; uint pos = 0; char *tok = nextToken(content, &pos); while (tok != NULL) { if (!strcasecmp(tok, "UserAgent")) { delete [] tok; userAgent = nextToken(content, &pos); } else if (!strcasecmp(tok, "From")) { delete [] tok; sender = nextToken(content, &pos); } else if (!strcasecmp(tok, "startUrl")) { delete [] tok; firstUrl = nextToken(content, &pos); } else if (!strcasecmp(tok, "waitduration")) { delete [] tok; tok = nextToken(content, &pos); waitDuration = atoi(tok); delete [] tok; } else if (!strcasecmp(tok, "proxy")) { delete [] tok; // host name and dns call tok = nextToken(content, &pos); struct hostent* hp; proxyAddr = new sockaddr_in; bzero((char *) proxyAddr, sizeof (struct sockaddr_in)); if ((hp = gethostbyname(tok)) == NULL) { endhostent(); cerr << "Unable to find proxy ip address\n"; exit(1); } else { proxyAddr->sin_family = hp->h_addrtype; memcpy ((char*) &proxyAddr->sin_addr, hp->h_addr, hp->h_length); } endhostent(); delete [] tok; // port number tok = nextToken(content, &pos); proxyAddr->sin_port = htons(atoi(tok)); delete [] tok; } else if (!strcasecmp(tok, "pagesConnexions")) { delete [] tok; tok = nextToken(content, &pos); nb_conn = atoi(tok); delete [] tok; } else if (!strcasecmp(tok, "dnsConnexions")) { delete [] tok; tok = nextToken(content, &pos); dnsConn = atoi(tok); delete [] tok; } else if (!strcasecmp(tok, "httpPort")) { delete [] tok; tok = nextToken(content, &pos); httpPort = atoi(tok); delete [] tok; } else if (!strcasecmp(tok, "inputPort")) { delete [] tok; tok = nextToken(content, &pos); inputPort = atoi(tok); delete [] tok; } else if (!strcasecmp(tok, "depthInSite")) { delete [] tok; tok = nextToken(content, &pos); depthInSite = atoi(tok); delete [] tok; } else if (!strcasecmp(tok, "specificSearch")) { delete [] tok; if (isSpecific) { cerr << "Bad configuration file : Two specificSearch fields\n"; exit(1); } isSpecific = true; contentType = nextToken(content, &pos); privilegedExt = nextToken(content, &pos); if (privilegedExt != NULL && !strcasecmp(privilegedExt, "end")) { delete [] privilegedExt; privilegedExt = NULL; } else { tok = nextToken(content, &pos); if (tok == NULL || strcasecmp(tok, "end")) { cerr << "Bad configuration file : no end to specificSearch\n"; exit(1); } delete [] tok; } } else if (!strcasecmp(tok, "limitToDomain")) { delete [] tok; manageDomain(content, &pos); } else if (!strcasecmp(tok, "forbiddenExtensions")) { delete [] tok; manageExt(content, &pos); } else { cerr << "bad configuration file : " << tok << "\n"; exit(1); } tok = nextToken(content, &pos); }}/** read the domain limit */void global::manageDomain (String &content, uint *pos) { char *tok = nextToken(content, pos); if (domains == NULL) { domains = new Vector<char>; } while (tok != NULL && strcasecmp(tok, "end")) { domains->addElement(tok); tok = nextToken(content, pos); } if (tok == NULL) { cerr << "Bad configuration file : no end to limitToDomain\n"; exit(1); } else { delete [] tok; }}/** read the forbidden extensions */void global::manageExt (String &content, uint *pos) { char *tok = nextToken(content, pos); while (tok != NULL && strcasecmp(tok, "end")) { forbExt.addElement(tok); tok = nextToken(content, pos); } if (tok == NULL) { cerr << "Bad configuration file : no end to forbiddenExtensions\n"; exit(1); } else { delete [] tok; }}/** connect to this server using connection conn * return the state of the socket */char global::getProxyFds (Connexion *conn) { assert (proxyAddr != NULL); int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { return EMPTY; } conn->socket = fd; for (;;) { fcntl(fd, F_SETFL, O_NONBLOCK); if (connect(fd, (struct sockaddr*) proxyAddr, sizeof(struct sockaddr_in)) == 0) { // success return WRITE; } else if (errno == EINPROGRESS) { // would block return CONNECTING; } else { // error (void) close(fd); return EMPTY; } }}///////////////////////////////////////////////////////////// Struct Connexion////////////////////////////////////////////////////////////** put Connection in a coherent state */Connexion::Connexion () { state = EMPTY; parser = NULL;}/** Destructor : never used : we recycle !!! */Connexion::~Connexion () { cerr << "My god, someone tries to delete a Connexion !\n";}/** Recycle a connexion */void Connexion::recycle () { delete parser; request.recycle();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -