int main(int argc, char **argv) { // Test argument count if (argc != 2) { printf("usage: bitmd <file.torrent>"); exit(0); } printf("++++ starting bitmd ++++\n"); printf("++++ parsing ++++ \n%s \n", argv[1]); struct Tor *tor = parse_torrent(argv[1]); print_tor(tor); printf("++++ announce ++++ \n"); struct Buffer *tracker_response = (struct Buffer *)malloc_eoe(sizeof(struct Buffer)); int res; while (res != 1) { res = announce(tor, tracker_response); } printf("++++ tracker responds ++++ \n"); print_nchars(tracker_response->cnt, tracker_response->size); printf("\n"); printf("++++ peers found ++++ \n"); struct Bcont *parsed = b_decode(tracker_response); struct Peer *peers = parse_peers(parsed->cnt); print_peers(peers); printf("+++ connecting to peer ++++ \n"); connect_to_peer(peers); return 0; }
int addpeer(hash_info_t *hashinfo, peer_t *peer) { if (hashinfo == NULL) { fprintf(stderr, "addpeer:hashinfo NULL\n"); return(-1); } #ifdef DEBUG { struct in_addr in; in.s_addr = peer->ip; fprintf(stderr, "addpeer:adding peer (%s) for hash (0x%016llx)\n", inet_ntoa(in), hashinfo->hash); fprintf(stderr, "addpeer:before\n"); print_peers(hashinfo); } #endif if (hashinfo->peers) { if ((hashinfo->peers = realloc(hashinfo->peers, (hashinfo->numpeers + 1) * sizeof(*peer))) == NULL) { fprintf(stderr, "addpeer:realloc failed\n"); return(-1); } } else { if ((hashinfo->peers = malloc(sizeof(*peer))) == NULL) { fprintf(stderr, "addpeer:malloc failed\n"); return(-1); } } memcpy(&hashinfo->peers[hashinfo->numpeers], peer, sizeof(*peer)); ++hashinfo->numpeers; #ifdef DEBUG fprintf(stderr, "addpeer:after\n"); print_peers(hashinfo); fprintf(stderr, "\n"); #endif return(0); }
void print_hash_table() { int i; fprintf(stderr, "head: (%d)\n", hash_table->head); fprintf(stderr, "tail: (%d)\n", hash_table->tail); if (hash_table->head >= hash_table->tail) { for (i = hash_table->head - 1; (i > hash_table->tail) && (i >= 0) ; --i) { fprintf(stderr, "entry[%d] : hash (0x%lx)\n", i, hash_table->entry[i].hash); print_peers(&hash_table->entry[i]); } } else { for (i = hash_table->head - 1; i >= 0 ; --i) { fprintf(stderr, "entry[%d] : hash (0x%lx)\n", i, hash_table->entry[i].hash); print_peers(&hash_table->entry[i]); } for (i = (hash_table->size - 1) ; (i > hash_table->tail) && (i >= 0) ; --i) { fprintf(stderr, "entry[%d] : hash (0x%lx)\n", i, hash_table->entry[i].hash); print_peers(&hash_table->entry[i]); } } return; }