Example #1
0
int destroy_cache(cache_t *c)
{
	printk(1, "[cache]: Destroying cache '%s'...\n", c->name);
	rwlock_acquire(c->rwl, RWL_WRITER);
	chash_t *h = c->hash;
	c->hash = 0;
	sync_cache(c);
	/* Destroy the tree */
	chash_destroy(h);
	
	struct llistnode *curnode, *next;
	struct ce_t *obj;
	ll_for_each_entry_safe(&c->primary_ll, curnode, next, struct ce_t *, obj)
	{
		ll_maybe_reset_loop(&c->primary_ll, curnode, next);
		remove_element(c, obj, 1);
	}
	ll_destroy(&c->dirty_ll);
	ll_destroy(&c->primary_ll);
	ll_remove_entry(cache_list, c);
	rwlock_release(c->rwl, RWL_WRITER);
	rwlock_destroy(c->rwl);
	printk(1, "[cache]: Cache '%s' destroyed\n", c->name);
	return 1;
}
Example #2
0
/**
 * remove all jobs matching the name..
 */ 
int del_cronjob(p_cronjob job){
	p_linkedlist item = ll_create(job, sizeof(t_cronjob));
	p_linkedlist remove	= ll_find(cronlist, item, jobequal);
	while(remove != 0) {
		cronlist = ll_remove(cronlist,remove);
		ll_destroy(remove);
		remove	= ll_find(cronlist, item, jobequal);
	} 

	ll_destroy(item);
}
Example #3
0
void log_list_thread(void)
{
	char buf[LOG_BUF_SIZE];
	log_running = 1;
	set_thread_name(__func__);
	do
	{
		log_list_queued = 0;
		LL_ITER it = ll_iter_create(log_list);
		struct s_log *log;
		while((log = ll_iter_next_remove(&it)))
		{
			int8_t do_flush = ll_count(log_list) == 0; //flush on writing last element

			cs_strncpy(buf, log->txt, LOG_BUF_SIZE);
			if(log->direct_log)
				{ cs_write_log(buf, do_flush); }
			else
				{ write_to_log(buf, log, do_flush); }
			NULLFREE(log->txt);
			NULLFREE(log);
		}
		if(!log_list_queued)  // The list is empty, sleep until new data comes in and we are woken up
			sleepms_on_cond(&log_thread_sleep_cond_mutex, &log_thread_sleep_cond, 60 * 1000);
	}
	while(log_running);
	ll_destroy(log_list);
	log_list = NULL;
}
Example #4
0
int32_t init_free_userdb(struct s_auth *ptr)
{
	int32_t nro;
	for(nro = 0; ptr; nro++)
	{
		struct s_auth *ptr_next;
		ptr_next = ptr->next;
		ll_destroy(&ptr->aureader_list);
		ptr->next = NULL;
		config_list_gc_values(account_opts, ptr);
		ftab_clear(&ptr->ftab);
		ftab_clear(&ptr->fchid);
		tuntab_clear(&ptr->ttab);
		caidtab_clear(&ptr->ctab);
    	NULLFREE(ptr->cltab.aclass);
 		NULLFREE(ptr->cltab.bclass);
#ifdef CS_CACHEEX
		cecspvaluetab_clear(&ptr->cacheex.filter_caidtab);
#endif
#ifdef WITH_LB
		caidvaluetab_clear(&ptr->lb_nbest_readers_tab);
#endif
		add_garbage(ptr);
		ptr = ptr_next;
	}
	cs_log("userdb %d accounts freed", nro);
	return nro;
}
Example #5
0
int top_create(char *name, char *schema) {
    Topic *st;
    int ncells;
    char bf[1024];

    if (tshm_containsKey(topicTable, name))	/* topic already defined */
        return 0;
    st = (Topic *)malloc(sizeof(Topic));
    if (st != NULL) {
        void *dummy;
        strcpy(bf, schema);
        pthread_mutex_init(&(st->lock), NULL);
        st->schema = unpack(bf, &ncells);
        if (st->schema != NULL) {
            st->ncells = ncells;
            st->regAUs = ll_create();
            if (st->regAUs != NULL) {
                if (tshm_put(topicTable, name, st, &dummy))
                    return 1;
                ll_destroy(st->regAUs, NULL);
            }
            free((void *)(st->schema));
        }
        free((void *)st);
    }
    return 0;
}
Example #6
0
File: parser.c Project: fwum/fwum
static statement *parse_func_call(linked_list *tokens) {
	linked_list *accumulator = ll_new();
	linked_iter iterator = ll_iter_head(tokens);
	statement *call = new(call);
	call->type = FUNC_CALL;
	call->data = ((parse_token*)ll_iter_next(&iterator))->data;
	call->children = ll_new();
	int paren_level = 0;
	ll_iter_next(&iterator); //Discard opening parenthesis
	for(parse_token *current = ll_iter_next(&iterator); ll_iter_has_next(&iterator); current = ll_iter_next(&iterator)) {
		if(paren_level == 0 && (equals_string(current->data, ",") || equals_string(current->data, ")"))) {
			statement *parameter = parse_simple_expression(accumulator);
			if(parameter != NULL)
				ll_add_last(call->children, parameter);
			ll_clear(accumulator);
		} else {
			ll_add_last(accumulator, current);
		}
		if(equals_string(current->data, "("))
			paren_level += 1;
		else if(equals_string(current->data, ")"))
			paren_level -= 1;
	}
	statement *parameter = parse_simple_expression(accumulator);
	if(parameter != NULL)
		ll_add_last(call->children, parameter);
	ll_destroy(accumulator);
	return call;
}
Example #7
0
int32_t tick (TCPsocket sock, uint32_t add) {
	
	p_linkedlist item, next;
	p_cronjob job;
	
	// increment timestamp
	searchjob.timestamp += add;
	uint32_t timestamp = searchjob.timestamp;
	

	log_message(DEBUG,"Tick : %d, cronlist: %d", timestamp, cronlist);
	/*
	 * handle cron call
	 */ 
	 
	item = cronlist;
	while(item) {
		next = item->next;

		job = (p_cronjob)item->data;
		if (! job) {
			return -1;
		}
		
		if (job->timestamp == timestamp) {
			log_message(DEBUG, "Found a job!");
					
			/*
			 * set message data
			 */
			strncpy(cronmessage.id, job->message, ROCS_IDSIZE);
			strcpy(cronmessage.tail, job->data);  
			rocsmq_send(sock, &cronmessage, 0);
			
			log_message(DEBUG, "message '%s' sent!", cronmessage.id);

			// put new timestamp 
			job->timestamp += job->period;

			// repeat until all repititions done
			if (job->repetitions > 0) {
				job->repetitions --;
		
			// after the last repitition, remove job
			} 
			
			if (job->repetitions == 0){
				log_message(DEBUG, "removing item");
				cronlist = ll_remove(cronlist,item);
				ll_destroy(item);
			}
		
		}
		/*
		 * check next item
		 */ 
		item = next; 
	}
}
Example #8
0
void stack_destroy(stack *s){
    if(!s){
        return;
    }
    
    ll_destroy(s->head);
    free(s);
}
Example #9
0
/* free any resources used by a connection
   these should ALL be allocated within xbee_conNew */
