Exemplo n.º 1
0
const char* ConfigurationGetValue(const struct Configuration* configuration, const char* section, const char* key) {
	const struct Table* currentSection = &configuration->root;
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
		if (!currentSection) {
			return 0;
		}
	}
	return HashTableLookup(currentSection, key);
}
Exemplo n.º 2
0
rc_t KVPFileFindOrInsert(KVPFile *self, KVPValue *xval,
                         bool *was_inserted, size_t alloc_size,
                         const void *key, size_t ksize
                        )
{
    KVPValueImpl *val = ( KVPValueImpl* ) xval;
    HashTableIterator iter = HashTableLookup(self->ht, key, ksize);

    if (HashTableIteratorHasValue(&iter)) {
        val->file = (void *)self;
        val->offset = (intptr_t)HashTableIteratorGetValue(&iter);
        val->size = 1;
        *was_inserted = false;
        return 0;
    }
    else {
        size_t new_id = self->next_obj++;
        my_key_t *new_key = KTempMMapPointer(&self->keys, new_id, 1);

        new_key->len = ksize;
        memmove(new_key->key, key, ksize);

        *was_inserted = true;
        val->file = (void *)self;
        val->offset = new_id;
        val->size = 1;
        HashTableIteratorSetValue(&iter, (void *)(val->offset));
    }
    return 0;
}
Exemplo n.º 3
0
void DigestCacheSet(DigestCache* self, const char* filename, uint32_t hash, uint64_t timestamp, const HashDigest& digest)
{
  ReadWriteLockWrite(&self->m_Lock);

  DigestCacheRecord* r;

  if (nullptr != (r = (DigestCacheRecord*) HashTableLookup(&self->m_Table, hash, filename)))
  {
    r->m_Timestamp     = timestamp;
    r->m_ContentDigest = digest;
    r->m_AccessTime    = self->m_AccessTime;
  }
  else
  {
    r = LinearAllocate<DigestCacheRecord>(&self->m_Allocator);
    r->m_Hash          = hash;
    r->m_ContentDigest = digest;
    r->m_Next          = nullptr;
    r->m_String        = StrDup(&self->m_Allocator, filename);
    r->m_Timestamp     = timestamp;
    r->m_AccessTime    = self->m_AccessTime;
    HashTableInsert(&self->m_Table, r);
  }

  ReadWriteUnlockWrite(&self->m_Lock);
}
Exemplo n.º 4
0
static FunctionMeta* FindFunction(lua_State* L, lua_Debug* ar)
{
  // This is slow, it involves string formatting. It's mostly OK, because we're
  // careful not to include this in the timings. It will of course affect cache
  // and other things. Not that we can make a lot of informed decisions about
  // that in an interpreted language anyway.
  if (!lua_getinfo(L, "Sn", ar))
    Croak("couldn't get debug info for function call");

  char buffer[1024];
  snprintf(buffer, sizeof(buffer), "%s;%s;%s;%d",
      ar->name ? ar->name : "", ar->namewhat ? ar->namewhat : "", ar->source, ar->linedefined);
  buffer[(sizeof buffer)-1] = 0;

  const uint32_t hash = Djb2Hash(buffer);
  HashRecord* r = HashTableLookup(&s_Profiler.m_Functions, hash, buffer);

  if (!r) {
    r = LinearAllocate<FunctionMeta>(s_Profiler.m_Allocator);
    r->m_Hash   = hash;
    r->m_String = StrDup(s_Profiler.m_Allocator, buffer);
    r->m_Next   = nullptr;
    HashTableInsert(&s_Profiler.m_Functions, r);
  }
  
  return static_cast<FunctionMeta*>(r);
}
Exemplo n.º 5
0
static int HashTableTestFull02 (void)
{
    int result = 0;
    HashTable *ht = HashTableInit(32, HashTableGenericHash, NULL, NULL);
    if (ht == NULL)
        goto end;

    int r = HashTableAdd(ht, "test", 4);
    if (r != 0)
        goto end;

    char *rp = HashTableLookup(ht, "test", 4);
    if (rp == NULL)
        goto end;

    r = HashTableRemove(ht, "test2", 5);
    if (r == 0)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (ht != NULL) HashTableFree(ht);
    return result;
}
Exemplo n.º 6
0
void ConfigurationEnumerate(const struct Configuration* configuration, const char* section, void (*handler)(const char* key, const char* value, void* user), void* user) {
	struct ConfigurationHandlerData handlerData = { handler, user };
	const struct Table* currentSection = &configuration->root;
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
	}
	if (currentSection) {
		HashTableEnumerate(currentSection, _enumHandler, &handlerData);
	}
}
Exemplo n.º 7
0
void ConfigurationClearValue(struct Configuration* configuration, const char* section, const char* key) {
	struct Table* currentSection = &configuration->root;
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
		if (!currentSection) {
			return;
		}
	}
	HashTableRemove(currentSection, key);
}
Exemplo n.º 8
0
/**
 * \test Check if the reference info from the reference.config file have
 *       been loaded into the hash table.
 */
