Exemplo n.º 1
0
/* 
 * process_stun_msg(): 
 * 			buf - incoming message
 * 			len - length of incoming message
 * 			ri  - information about socket that received a message and 
 *                also information about sender (its IP, port, protocol)
 * 
 * This function ensures processing of incoming message. It's common for both
 * TCP and UDP protocol. There is no other function as an interface.
 * 
 * Return value:	0	if there is no environment error
 * 					-1	if there is some enviroment error such as insufficiency
 * 						of memory
 * 
 */
int process_stun_msg(char* buf, unsigned len, struct receive_info* ri)
{
	struct stun_msg 			msg_req;
	struct stun_msg 			msg_res;
	struct dest_info			dst;
	struct stun_unknown_att*	unknown;
	USHORT_T					error_code;
	 
	memset(&msg_req, 0, sizeof(msg_req));
	memset(&msg_res, 0, sizeof(msg_res));
	
	msg_req.msg.buf.s = buf;
	msg_req.msg.buf.len = len;	
	unknown = NULL;
	error_code = RESPONSE_OK;
	
	if (stun_parse_header(&msg_req, &error_code) != 0) {
		goto error;
	}
	
	if (error_code == RESPONSE_OK) {
		if (stun_parse_body(&msg_req, &unknown, &error_code) != 0) {
			goto error;
		}
	}
	
	if (stun_create_response(&msg_req, &msg_res, ri,  
							unknown, error_code) != 0) {
		goto error;
	}
	
	init_dst_from_rcv(&dst, ri);

#ifdef EXTRA_DEBUG	
	struct ip_addr ip;
	su2ip_addr(&ip, &dst.to);
	LOG(L_DBG, "DEBUG: process_stun_msg: decoded request from (%s:%d)\n", ip_addr2a(&ip), 
		su_getport(&dst.to));
#endif
	
	/* send STUN response */
	if (msg_send(&dst, msg_res.msg.buf.s, msg_res.msg.buf.len) != 0) {
		goto error;
	}
	
#ifdef EXTRA_DEBUG
	LOG(L_DBG, "DEBUG: process_stun_msg: send response\n");
#endif
	clean_memory(&msg_req, &msg_res, unknown);
	return 0;
	
error:
#ifdef EXTRA_DEBUG
	LOG(L_DBG, "DEBUG: process_stun_msg: failed to decode request\n");
#endif
	clean_memory(&msg_req, &msg_res, unknown);
	return FATAL_ERROR;
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{
    if (argc < 2){
        printf("\nUsage: %s <brainfuck-code.txt>\n", argv[0]);
        free_memory();
        exit(EXIT_FAILURE);
    }
    clean_memory();
    reset_pointer();
    
    load_program(argv[1]);
    if (!is_balanced(program, program_size)){
        printf("ERROR: Unbalanced brackets found in the supplied Brainfuck code.\n");
        free_memory();
        exit(EXIT_FAILURE);
    }
    execute_program();
    free_memory();
    return 0;
}
Exemplo n.º 3
0
int		parsexec(char *cmd, t_shell *shell)
{
	int		ret;
	t_cmd	*cmd_data;
	t_tree	*cmd_tree;

	g_is_a_term = 0;
	if ((cmd = add_some_space(cmd)) == NULL
		|| (cmd_data = get_cmd_data(cmd)) == NULL)
		return (-1);
	lexing(cmd_data);
	if ((ret = replace_var(cmd_data, shell)) != -1
		&& ret != -2 && parsing(cmd_data))
	{
		if ((cmd_tree = get_tree(cmd_data)) == NULL
			|| exec_tree(cmd_tree, shell) == -1)
			return (-1);
		clean_memory(cmd_tree, cmd_data, cmd);
	}
	return (ret);
}
Exemplo n.º 4
0
int main(int argc, char *argv[]) {
	FILE *fd = NULL;
	PSTACK_NODE p = NULL;
	int i;
	char *fn = (char *)malloc(sizeof(char) * BUFFSIZE);
	char *buff = (char *)malloc(sizeof(char) * BUFFSIZE);

	printf("Input filename: ");
	gets_s(fn, BUFFSIZE);

	if (!*fn) {
		printf("File name is empty\n");
		_getch();
		return -1;
	}

	if ((i = fopen_s(&fd, fn, "rt")) != 0) {
		printf("Error open file: \"%s\"\n", fn);
		_getch();
		return i;
	}

	while (fgets(buff, BUFFSIZE, fd) != NULL) 
		push(&p, buff);

	printf("=> Strings of file in reverse mode\n\n");
	for(i = 0; p; ++i) 
		printf("[%d] %s", i, pop(&p));

	fclose(fd);

	free(buff);
	free(fn);

	clean_memory(&p);

	_getch();
	return 0;
}
Exemplo n.º 5
0
int main(int argc, const char *argv[])
{
	struct ZAGLAVLJE_t *it;
	int cnt, velicina, adresa, suma;
	char cmd[5];
	
	init();
	
	sigset( SIGINT, clean_memory );

	while (1) {

		printf("RS: broj blokova %d, slobodni %d, zauzeti %d\n", blokova, slobodni, blokova - slobodni);
	
		
		for (cnt = suma = 0, it = memorija; it; it=it->next, ++cnt) {
				printf("%d. pocetak %d, velicina %d, oznaka %c\n", cnt + 1, suma, it->size, it->flag);
				suma += it->size;
		}
		
		printf("Unesi zahtjev (d - dodijeli, o - oslobodi, x - izadji) ");
		scanf( "%s", cmd );

		if ( *cmd == 'd') {
			printf("Unesi velicinu programa (u oktetima) ");
			scanf( "%d", &velicina );
			it = dodijeli( ( unsigned )velicina );
			
			if ( it == NULL ) {
				printf("Greska kod dodijeljivanja memorije\n");
				continue;
			}

			adresa = 0;
			while (it) {
				adresa += it->size;
				it = it->next;
			}

			printf("Dodijeljen blok na adresi %d\n", suma - adresa);

		} else if( *cmd == 'o' ) {
			printf("Unesi pocetnu adresu programa ");
			scanf( "%d", &adresa );
			
			velicina = 0;
			it = memorija;
			while (it) {
				if ( velicina == adresa ) {
					oslobodi( it );
					break;
				}
				velicina += it->size;
				it = it->next;
			}

		}	else {
			break;
		}

	}
	
	clean_memory( 0 );
	return 0;
}