int xbee_conFree(struct xbee *xbee, struct xbee_con *con) {
	if (!xbee) return XBEE_ENOXBEE;
	xsys_mutex_destroy(&con->txMutex);
	xsys_sem_destroy(&con->callbackSem);
	ll_destroy(&con->rxList, (void(*)(void*))xbee_pktFree);
	free(con);
	return XBEE_ENONE;
}
Example #10
0
void queue_destroy(queue *q)
{
	if(!q) {
		return;
	}
	ll_destroy(q->head);

	free(q);
}
Example #11
0
void ht_destroy(struct hash_table *ht){
	int i;

	for(i=0; i<ht->size; i++){
		if(ht->table[i])
            ll_destroy(ht->table[i]);
	}

	free(ht);
	return;
}
Example #12
0
double stack_pop(stack *s){
    if(stack_is_empty(s)){
        return 0;
    }
    double ret = s->head->data;
    struct llist *tmp = s->head;

    s->head = s->head->next;
    tmp->next = NULL;
    ll_destroy(tmp);

    return ret;
}
int32_t init_free_userdb(struct s_auth *ptr) {
	int32_t nro;
	for (nro = 0; ptr; nro++) {
		struct s_auth *ptr_next;
		ptr_next = ptr->next;
		ll_destroy(ptr->aureader_list);
		ptr->next = NULL;
		config_list_gc_values(account_opts, ptr);
		add_garbage(ptr);
		ptr = ptr_next;
	}
	cs_log("userdb %d accounts freed", nro);
	return nro;
}
Example #14
0
/**
 * remove a client from our array of clients
 */