int SCRConfTest06(void)
{
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
    SCRConfReference *ref = NULL;
    int result = 1;

    if (de_ctx == NULL)
        return 0;

    SCRConfGenerateInValidDummyReferenceConfigFD02();
    SCRConfLoadReferenceConfigFile(de_ctx);
    SCRConfDeleteDummyReferenceConfigFD();

    if (de_ctx->reference_conf_ht == NULL)
        goto end;

    result = (de_ctx->reference_conf_ht->count == 1);

    ref = SCRConfAllocSCRConfReference("one", "one");
    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) != NULL);
    SCRConfDeAllocSCRConfReference(ref);

    ref = SCRConfAllocSCRConfReference("two", "two");
    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL);
    SCRConfDeAllocSCRConfReference(ref);

    ref = SCRConfAllocSCRConfReference("three", "three");
    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL);
    SCRConfDeAllocSCRConfReference(ref);

    ref = SCRConfAllocSCRConfReference("four", "four");
    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL);
    SCRConfDeAllocSCRConfReference(ref);

    ref = SCRConfAllocSCRConfReference("five", "five");
    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL);
    SCRConfDeAllocSCRConfReference(ref);

 end:
    if (de_ctx != NULL)
        DetectEngineCtxFree(de_ctx);
    return result;
}
Exemplo n.º 9
0
/**
 * \brief Parses a line from the reference config file and adds it to Reference
 *        Config hash table DetectEngineCtx->reference_conf_ht.
 *
 * \param rawstr Pointer to the string to be parsed.
 * \param de_ctx Pointer to the Detection Engine Context.
 *
 * \retval  0 On success.
 * \retval -1 On failure.
 */
static int SCRConfAddReference(char *rawstr, DetectEngineCtx *de_ctx)
{
    char system[64];
    char url[1024];

    SCRConfReference *ref_new = NULL;
    SCRConfReference *ref_lookup = NULL;

#define MAX_SUBSTRINGS 30
    int ret = 0;
    int ov[MAX_SUBSTRINGS];

    ret = pcre_exec(regex, regex_study, rawstr, strlen(rawstr), 0, 0, ov, 30);
    if (ret < 0) {
        SCLogError(SC_ERR_REFERENCE_CONFIG, "Invalid Reference Config in "
                   "reference.config file");
        goto error;
    }

    /* retrieve the reference system */
    ret = pcre_copy_substring((char *)rawstr, ov, 30, 1, system, sizeof(system));
    if (ret < 0) {
        SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring() failed");
        goto error;
    }

    /* retrieve the reference url */
    ret = pcre_copy_substring((char *)rawstr, ov, 30, 2, url, sizeof(url));
    if (ret < 0) {
        SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring() failed");
        goto error;
    }

    /* Create a new instance of the parsed Reference string */
    ref_new = SCRConfAllocSCRConfReference(system, url);
    if (ref_new == NULL)
        goto error;

    /* Check if the Reference is present in the HashTable.  In case it's present
     * ignore it, as it's a duplicate.  If not present, add it to the table */
    ref_lookup = HashTableLookup(de_ctx->reference_conf_ht, ref_new, 0);
    if (ref_lookup == NULL) {
        if (HashTableAdd(de_ctx->reference_conf_ht, ref_new, 0) < 0) {
            SCLogDebug("HashTable Add failed");
        }
    } else {
        SCLogDebug("Duplicate reference found inside reference.config");
        SCRConfDeAllocSCRConfReference(ref_new);
    }

    return 0;

 error:
    return -1;
}
Exemplo n.º 10
0
rc_t KVPFileFind(const KVPFile *self, KVPValue *xval, const void *key, size_t ksize)
{
    KVPValueImpl *val = ( KVPValueImpl* ) xval;
    const HashTableIterator iter = HashTableLookup(self->ht, key, ksize);
    if (HashTableIteratorHasValue(&iter)) {
        val->file = (void *)self;
        val->offset = (intptr_t)HashTableIteratorGetValue(&iter);
        val->size = 1;
        return 0;
    }
    return RC(rcApp, rcMemMap, rcReading, rcId, rcNotFound);
}
Exemplo n.º 11
0
/**
 * \brief Gets the refernce config from the corresponding hash table stored
 *        in the Detection Engine Context's reference conf ht, given the
 *        reference name.
 *
 * \param ct_name Pointer to the reference name that has to be looked up.
 * \param de_ctx  Pointer to the Detection Engine Context.
 *
 * \retval lookup_rconf_info Pointer to the SCRConfReference instance from
 *                           the hash table on success; NULL on failure.
 */
