void check_parse(char* fileName)
{
   //http://sourceforge.net/p/funzix/code/ci/master/tree/bencode/test.c#l43
    

char *buf;
long long len;
be_node *n;
buf = read_file(fileName, &len);
if (!buf) {
buf = fileName;
len = strlen(fileName);
}
printf("DECODING: %s\n", fileName);
n = be_decoden(buf, len);
if (n) {
be_dump(n);
be_free(n);
} else
printf("\tparsing failed!\n");
#ifdef FILE_IO
if (buf != fileName)
free(buf);
#endif 
return;
    
}
Exemple #2
0
/**
 * Transforms the standard Firm graph into an ARM firm graph.
 */
static void arm_select_instructions(ir_graph *irg)
{
	/* transform nodes into assembler instructions */
	be_timer_push(T_CODEGEN);
	arm_transform_graph(irg);
	be_timer_pop(T_CODEGEN);
	be_dump(DUMP_BE, irg, "code-selection");

	/* do local optimizations (mainly CSE) */
	local_optimize_graph(irg);

	/* do code placement, to optimize the position of constants */
	place_code(irg);
}
Exemple #3
0
void amd64_adjust_pic(ir_graph *irg)
{
	switch (be_options.pic_style) {
	case BE_PIC_NONE:
		return;
	case BE_PIC_ELF_PLT:
		irg_walk_graph(irg, fix_address_pic_elf, NULL, NULL);
		break;
	case BE_PIC_ELF_NO_PLT:
		panic("amd64 elf/no-plt not implemented yet");
	case BE_PIC_MACH_O:
		irg_walk_graph(irg, fix_address_pic_mach_o, NULL, NULL);
		break;
	}
	be_dump(DUMP_BE, irg, "pic");
}
int main (int argc, char * argv[]){

  bt_args_t bt_args;
  be_node * node; // top node in the bencoding
  int i;
  bt_info_t* my_bt_info = malloc(sizeof(my_bt_info));  
  parse_args(&bt_args, argc, argv);

    

// To compare with server response we need this bt_args.peers[1]->id

//struct bt_handshake handshake;
//char *str_handshake;


  if(bt_args.verbose){
    printf("Args:\n");
    printf("verbose: %d\n",bt_args.verbose);
    printf("save_file: %s\n",bt_args.save_file);
    printf("log_file: %s\n",bt_args.log_file);
    printf("torrent_file: %s\n", bt_args.torrent_file);

    for(i=0;i<MAX_CONNECTIONS;i++){
      if(bt_args.peers[i] != NULL)
        print_peer(bt_args.peers[i]);
    }
    
  }

  //read and parse the torent file
  node = load_be_node(bt_args.torrent_file);
  
  //check_parse(bt_args.torrent_file);
  
  /*
  parseNode(node,0,my_bt_info);
  
  
  ///// Print the datat from the sturcure
  printf("my_bt_info->announce %s\n",my_bt_info->announce);
  printf("my_bt_info->name  %s\n",my_bt_info->name);
  printf("my_bt_info->piece_length  %i\n",my_bt_info->piece_length);
  printf("my_bt_info->length  %i\n",my_bt_info->length);
  printf("my_bt_info->num_pieces  %i\n",my_bt_info->num_pieces);
*/
  
  
  bt_args.bt_info=malloc(sizeof(bt_info_t));
  parseNode(node,0,bt_args.bt_info);
  
  printf("my_bt_info->announce %s\n",bt_args.bt_info->announce);
  printf("my_bt_info->name  %s\n",bt_args.bt_info->name);
  printf("my_bt_info->piece_length  %i\n",bt_args.bt_info->piece_length);
  printf("my_bt_info->length  %i\n",bt_args.bt_info->length);
  printf("my_bt_info->num_pieces  %i\n",bt_args.bt_info->num_pieces);


 // printf("my_bt_info->my_peiecehases  %s\n",my_bt_info->my_peiecehases);

  //printf(" \n size : %d",sizeof(my_bt_info->my_peiecehases));
  //be_dump(node);
  

  if(bt_args.verbose){
    be_dump(node);
  }



printf("bt_args.client value is %i \n :", bt_args.client);
printf("\n\n\n seeder not started");

 if(bt_args.client)
{
printf("Now as clien \n");

char * filenamew="TempTemp1.txt";
char* bitfilename= malloc(1000);
//bitfilename=NULL;
if (bt_args.save_file != NULL)
  {
      filenamew= bt_args.save_file;
      strcpy(bitfilename,filenamew);
    }
strcat(bitfilename,"Restart.tmp");

printf("fielname for restart %s",bitfilename);
intial_file_to_write(filenamew, bt_args.bt_info->length);
client(&bt_args,my_bt_info,bitfilename);
}
if(bt_args.client==0)
{

printf("\n\n\n seeder started");
printf("\n\n\n seeder started");
seeder(&bt_args,&bt_args.bt_info);

}
/*
  //main client loop
  printf("Starting Main Loop\n");
  while(1){

    //try to accept incoming connection from new peer
       
    
    //poll current peers for incoming traffic
    //   write pieces to files
    //   udpdate peers choke or unchoke status
    //   responses to have/havenots/interested etc.
    
    //for peers that are not choked
    //   request pieaces from outcoming traffic

    //check livelenss of peers and replace dead (or useless) peers
    //with new potentially useful peers
    
    //update peers, 

  }*/

  return 0;
}
Exemple #5
0
int contact_tracker(bt_args_t *bt_args)
{
    printf("Please wait ...\nConnecting with tracker.\n");

    char *new_file;
    long long leng;

    new_file = read_file(bt_args->torrent_file, &leng);

    if (!new_file)
        return 1;

    char *inf = strstr(strstr(new_file, "info"), "d");
    // length on ubuntu 14.04 torrent should be 44478
    long long len = be_len(inf);

    memset(bt_args->info_hash, '\0', BT_INFO_HASH_SIZE);
    memset(bt_args->bt_peer_id, '\0', BT_INFO_HASH_SIZE);
    SHA1((unsigned char const *) inf, (size_t) len, (unsigned char *) bt_args->info_hash);

    char *request_to_send = malloc(FILE_NAME_MAX);
    request_to_send = malloc(FILE_NAME_MAX);

    memset(request_to_send, '\0', FILE_NAME_MAX);
    memcpy(bt_args->bt_peer_id, generate_peer_id(), 20);

    //Port number this peer is listening on.
    //Common behavior is for a downloader to try to listen on
    //port 6881 and if that port is taken try 6882, then 6883, etc. and give up after 6889.
    uint16_t port = INIT_PORT;
    bt_args->bt_info->num_pieces = bt_args->bt_info->length / bt_args->bt_info->piece_length;
    sprintf(request_to_send,
            "%s?info_hash=%s&peer_id=%s&port=%hu&uploaded=0"
            "&downloaded=0&left=%d&event=started&compact=1",
            bt_args->bt_info->announce, url_encode(bt_args->info_hash),
            url_encode(bt_args->bt_peer_id), port, bt_args->bt_info->length);

    // correct request to send on ubuntu torrent

    //  http://torrent.ubuntu.com:6969/announce?
    //      info_hash=%B4%15%C9%13d%3E%5F%F4%9F%E3%7D0K%BB%5En%11%ADQ%01
    //      announce?info_hash=%b4%15%c9%13d%3e_%f4%9f%e3%7d0K%bb%5en%11%adQ%01
    //      &peer_id=TueFeb32137332015RRR&port=6681&event=started&uploaded=0
    //      &downloaded=0&left=1162936320&compact=1

    if (bt_args->verbose)
        printf("Request URL for tracker: %s\n", request_to_send);

    char *result = _send_http_request(request_to_send);
    if (result)
    {
        be_node *node = be_decoden(result, (long long int) be_len);

        if (bt_args->verbose)
            be_dump(node);

        bt_peer *peer = malloc(sizeof(bt_peer));

        // parse_info(peer, node);
        _fill_peer_info(peer, node, 0, "");

        int num_peers = 0;

        char *peer_num = strstr(result, "peers");
        if (peer_num == NULL)
        {
            printf("Something went wrong in parsing received data!\n");
            free(result);
            return 1;
        }
        int i = 0;
        peer_num += 5;
        char buff[20];
        memset(buff, 0, 20);
        for (; *peer_num != ':'; peer_num++, i++)
            buff[i] = *peer_num;

        char *endptr;
        num_peers = (int) strtol(buff, &endptr, 10) / 6;

        if (num_peers == 0)
        {
            free(result);
            return 1;
        }
        int count = 0;
        pthread_t *thread = malloc(num_peers * sizeof(pthread_t));
        printf("Connecting with peers.\n");
        for (i = 0; i < num_peers; i++)
        {
            uint32_t ip = *(uint32_t *) (peer->peer_hashes + count);
            count = (int) (count + sizeof(uint32_t));
            port = *(uint16_t *) (peer->peer_hashes + count);
            count = (int) (count + sizeof(uint16_t));

            peer_t *my_peer_t = malloc(sizeof(peer_t));

            my_peer_t->interested = -1;
            my_peer_t->choked = -1;

            //IP to string
            struct in_addr ip_addr;
            ip_addr.s_addr = ip;

            char *id = malloc(21);
            memset(id, 0, 21);
            calc_id(inet_ntoa(ip_addr), port, id);
            memset(my_peer_t->id, 0, ID_SIZE);
            strcpy((char *) my_peer_t->id, id);

            init_peer(my_peer_t, id, inet_ntoa(ip_addr), htons(port));
            add_peer(my_peer_t, bt_args, inet_ntoa(ip_addr), port);

            thdata *data = malloc(sizeof(thdata));

            data->th_num = i;
            data->bt_args = bt_args;
            data->bt_peer_t = my_peer_t;

            pthread_create (&thread[i], NULL, (void *) &_connect_function, (void *) data);
        }

        for (i = 0; i < num_peers; i++);
        pthread_join(thread[i], NULL);
    }
    else
    {
        printf("Something went wrong!\n");
        return 1;
    }

    return 0;
}