void remove_client_idx(int i) {
	if (i < 0 && i >= num_clients)
		return;

	/* close the old socket, even if it's dead... */
	SDLNet_TCP_Close(clients[i].sock);

	ll_destroy(clients[i].filters);
	num_clients--;
	if (num_clients > i)
		memmove(&clients[i], &clients[i + 1],
				(num_clients - i) * sizeof(t_client));
	clients = (t_client*) realloc(clients, num_clients * sizeof(t_client));
	/* server side info */
	/* inform all clients, excluding the old one, of the disconnected user */
}
Example #15
0
int module_exit()
{
	printk(1, "[ext2]: Unmounting all ext2 filesystems\n");
	int i=0;
	unregister_sbt("ext2");
	if(ll_is_active(fslist)) 
	{
		struct llistnode *cur, *next;
		ext2_fs_t *f=0;
		ll_for_each_entry_safe(fslist, cur, next, ext2_fs_t *, f);
		{
			if(f)
				ext2_unmount(0, f->flag);
		}
	}
	ll_destroy(fslist);
	return 0;
}
Example #16
0
void free_joblist(struct s_client *cl)
{
	pthread_mutex_trylock(&cl->thread_lock);
	LL_ITER it = ll_iter_create(cl->joblist);
	struct job_data *data;
	while((data = ll_iter_next(&it)))
	{
		free_job_data(data);
	}
	ll_destroy(cl->joblist);
	cl->joblist = NULL;
	cl->account = NULL;
	if(cl->work_job_data)  // Free job_data that was not freed by work_thread
		{ free_job_data(cl->work_job_data); }
	cl->work_job_data = NULL;
	pthread_mutex_unlock(&cl->thread_lock);
	pthread_mutex_destroy(&cl->thread_lock);
}
Example #17
0
double queue_dequeue(queue *q)
{
	if(queue_is_empty(q)) {
		return 0;
	}

	double value = q->head->data;
	struct llist *tmp = q->head;

	q->head = tmp->next;
	tmp->next = NULL;
	ll_destroy(tmp);

	if(!q->head) {
		q->tail = NULL;
	}

	return value;
}
Example #18
0
File: parser.c Project: fwum/fwum
static statement *parse_array_index(linked_list *tokens) {
	statement *index = new(index);
	index->type = OP_INDEX;
	index->data = new_slice("");
	index->children = ll_new();
	statement *name = new(name);
	name->type = NAME;
	name->data = ((parse_token*)ll_get_first(tokens))->data;
	name->children = NULL;
	ll_add_first(index->children, name);
	linked_list *inside_list = ll_duplicate(tokens);
	ll_remove_first(inside_list); //Remove name
	ll_remove_first(inside_list); //Remove [
	ll_remove_last(inside_list); //Remove ]
	statement *inside = parse_simple_expression(inside_list);
	ll_destroy(inside_list);
	ll_add_last(index->children, inside);
	return index;
}
Example #19
0
static void ghttp_cleanup(struct s_client *client)
{
	s_ghttp *context = (s_ghttp *)client->ghttp;

	if(context)
	{
		NULLFREE(context->session_id);
		NULLFREE(context->host_id);
		NULLFREE(context->fallback_id);
		if(context->ecm_q) { ll_destroy(context->ecm_q); }
		if(context->post_contexts) { ll_destroy_data(context->post_contexts); }
#ifdef WITH_SSL
		if(context->ssl_handle)
		{
			SSL_shutdown(context->ssl_handle);
			SSL_free(context->ssl_handle);
		}
#endif
		NULLFREE(context);
	}
}
Example #20
0
void free_joblist(struct s_client *cl)
{
	int32_t lock_status = pthread_mutex_trylock(&cl->thread_lock);
	
	LL_ITER it = ll_iter_create(cl->joblist);
	struct job_data *data;
	while((data = ll_iter_next(&it)))
	{
		free_job_data(data);
	}
	ll_destroy(&cl->joblist);
	cl->account = NULL;
	if(cl->work_job_data)  // Free job_data that was not freed by work_thread
		{ free_job_data(cl->work_job_data); }
	cl->work_job_data = NULL;
	
	if(lock_status == 0)
		{ SAFE_MUTEX_UNLOCK(&cl->thread_lock); }
	
	pthread_mutex_destroy(&cl->thread_lock);
}
Example #21
0
void KSegmentorBase::CreateLLs(LLset& ll)
{
    if (ll.Lz!=NULL){
        //destroy linked lists
        ll_destroy(ll.Lz);
        ll_destroy(ll.Ln1);
        ll_destroy(ll.Ln2);
        ll_destroy(ll.Lp1);
        ll_destroy(ll.Lp2);
        ll_destroy(ll.Lin2out);
        ll_destroy(ll.Lout2in);
        ll_destroy(ll.Lchanged);
    }

    //create linked lists
    ll.Lz  = ll_create();
    ll.Ln1 = ll_create();
    ll.Ln2 = ll_create();
    ll.Lp1 = ll_create();
    ll.Lp2 = ll_create();
    ll.Lin2out = ll_create();
    ll.Lout2in = ll_create();
    ll.Lchanged =ll_create();
}
Example #22
0
int main(int argc, char *argv[]) {
    char buf[1024];
    char *p;
    LinkedList *ll;
    long i, n;
    FILE *fd;
    char **array;
    Iterator *it;

    if (argc != 2) {
        fprintf(stderr, "usage: ./lltest file\n");
        return -1;
    }
    if ((ll = ll_create()) == NULL) {
        fprintf(stderr, "Error creating linked list of strings\n");
        return -1;
    }
    if ((fd = fopen(argv[1], "r")) == NULL) {
        fprintf(stderr, "Unable to open %s to read\n", argv[1]);
        return -1;
    }
    /*
     * test of add()
     */
    printf("===== test of add\n");
    while (fgets(buf, 1024, fd) != NULL) {
        if ((p = strdup(buf)) == NULL) {
            fprintf(stderr, "Error duplicating string\n");
            return -1;
        }
        if (!ll_add(ll, p)) {
            fprintf(stderr, "Error adding string to linked list\n");
            return -1;
        }
    }
    fclose(fd);
    n = ll_size(ll);
    /*
     * test of get()
     */
    printf("===== test of get\n");
    for (i = 0; i < n; i++) {
        if (!ll_get(ll, i, (void **)&p)) {
            fprintf(stderr, "Error retrieving %ld'th element\n", i);
            return -1;
        }
        printf("%s", p);
    }
    /*
     * test of remove
     */
    printf("===== test of remove\n");
    for (i = n - 1; i >= 0; i--) {
        if (!ll_remove(ll, i, (void **)&p)) {
            fprintf(stderr, "Error removing string from linked list\n");
            return -1;
        }
        free(p);
    }
    /*
     * test of destroy with NULL userFunction
     */
    printf("===== test of destroy(NULL)\n");
    ll_destroy(ll, NULL);
    /*
     * test of insert
     */
    if ((ll = ll_create()) == NULL) {
        fprintf(stderr, "Error creating linked list of strings\n");
        return -1;
    }
    fd = fopen(argv[1], "r");		/* we know we can open it */
    printf("===== test of insert\n");
    while (fgets(buf, 1024, fd) != NULL) {
        if ((p = strdup(buf)) == NULL) {
            fprintf(stderr, "Error duplicating string\n");
            return -1;
        }
        if (!ll_insert(ll, 0, p)) {
            fprintf(stderr, "Error adding string to linked list\n");
            return -1;
        }
    }
    fclose(fd);
    for (i = 0; i < n; i++) {
        if (!ll_get(ll, i, (void **)&p)) {
            fprintf(stderr, "Error retrieving %ld'th element\n", i);
            return -1;
        }
        printf("%s", p);
    }
    /*
     * test of set
     */
    printf("===== test of set\n");
    for (i = 0; i < n; i++) {
        char bf[1024], *q;
        sprintf(bf, "line %ld\n", i);
        if ((p = strdup(bf)) == NULL) {
            fprintf(stderr, "Error duplicating string\n");
            return -1;
        }
        if (!ll_set(ll, i, p, (void **)&q)) {
            fprintf(stderr, "Error replacing %ld'th element\n", i);
            return -1;
        }
        free(q);
    }
    /*
     * test of toArray
     */
    printf("===== test of toArray\n");
    if ((array = (char **)ll_toArray(ll, &n)) == NULL) {
        fprintf(stderr, "Error in invoking ll_toArray()\n");
        return -1;
    }
    for (i = 0; i < n; i++) {
        printf("%s", array[i]);
    }
    free(array);
    /*
     * test of iterator
     */
    printf("===== test of iterator\n");
    if ((it = ll_it_create(ll)) == NULL) {
        fprintf(stderr, "Error in creating iterator\n");
        return -1;
    }
    while (it_hasNext(it)) {
        char *p;
        (void) it_next(it, (void **)&p);
        printf("%s", p);
    }
    it_destroy(it);
    /*
     * test of destroy with free() as userFunction
     */
    printf("===== test of destroy(free)\n");
    ll_destroy(ll, free);

    return 0;
}
Example #23
0
int q_destroy(queue_t * q) {
  return ll_destroy(q);
}
Example #24
0
KSegmentor3D::~KSegmentor3D(){

  //delete [] this->mdims;//Causes trouble! Haveto find out why!!
  delete [] this->img;
  delete [] this->mask;
  delete [] this->imgRange;
  delete [] this->labelRange;
  delete [] this->phi;
  delete [] this->label;

  LL *Lz, *Ln1, *Ln2, *Lp1, *Lp2;
  LL *Lin2out, *Lout2in,*Lchanged;


  Lz=LL3D.Lz;
  Ln1=LL3D.Ln1;
  Ln2=LL3D.Ln2;
  Lp1=LL3D.Lp1;
  Lp2=LL3D.Lp2;
  Lin2out=LL3D.Lin2out;
  Lout2in=LL3D.Lout2in;
  Lchanged=LL3D.Lchanged;

  ll_destroy(Lz);
  ll_destroy(Ln1);
  ll_destroy(Ln2);
  ll_destroy(Lp1);
  ll_destroy(Lp2);
  ll_destroy(Lin2out);
  ll_destroy(Lout2in);
  ll_destroy(Lchanged);

  Lz=LL2D.Lz;
  Ln1=LL2D.Ln1;
  Ln2=LL2D.Ln2;
  Lp1=LL2D.Lp1;
  Lp2=LL2D.Lp2;
  Lin2out=LL2D.Lin2out;
  Lout2in=LL2D.Lout2in;
  Lchanged=LL2D.Lchanged;

  ll_destroy(Lz);
  ll_destroy(Ln1);
  ll_destroy(Ln2);
  ll_destroy(Lp1);
  ll_destroy(Lp2);
  ll_destroy(Lin2out);
  ll_destroy(Lout2in);
  ll_destroy(Lchanged);
}
Example #25
0
int main(int argc, char *argv[])
{
	int		s;
	struct Stmt	*stmt	= 0;

	s = vos_init(argc, argv);
	if (s)
		goto err;

	s = vos_parsing(&stmt, _vos.script);
	if (s)
		goto err;

	if (! (_vos.debug & DBG_SCRIPT))
		s = vos_process(stmt);

	if (! s)
		goto out;

err:
	switch (s) {
	case E_VOS_PARAM:
	case E_FILE_NOT_OPEN:
	case E_MEM:
		vos_error0(s);
		break;
	case E_FILE_OPEN:
	case E_FILE_EXIST:
	case E_FILE_NOT_EXIST:
	case E_FILE_SEEK:
	case E_FILE_READ:
	case E_FILE_WRITE:
		vos_error1(s, _vos.e_sparm0);
		break;
	case E_PARSER_INV_POS:
		vos_error1(s, _vos.e_nparm0);
		break;
	case E_PARSER_UNX_CHAR:
		vos_error2(s, _vos.e_nparm0, _vos.e_nparm1);
		break;
	case E_PARSER_UNX_TOKEN:
	case E_PARSER_UNK_TOKEN:
	case E_PARSER_UNK_FIELDNAME:
	case E_PARSER_INV_FIELDNAME:
	case E_PARSER_INV_VALUE:
	case E_PARSER_INV_STMT:
	case E_PARSER_AMB_FIELDNAME:
		vos_error2(s, _vos.e_nparm0, _vos.e_sparm0);
		break;
	case E_FILE_END:
		break;
	default:
		fprintf(stderr, "vos error number : %d\n", s);
	}

	if (_vos.e_sparm0)
		free(_vos.e_sparm0);
	if (_vos.e_sparm1)
		free(_vos.e_sparm1);
out:
	if (_vos.proc_tmp_dir)
		ll_destroy(&_vos.proc_tmp_dir);

	pthread_mutex_destroy(&_vos.proc_tmp_dir_lock);
	stmt_destroy(&stmt);

	return s;
}
Example #26
0
void ll_delete(smb_ll *list)
{
  ll_destroy(list);
  smb_free(list);
}
Example #27
0
void ls_iteration(double *F, double *phi, double* label, long* dims, 
                  LL* Lz, LL* Ln1, LL* Lp1, LL *Ln2, LL *Lp2, 
                  LL *Lin2out, LL* Lout2in){
  int x,y,z,i,idx;
  int u,d,r,l,f,b;
  double p, phi_old;
  LL *Sz, *Sn1, *Sp1, *Sn2, *Sp2;

  // create 'changing status' lists
  Sz  = ll_create();
  Sn1 = ll_create();
  Sp1 = ll_create();
  Sn2 = ll_create();
  Sp2 = ll_create();

  // #1) Normalize F
  double Fmax = .001;
  for(i=0;i<Lz->length;i++){
    if(fabs(F[i])>Fmax) Fmax = fabs(F[i]);
  }

  for(i=0;i<Lz->length;i++){ 
    F[i] = F[i]/Fmax*0.4;
  }

  // #2) add F to phi(Lz), create Sn1 & Sp1 
  //                                             ========
  //     (a) scan Lz values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5]
  ll_init(Lz); i = 0;
  while(Lz->curr != NULL){
    x= Lz->curr->x; y= Lz->curr->y; z= Lz->curr->z; idx= Lz->curr->idx;
    phi_old = phi[idx];

    phi[idx] = phi[idx]+F[i];

    //check to see if point crossed interface
    if(phi_old<=0 && phi[idx]>0 ){
      ll_pushnew(Lin2out,x,y,z,idx);
    }
    if(phi_old>0  && phi[idx]<=0){
      ll_pushnew(Lout2in,x,y,z,idx);
    }
    
    if(phi[idx] > .5){
      ll_push(Sp1, ll_remcurr(Lz)); 
    }
    else if(phi[idx] < -.5){
      ll_push(Sn1, ll_remcurr(Lz));
    }
    else{
      ll_step(Lz);
    }
    i++; //increment index into F
  }
  if(F!= NULL) mxFree(F); // free F (no longer needed);

  // #3) update Ln1,Ln2,Lp1,Lp2
  //                                    ==========
  //     (c) scan Ln1 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5]
  ll_init(Ln1);
  while(Ln1->curr != NULL){
    x= Ln1->curr->x; y= Ln1->curr->y; z= Ln1->curr->z; idx= Ln1->curr->idx;
    p = ls_max_hood_onlevel(idx, x, y, z, dims, phi,label,0);
    if(p>=-0.5){        // found something
      phi[idx] = p-1;

      if(phi[idx]>=-0.5){
        ll_push(Sz,ll_remcurr(Ln1));
      }
      else if(phi[idx]<-1.5){
        ll_push(Sn2,ll_remcurr(Ln1));
      }
      else ll_step(Ln1);
    }
    else{
      ll_push(Sn2,ll_remcurr(Ln1));
    }
  }

  //                                                      ========
  //     (c) scan Lp1 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5]
  ll_init(Lp1);
  while(Lp1->curr != NULL){
    x= Lp1->curr->x; y= Lp1->curr->y; z= Lp1->curr->z; idx= Lp1->curr->idx;
    p = ls_min_hood_onlevel(idx, x, y, z, dims, phi,label,0);
    if(p<=0.5){         // found something
      phi_old = phi[idx];
      phi[idx] = p+1;

      if(phi[idx]<=0.5){
        ll_push(Sz,ll_remcurr(Lp1));
      }
      else if(phi[idx]>1.5){
        ll_push(Sp2,ll_remcurr(Lp1));
      }
      else ll_step(Lp1);
    }
    else{
      ll_push(Sp2,ll_remcurr(Lp1));
    }
  }

  //                         ===========
  //     (c) scan Ln2 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5]
  ll_init(Ln2);
  while(Ln2->curr != NULL){
    x = Ln2->curr->x; y = Ln2->curr->y; z = Ln2->curr->z; idx= Ln2->curr->idx;
    p = ls_max_hood_onlevel(idx, x, y, z, dims, phi,label,-1);
    if(p>=-1.5){         // found something
      phi[idx] = p-1;
      if(phi[idx]>=-1.5){
        ll_push(Sn1,ll_remcurr(Ln2));
      }
      else if(phi[idx]<-2.5){
        ll_remcurr_free(Ln2);
        phi[idx] = -3; label[idx] = -3;
      }
      else ll_step(Ln2);
    }
    else{
      ll_remcurr_free(Ln2);
      phi[idx] = -3; label[idx] = -3;
    }
  }

  //                                                              =========
  //     (d) scan Lp2 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5]
  ll_init(Lp2);
  while(Lp2->curr != NULL){
    x = Lp2->curr->x; y = Lp2->curr->y; z = Lp2->curr->z; idx= Lp2->curr->idx;
    p = ls_min_hood_onlevel(idx, x, y, z, dims, phi,label,1);
    if(p<=1.5){         // found something
      phi[idx] = p+1;
      if(phi[idx]<=1.5){
        ll_push(Sp1,ll_remcurr(Lp2));
      }
      else if(phi[idx]>2.5){
        ll_remcurr_free(Lp2);
        phi[idx] = 3; label[idx] = 3;
      }
      else ll_step(Lp2);
    }
    else{
      ll_remcurr_free(Lp2);
      phi[idx] = 3; label[idx] = 3;
    }
  }

  // #4) Deal with S-lists Sz,Sn1,Sp1,Sn2,Sp2
  //     (a) Scan Sz
  ll_init(Sz);
  while(Sz->curr != NULL){
    idx= Sz->curr->idx;
    ll_push(Lz,ll_remcurr(Sz));
    label[idx] = 0;
  }

  //     (b) Scan Sn1
  ll_init(Sn1);
  while(Sn1->curr != NULL){
    x = Sn1->curr->x; y = Sn1->curr->y; z = Sn1->curr->z; idx = Sn1->curr->idx;
    ll_push(Ln1,ll_remcurr(Sn1));
    label[idx] = -1;
    if(((y+1)<DIMY) && phi[idx+OFFY]==-3){
      ll_pushnew(Sn2,x,y+1,z,idx+OFFY); 
      phi[idx+OFFY] = phi[idx] - 1; 
    }//up
    if(((y-1)>=0)   && phi[idx-OFFY]==-3){
      ll_pushnew(Sn2,x,y-1,z,idx-OFFY);
      phi[idx-OFFY] = phi[idx] - 1; 
    }//down
    if(((x+1)<DIMX) && phi[idx+OFFX]==-3){
      ll_pushnew(Sn2,x+1,y,z,idx+OFFX);
      phi[idx+OFFX] = phi[idx] - 1; 
    }//right
    if(((x-1)>=0)   && phi[idx-OFFX]==-3){
      ll_pushnew(Sn2,x-1,y,z,idx-OFFX);
      phi[idx-OFFX] = phi[idx] - 1; 
    }//left
    if(((z+1)<DIMZ) && phi[idx+OFFZ]==-3){
      ll_pushnew(Sn2,x,y,z+1,idx+OFFZ);
      phi[idx+OFFZ] = phi[idx] - 1; 
    }//front
    if(((z-1)>=0)   && phi[idx-OFFZ]==-3){
      ll_pushnew(Sn2,x,y,z-1,idx-OFFZ);
      phi[idx-OFFZ] = phi[idx] - 1; 
    }//back
  }

  //     (c) Scan Sp1
  ll_init(Sp1);
  while(Sp1->curr != NULL){
    x = Sp1->curr->x; y = Sp1->curr->y; z = Sp1->curr->z; idx=Sp1->curr->idx;
    ll_push(Lp1,ll_remcurr(Sp1));
    label[idx] = 1;
    if(((y+1)<DIMY) && phi[idx+OFFY]==3){
      ll_pushnew(Sp2,x,y+1,z,idx+OFFY); 
      phi[idx+OFFY] = phi[idx] + 1;
    }//up
    if(((y-1)>=0)   && phi[idx-OFFY]==3){
      ll_pushnew(Sp2,x,y-1,z,idx-OFFY); 
      phi[idx-OFFY] = phi[idx] + 1;
    }//down
    if(((x+1)<DIMX) && phi[idx+OFFX]==3){
      ll_pushnew(Sp2,x+1,y,z,idx+OFFX); 
      phi[idx+OFFX] = phi[idx] + 1;
    }//right
    if(((x-1)>=0)   && phi[idx-OFFX]==3){
      ll_pushnew(Sp2,x-1,y,z,idx-OFFX); 
      phi[idx-OFFX] = phi[idx] + 1;
    }//left
    if(((z+1)<DIMZ) && phi[idx+OFFZ]==3){
      ll_pushnew(Sp2,x,y,z+1,idx+OFFZ); 
      phi[idx+OFFZ] = phi[idx] + 1;
    }//front
    if(((z-1)>=0)   && phi[idx-OFFZ]==3){
      ll_pushnew(Sp2,x,y,z-1,idx-OFFZ); 
      phi[idx-OFFZ] = phi[idx] + 1;
    }//back
  }

  //     (d) Scan Sn2
  ll_init(Sn2);
  while(Sn2->curr != NULL){
    idx = Sn2->curr->idx;
    ll_push(Ln2,ll_remcurr(Sn2));
    label[idx] = -2;
  }

  //     (e) Scan Sp2
  ll_init(Sp2);
  while(Sp2->curr != NULL){
    idx = Sp2->curr->idx;
    ll_push(Lp2,ll_remcurr(Sp2));
    label[idx] = 2;
  }

  ll_destroy(Sz);
  ll_destroy(Sn1);
  ll_destroy(Sp1);
  ll_destroy(Sn2);
  ll_destroy(Sp2);
}
Example #28
0
int main(int argc, char *argv[])
{
  /* Define the options specific to the DNS protocol. */
  struct option long_options[] =
  {
    /* General options */
    {"help",    no_argument,       0, 0}, /* Help */
    {"h",       no_argument,       0, 0},
    {"version", no_argument,       0, 0}, /* Version */
#if 0
    {"name",    required_argument, 0, 0}, /* Name */
    {"n",       required_argument, 0, 0},
    {"download",required_argument, 0, 0}, /* Download */
    {"n",       required_argument, 0, 0},
    {"chunk",   required_argument, 0, 0}, /* Download chunk */
    {"isn",     required_argument, 0, 0}, /* Initial sequence number */
#endif

    {"delay",              required_argument, 0, 0}, /* Retransmit delay */
    {"steady",             no_argument,       0, 0}, /* Don't transmit immediately after getting a response. */
    {"max-retransmits",    required_argument, 0, 0}, /* Set the max retransmissions */
    {"retransmit-forever", no_argument,       0, 0}, /* Retransmit forever if needed */
#ifndef NO_ENCRYPTION
    {"secret",             required_argument, 0, 0}, /* Pre-shared secret */
    {"no-encryption",      no_argument,       0, 0}, /* Disable encryption */
#endif

    /* i/o options. */
    {"console", no_argument,       0, 0}, /* Enable console */
    {"exec",    required_argument, 0, 0}, /* Enable execute */
    {"e",       required_argument, 0, 0},
    {"command", no_argument,       0, 0}, /* Enable command (default) */
    {"ping",    no_argument,       0, 0}, /* Ping */

    /* Tunnel drivers */
    {"dns",     required_argument, 0, 0}, /* Enable DNS */
#if 0
    {"tcp",     optional_argument, 0, 0}, /* Enable TCP */
#endif

    /* Debug options */
    {"d",            no_argument, 0, 0}, /* More debug */
    {"q",            no_argument, 0, 0}, /* Less debug */
    {"packet-trace", no_argument, 0, 0}, /* Trace packets */

    /* Sentry */
    {0,              0,                 0, 0}  /* End */
  };

  int               c;
  int               option_index;
  const char       *option_name;

  NBBOOL            tunnel_driver_created = FALSE;
  ll_t             *drivers_to_create     = ll_create(NULL);
  uint32_t          drivers_created       = 0;

  log_level_t       min_log_level = LOG_LEVEL_WARNING;

  group = select_group_create();
  system_dns = dns_get_system();

  /* Seed with the current time; not great, but it'll suit our purposes. */
  srand((unsigned int)time(NULL));

  /* This is required for win32 support. */
  winsock_initialize();

#ifndef WIN32  
  /* set the SIGCHLD handler to SIG_IGN causing zombie child processes to be reaped automatically */
  if(signal(SIGCHLD, SIG_IGN) == SIG_ERR) 
  {
    perror("Couldn't set SIGCHLD handler to SIG_IGN");
    exit(1);
  }  
#endif

  /* Set the default log level */
  log_set_min_console_level(min_log_level);

  /* Parse the command line options. */
  opterr = 0;
  while((c = getopt_long_only(argc, argv, "", long_options, &option_index)) != -1)
  {
    switch(c)
    {
      case 0:
        option_name = long_options[option_index].name;

        /* General options */
        if(!strcmp(option_name, "help") || !strcmp(option_name, "h"))
        {
          usage(argv[0], "--help requested");
        }
        if(!strcmp(option_name, "version"))
        {
          printf(NAME" "VERSION" (client)\n");
          exit(0);
        }
        else if(!strcmp(option_name, "isn"))
        {
          uint16_t isn = (uint16_t) (atoi(optarg) & 0xFFFF);
          debug_set_isn(isn);
        }
        else if(!strcmp(option_name, "delay"))
        {
          int delay = (int) atoi(optarg);
          session_set_delay(delay);
          LOG_INFO("Setting delay between packets to %dms", delay);
        }
        else if(!strcmp(option_name, "steady"))
        {
          session_set_transmit_immediately(FALSE);
        }
        else if(!strcmp(option_name, "max-retransmits"))
        {
          controller_set_max_retransmits(atoi(optarg));
        }
        else if(!strcmp(option_name, "retransmit-forever"))
        {
          controller_set_max_retransmits(-1);
        }
#ifndef NO_ENCRYPTION
        else if(!strcmp(option_name, "secret"))
        {
          session_set_preshared_secret(optarg);
        }
        else if(!strcmp(option_name, "no-encryption"))
        {
          session_set_encryption(FALSE);
        }
#endif

        /* i/o drivers */
        else if(!strcmp(option_name, "console"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_console());

/*          session = session_create_console(group, "console");
          controller_add_session(session); */
        }
        else if(!strcmp(option_name, "exec") || !strcmp(option_name, "e"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_exec(optarg));

/*          session = session_create_exec(group, optarg, optarg);
          controller_add_session(session); */
        }
        else if(!strcmp(option_name, "command"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_command());

/*          session = session_create_command(group, "command");
          controller_add_session(session); */
        }
        else if(!strcmp(option_name, "ping"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_ping());

/*          session = session_create_ping(group, "ping");
          controller_add_session(session); */
        }

        /* Tunnel driver options */
        else if(!strcmp(option_name, "dns"))
        {
          tunnel_driver_created = TRUE;
          tunnel_driver = create_dns_driver(group, optarg);
        }
        else if(!strcmp(option_name, "tcp"))
        {
          tunnel_driver_created = TRUE;
          create_tcp_driver(optarg);
        }

        /* Debug options */
        else if(!strcmp(option_name, "d"))
        {
          if(min_log_level > 0)
          {
            min_log_level--;
            log_set_min_console_level(min_log_level);
          }
        }
        else if(!strcmp(option_name, "q"))
        {
          min_log_level++;
          log_set_min_console_level(min_log_level);
        }
        else if(!strcmp(option_name, "packet-trace"))
        {
          session_enable_packet_trace();
        }
        else
        {
          usage(argv[0], "Unknown option");
        }
        break;

      case '?':
      default:
        usage(argv[0], "Unrecognized argument");
        break;
    }
  }

  create_drivers(drivers_to_create);
  ll_destroy(drivers_to_create);

  if(tunnel_driver_created && argv[optind])
  {
    printf("It looks like you used --dns and also passed a domain on the commandline.\n");
    printf("That's not allowed! Either use '--dns domain=xxx' or don't use a --dns\n");
    printf("argument!\n");
    exit(1);
  }

  /* If no output was set, use the domain, and use the last option as the
   * domain. */
  if(!tunnel_driver_created)
  {
    /* Make sure they gave a domain. */
    if(optind >= argc)
    {
      printf("Starting DNS driver without a domain! This will only work if you\n");
      printf("are directly connecting to the dnscat2 server.\n");
      printf("\n");
      printf("You'll need to use --dns server=<server> if you aren't.\n");
      tunnel_driver = create_dns_driver_internal(group, NULL, "0.0.0.0", 53, DEFAULT_TYPES, NULL);
    }
    else
    {
      tunnel_driver = create_dns_driver_internal(group, argv[optind], "0.0.0.0", 53, DEFAULT_TYPES, NULL);
    }
  }

  /* Be sure we clean up at exit. */
  atexit(cleanup);

  /* Start the driver! */
  driver_dns_go(tunnel_driver);

  return 0;
}
Example #29
0
void dealloc(obj* self, va_list* args) {
    list* value = *((list**)get_p(self, "value"));
    ll_destroy(value, list_op_release);
}
Example #30
0
int main(int argc, char *argv[]) {
   LinkedList *ll = NULL;
   TreeSet *ts = NULL;
   char *sp;
   char pattern[4096];
   RegExp *reg;
   Iterator *it;

   if (argc < 2) {
      fprintf(stderr, "Usage: ./fileCrawler pattern [dir] ...\n");
      return -1;
   }
   /*
    * convert bash expression to regular expression and compile
    */
   cvtPattern(pattern, argv[1]);
   if ((reg = re_create()) == NULL) {
      fprintf(stderr, "Error creating Regular Expression Instance\n");
      return -1;
   }
   if (! re_compile(reg, pattern)) {
      char eb[4096];
      re_status(reg, eb, sizeof eb);
      fprintf(stderr, "Compile error - pattern: `%s', error message: `%s'\n",
              pattern, eb);
      re_destroy(reg);
      return -1;
   }
   /*
    * create linked list and treeset
    */
   if ((ll = ll_create()) == NULL) {
      fprintf(stderr, "Unable to create linked list\n");
      goto done;
   }
   if ((ts = ts_create(scmp)) == NULL) {
      fprintf(stderr, "Unable to create tree set\n");
      goto done;
   }
   /*
    * populate linked list
    */
   if (argc == 2) {
      if (! processDirectory(".", ll, 1))
         goto done;
   } else {
      int i;
      for (i = 2; i < argc; i++) {
         if (! processDirectory(argv[i], ll, 1))
            goto done;
      }
   }
   /*
    * for each directory in the linked list, apply regular expression
    */
   while (ll_removeFirst(ll, (void **)&sp)) {
      int stat = applyRe(sp, reg, ts);
      free(sp);
      if (! stat)
         break;
   }
   /*
    * create iterator to traverse files matching pattern in sorted order
    */
   if ((it = ts_it_create(ts)) == NULL) {
      fprintf(stderr, "Unable to create iterator over tree set\n");
      goto done;
   }
   while (it_hasNext(it)) {
      char *s;
      (void) it_next(it, (void **)&s);
      printf("%s\n", s);
   }
   it_destroy(it);
/*
 * cleanup after ourselves so there are no memory leaks
 */
done:
   if (ll != NULL)
      ll_destroy(ll, free);
   if (ts != NULL)
      ts_destroy(ts, free);
   re_destroy(reg);
   return 0;
}