SCRConfReference *SCRConfGetReference(const char *rconf_name,
                                      DetectEngineCtx *de_ctx)
{
    SCRConfReference *ref_conf = SCRConfAllocSCRConfReference(rconf_name, NULL);
    if (ref_conf == NULL)
        return NULL;
    SCRConfReference *lookup_ref_conf = HashTableLookup(de_ctx->reference_conf_ht,
                                                        ref_conf, 0);

    SCRConfDeAllocSCRConfReference(ref_conf);
    return lookup_ref_conf;
}
Exemplo n.º 12
0
/**
 * \brief Gets the classtype from the corresponding hash table stored
 *        in the Detection Engine Context's class conf ht, given the
 *        classtype name.
 *
 * \param ct_name Pointer to the classtype name that has to be looked up.
 * \param de_ctx  Pointer to the Detection Engine Context.
 *
 * \retval lookup_ct_info Pointer to the SCClassConfClasstype instance from
 *                        the hash table on success; NULL on failure.
 */
SCClassConfClasstype *SCClassConfGetClasstype(const char *ct_name,
        DetectEngineCtx *de_ctx)
{
    SCClassConfClasstype *ct_info = SCClassConfAllocClasstype(0, ct_name, NULL,
                                    0);
    if (ct_info == NULL)
        return NULL;
    SCClassConfClasstype *lookup_ct_info = HashTableLookup(de_ctx->class_conf_ht,
                                           ct_info, 0);

    SCClassConfDeAllocClasstype(ct_info);
    return lookup_ct_info;
}
Exemplo n.º 13
0
void
HashTableInsert(HashTable_t* h, HashEntry_t* e)
{
	HashEntry_t *slot = HashTableLookup(h,e->key,1);

	if (slot->key != 0xffffffff)
	{
		printf("Slot already used\n");
		exit(-1);
	}
	slot->key = e->key;
	slot->data = e->data;
}
Exemplo n.º 14
0
/**
 * \brief Gets the classtype from the corresponding hash table stored
 *        in the Detection Engine Context's class conf ht, given the
 *        classtype name.
 *
 * \param ct_name Pointer to the classtype name that has to be looked up.
 * \param de_ctx  Pointer to the Detection Engine Context.
 *
 * \retval lookup_ct_info Pointer to the SCClassConfClasstype instance from
 *                        the hash table on success; NULL on failure.
 */
SCClassConfClasstype *SCClassConfGetClasstype(const char *ct_name,
                                              DetectEngineCtx *de_ctx)
{
    char name[strlen(ct_name) + 1];
    size_t s;
    for (s = 0; s < strlen(ct_name); s++)
        name[s] = tolower((unsigned char)ct_name[s]);
    name[s] = '\0';

    SCClassConfClasstype ct_lookup = {0, name, NULL, 0 };
    SCClassConfClasstype *lookup_ct_info = HashTableLookup(de_ctx->class_conf_ht,
                                                           &ct_lookup, 0);
    return lookup_ct_info;
}
Exemplo n.º 15
0
static NTSTATUS
GnttabUnmapForeignPages(
    IN  PINTERFACE              Interface,
    IN  PHYSICAL_ADDRESS        Address
    )
{
    PXENBUS_GNTTAB_CONTEXT      Context = Interface->Context;
    ULONG                       PageIndex;
    PHYSICAL_ADDRESS            PageAddress;
    PXENBUS_GNTTAB_MAP_ENTRY    MapEntry;
    NTSTATUS                    status;

    status = HashTableLookup(Context->MapTable,
                             (ULONG_PTR)Address.QuadPart,
                             (PULONG_PTR)&MapEntry);
    if (!NT_SUCCESS(status))
        goto fail1;

    status = HashTableRemove(Context->MapTable,
                             (ULONG_PTR)Address.QuadPart);
    if (!NT_SUCCESS(status))
        goto fail2;

    PageAddress.QuadPart = Address.QuadPart;

    for (PageIndex = 0; PageIndex < MapEntry->NumberPages; PageIndex++) {
        status = GrantTableUnmapForeignPage(MapEntry->MapHandles[PageIndex],
                                            PageAddress);
        BUG_ON(!NT_SUCCESS(status));

        PageAddress.QuadPart += PAGE_SIZE;
    }

    FdoFreeIoSpace(Context->Fdo,
                   Address,
                   MapEntry->NumberPages * PAGE_SIZE);

    __GnttabFree(MapEntry);

    return STATUS_SUCCESS;

fail2:
    Error("fail2\n");

fail1:
    Error("fail1: (%08x)\n", status);

    return status;
}
Exemplo n.º 16
0
bool ConfigurationWriteSection(const struct Configuration* configuration, const char* path, const char* section) {
	const struct Table* currentSection = &configuration->root;
	FILE* file = fopen(path, "w");
	if (!file) {
		return false;
	}
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
		fprintf(file, "[%s]\n", section);
	}
	if (currentSection) {
		HashTableEnumerate(currentSection, _sectionHandler, file);
	}
	fclose(file);
	return true;
}
Exemplo n.º 17
0
void ConfigurationSetValue(struct Configuration* configuration, const char* section, const char* key, const char* value) {
	struct Table* currentSection = &configuration->root;
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
		if (!currentSection) {
			if (value) {
				currentSection = malloc(sizeof(*currentSection));
				HashTableInit(currentSection, 0, _sectionDeinit);
				HashTableInsert(&configuration->sections, section, currentSection);
			} else {
				return;
			}
		}
	}
	if (value) {
		HashTableInsert(currentSection, key, strdup(value));
	} else {
		HashTableRemove(currentSection, key);
	}
}
Exemplo n.º 18
0
bool ConfigurationWriteSection(const struct Configuration* configuration, const char* path, const char* section) {
	const struct Table* currentSection = &configuration->root;
	struct VFile* vf = VFileOpen(path, O_WRONLY | O_CREAT | O_APPEND);
	if (!vf) {
		return false;
	}
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
		char line[256];
		size_t len = snprintf(line, sizeof(line), "[%s]\n", section);
		if (len >= sizeof(line)) {
			len = sizeof(line) - 1;
		}
		vf->write(vf, line, len);
	}
	if (currentSection) {
		HashTableEnumerate(currentSection, _sectionHandler, vf);
	}
	vf->close(vf);
	return true;
}
Exemplo n.º 19
0
bool DigestCacheGet(DigestCache* self, const char* filename, uint32_t hash, uint64_t timestamp, HashDigest* digest_out)
{
  bool result = false;

  ReadWriteLockRead(&self->m_Lock);

  if (DigestCacheRecord* r = (DigestCacheRecord*) HashTableLookup(&self->m_Table, hash, filename))
  {
    if (r->m_Timestamp == timestamp)
    {
      // Technically violates r/w lock - doesn't matter
      r->m_AccessTime = self->m_AccessTime;
      *digest_out     = r->m_ContentDigest;
      result          = true;
    }
  }

  ReadWriteUnlockRead(&self->m_Lock);

  return result;
}
Exemplo n.º 20
0
/**
 * \test Check if the classtype info from the invalid classification.config file
 *       have not been loaded into the hash table, and cross verify to check
 *       that the hash table contains no classtype data.
 */
int SCClassConfTest05(void)
{
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
    SCClassConfClasstype *ct = NULL;
    int result = 1;

    if (de_ctx == NULL)
        return 0;

    SCClassConfGenerateInValidDummyClassConfigFD03();
    SCClassConfLoadClassficationConfigFile(de_ctx);
    SCClassConfDeleteDummyClassificationConfigFD();

    if (de_ctx->class_conf_ht == NULL)
        return 0;

    result = (de_ctx->class_conf_ht->count == 0);

    ct = SCClassConfAllocClasstype(0, "unknown", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "unKnoWn", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "bamboo", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "bad-unknown", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "BAD-UNKnOWN", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "bed-unknown", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    DetectEngineCtxFree(de_ctx);

    return result;
}
Exemplo n.º 21
0
/**
 * \test Check if the classtype info from the classification.config file have
 *       been loaded into the hash table.
 */
int SCClassConfTest06(void)
{
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
    SCClassConfClasstype *ct = NULL;
    int result = 1;

    if (de_ctx == NULL)
        return 0;

    SCClassConfGenerateInValidDummyClassConfigFD02();
    SCClassConfLoadClassficationConfigFile(de_ctx);
    SCClassConfDeleteDummyClassificationConfigFD();

    if (de_ctx->class_conf_ht == NULL)
        return 0;

    result = (de_ctx->class_conf_ht->count == 3);

    ct = SCClassConfAllocClasstype(0, "unknown", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "not-suspicious", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "bamboola1", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "bamboola1", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "BAMBOolA1", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
    SCClassConfDeAllocClasstype(ct);

    ct = SCClassConfAllocClasstype(0, "unkNOwn", NULL, 0);
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
    SCClassConfDeAllocClasstype(ct);

    DetectEngineCtxFree(de_ctx);

    return result;
}
Exemplo n.º 22
0
bool ConfigurationHasSection(const struct Configuration* configuration, const char* section) {
	return HashTableLookup(&configuration->sections, section);
}
Exemplo n.º 23
0
char *var(char *name) {
  char *ret;
  if (!symtab) { shellinit(); return nullstring; }
  ret = HashTableLookup(symtab,name);
  return ret ? ret : nullstring;
}
Exemplo n.º 24
0
int main(int argc, char* argv[])
{
    
    //Check for the number of arguments
    if(argc != 4){
	    printf("Invalid Input Argument\n");
	    printHelp();
        exit(1);
    } 
   
    //direcotry file path
    int dirSize = strlen(argv[2]);
    char dir[dirSize + 1];
    dir[0] = '\0';
    strcat(dir, argv[2]);

    int urlSize = strlen(argv[1]);
    char inputURL[urlSize + 1];
    inputURL[0] = '\0';
    strcat(inputURL, argv[1]);

    //Get the max depth number.
    int inputDepth = atoi(argv[3]);

    //Check if correct depth is provided.
    if(inputDepth > 4 || inputDepth < 0){
        printf("Invalid [depth]\n");
        printHelp();
        exit(1);
    }
    //Check for URL validity 
    if(!strstr(inputURL,URL_PREFIX)){
 	    printf("Invalid input [seed url]\n");
        printHelp();
	    exit(1);
    }
    //checkf for directory location validity
    DIR* directory = opendir(dir);
    if(directory){
	    closedir(directory);
    }
    else if(ENOENT == errno){
	    printf("Directory does not exist\n");
	    printHelp();
        exit(1);
    }
    else{
	    printf("Directory can't be opened\n");
        printHelp();
	    exit(1);
    }

    // init curl
    curl_global_init(CURL_GLOBAL_ALL);

    // setup seed page
    WebPage* seedWebPage = calloc(1, sizeof(WebPage));//Memory allocation for seed webpage
    seedWebPage->url = calloc((strlen(inputURL) + 1), sizeof(char));//Memory allocation to the seedURL
    seedWebPage->url[0] = '\0';
    strcat(seedWebPage->url, inputURL);
    seedWebPage->depth = 0;
    seedWebPage->html = NULL;
    
    //Initialize data structures
    HashTable* visitedURLHash = initHashTable();
    List* webPageList = initializeList();
    webPageList->head->page = seedWebPage;  
 
    //get seed webpage.
    if(GetWebPage(seedWebPage)){	
        // write seed file
        FILE *fPointer;
        char* pathVar1 = pathToDirectory(dir, fileNumber);
        fPointer = fopen(pathVar1, "w");
        free(pathVar1);	
        writeHTMLtoFile(fPointer, seedWebPage);
        //free(fPointer);
        
        if(inputDepth == 0){
            curl_global_cleanup();
            free(seedWebPage->html);
            free(seedWebPage->url);
            free(seedWebPage);

            //free webPageList and hashtable
            free(webPageList);
            for(int i = 0; i < MAX_HASH_SLOT; i++){
                free(visitedURLHash->table[i]->url);
                free(visitedURLHash->table[i]);
            }
            free(visitedURLHash);
            return 0;
        }   
        fileNumber += 1;
        depth += 1;
        HashTableInsert(visitedURLHash, seedWebPage->url); //mark as visited
        
        // extract urls from seed page
        char * result;
        int pos = 0;
        while((pos = GetNextURL(seedWebPage->html, pos, seedWebPage->url, &result))>0){

            if(NormalizeURL(result) && strstr(result,URL_PREFIX)){
                strtok(result, "#");
                //If not in hashtable, add it to the hashtable and add it to the webPageList.
                if(HashTableLookup(visitedURLHash, result) == 0){
                    HashTableInsert(visitedURLHash, result);
                    AppendList(webPageList, webPageInit(result, depth));
                    free(result);
                }
            }
        }
        if(webPageList->head->next->next == NULL){  //seed redirect case
            webPageList->head->next->page->depth = 0;
            fileNumber = 1;		
        }
        tempWebPage = PopList(webPageList); // Get rid of visited seedPage
    }
    else{	
        curl_global_cleanup();
        tempWebPage = PopList(webPageList);
        free(seedWebPage->html);
        free(seedWebPage->url);
        free(seedWebPage);
        //free(tempWebPage);
        free(webPageList);
        for(int i = 0; i < MAX_HASH_SLOT; i++){
            free(visitedURLHash->table[i]->url);
            free(visitedURLHash->table[i]);
        }
        free(visitedURLHash);
        exit(1);
    }

    
    //while there are urls to crawl
    while(webPageList->head != NULL && webPageList->tail != NULL){
        // get webpage for url
        tempWebPage = PopList(webPageList);
        if(GetWebPage(tempWebPage)){ 
            // write page file
            char* pathVar = pathToDirectory(dir, fileNumber);
            FILE *fPointer = fopen(pathVar, "w");
            free(pathVar);
            printf("Found link: %s\n",tempWebPage->url);
            writeHTMLtoFile(fPointer, tempWebPage);
            fileNumber += 1;
                
            if((tempWebPage->depth + 1) <= inputDepth ){
                char * resultTemp;
                int posTemp = 0;
                while((posTemp = GetNextURL(tempWebPage->html, posTemp, tempWebPage->url, &resultTemp))>0){
                    
                    if( NormalizeURL(resultTemp) && strstr(resultTemp,URL_PREFIX) ){
                        strtok(resultTemp, "#");
                        //insert to the hashtable and the webPageList if not already present
                        if(HashTableLookup(visitedURLHash, resultTemp) == 0){
                            HashTableInsert(visitedURLHash, resultTemp);
                            AppendList(webPageList, webPageInit(resultTemp, tempWebPage->depth+1));
                        }
                    }
                        free(resultTemp);
                }
            }
        
            free(tempWebPage->url);
            free(tempWebPage->html);
            free(tempWebPage);
        }
        else{
            free(tempWebPage->url);
            free(tempWebPage->html);
            free(tempWebPage);
        }
        sleep(INTERVAL_PER_FETCH);
    }
    // cleanup curl
    curl_global_cleanup();
    free(seedWebPage->url);
    free(seedWebPage->html);
    free(seedWebPage);
    free(webPageList);

    //free the hashtable
    for(int i = 0; i < MAX_HASH_SLOT; i++){
        if(visitedURLHash->table[i]->url != NULL){
            HashTableNode* currNode = visitedURLHash->table[i];
            while(currNode->next != NULL){
                HashTableNode* tempNode = currNode;
                currNode = currNode->next;
                free(tempNode->url);
                free(tempNode);
            }
            free(currNode->url);
            free(currNode);		
        }
        else{	
            free(visitedURLHash->table[i]);
        }
    }
    free(visitedURLHash);
    return 0;
}
Exemplo n.º 25
0
/**
 * \brief Parses a line from the classification file and adds it to Classtype
 *        hash table in DetectEngineCtx, i.e. DetectEngineCtx->class_conf_ht.
 *
 * \param rawstr Pointer to the string to be parsed.
 * \param index  Relative index of the string to be parsed.
 * \param de_ctx Pointer to the Detection Engine Context.
 *
 * \retval  0 On success.
 * \retval -1 On failure.
 */
int SCClassConfAddClasstype(char *rawstr, uint8_t index, DetectEngineCtx *de_ctx)
{
    const char *ct_name = NULL;
    const char *ct_desc = NULL;
    const char *ct_priority_str = NULL;
    int ct_priority = 0;
    uint8_t ct_id = index;

    SCClassConfClasstype *ct_new = NULL;
    SCClassConfClasstype *ct_lookup = NULL;

#define MAX_SUBSTRINGS 30
    int ret = 0;
    int ov[MAX_SUBSTRINGS];

    ret = pcre_exec(regex, regex_study, rawstr, strlen(rawstr), 0, 0, ov, 30);
    if (ret < 0) {
        SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid Classtype in "
                   "classification.config file");
        goto error;
    }

    /* retrieve the classtype name */
    ret = pcre_get_substring((char *)rawstr, ov, 30, 1, &ct_name);
    if (ret < 0) {
        SCLogInfo("pcre_get_substring() failed");
        goto error;
    }

    /* retrieve the classtype description */
    ret = pcre_get_substring((char *)rawstr, ov, 30, 2, &ct_desc);
    if (ret < 0) {
        SCLogInfo("pcre_get_substring() failed");
        goto error;
    }

    /* retrieve the classtype priority */
    ret = pcre_get_substring((char *)rawstr, ov, 30, 3, &ct_priority_str);
    if (ret < 0) {
        SCLogInfo("pcre_get_substring() failed");
        goto error;
    }
    if (ct_priority_str == NULL) {
        goto error;
    }

    ct_priority = atoi(ct_priority_str);

    /* Create a new instance of the parsed Classtype string */
    ct_new = SCClassConfAllocClasstype(ct_id, ct_name, ct_desc, ct_priority);
    if (ct_new == NULL)
        goto error;

    /* Check if the Classtype is present in the HashTable.  In case it's present
     * ignore it, as it is a duplicate.  If not present, add it to the table */
    ct_lookup = HashTableLookup(de_ctx->class_conf_ht, ct_new, 0);
    if (ct_lookup == NULL) {
        if (HashTableAdd(de_ctx->class_conf_ht, ct_new, 0) < 0)
            SCLogDebug("HashTable Add failed");
    } else {
        SCLogDebug("Duplicate classtype found inside classification.config");
        if (ct_new->classtype_desc) SCFree(ct_new->classtype_desc);
        if (ct_new->classtype) SCFree(ct_new->classtype);
        SCFree(ct_new);
    }

    if (ct_name) SCFree((char *)ct_name);
    if (ct_desc) SCFree((char *)ct_desc);
    if (ct_priority_str) SCFree((char *)ct_priority_str);
    return 0;

 error:
    if (ct_name) SCFree((char *)ct_name);
    if (ct_desc) SCFree((char *)ct_desc);
    if (ct_priority_str) SCFree((char *)ct_priority_str);

    return -1;
}