Beispiel #1
0
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;
}
Beispiel #2
0
int
main (int argc, char **argv)
{
    char *peerfile;
    register SVCXPRT *transp;
	FILE *fd;
	int i, ret;
	char tmp[MAXHOSTNAME+2];
    pthread_t ireaper;
    pthread_t qreaper;
    pthread_t validate_thr;
    pthread_t trigger_thr;
    int opt;
	

    if (argc < 3 || argc > 8) {
        usage(argv[0]);
        return (1);
    }

    while ((opt = getopt(argc, argv, "ul:t:")) != -1) {
        switch (opt) {
            case 'u':
                push = 1;
                break;
            case 'l':
                pull = 1;
                ttrtime = atoi(optarg);
                break;
            case 't':
                trigger = 1;
                delay = atoi(optarg);
                break;
            default:
                usage(argv[0]);
                return (1);
        }
    }

	if ( gethostname(localhostname, sizeof(localhost)) != 0) {
        printf("Unable to get the local hostname ! errno = %d\n", errno);
        return (1);
    }

	peerfile = argv[optind];
	sharedir = argv[optind + 1];
    printf("peerfile %s sharedir %s\n", peerfile, sharedir);

    if (!peerfile || strlen(peerfile) == 0 || !sharedir || strlen(sharedir) == 0) {
        usage(argv[0]);
        return (1);
    }

#ifdef DEBUG
    printf("localhostname : %s ttrtime = %d delay = %d\n", localhostname, ttrtime, delay);
#endif

	if (parse_peers(peerfile) != 0) {
		printf("Failed to parse the peers file. Quitting !\n");
		return (1);
	}

    /*
     * Initialize the pending queue mutex.
     */
    pthread_mutex_init(&(qpending.lock), NULL);
    pthread_mutex_init(&(ipending.lock), NULL);

    /*
     * Register the files in the given directory with the index-server.
     */
    if (register_files(localhostname, sharedir) != 0) {
        printf("Failed to register with the index server on %s\n", argv[2]);
        printf("Quitting :(\n");
        return (1);
    }

/*-------------- start change ----------------*/
    /*
     * Create the reaper_threads for ipending and qpending lists.
     */
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_create(&qreaper, &attr, reaper_thread, (void *)&qpending);
    pthread_create(&ireaper, &attr, reaper_thread, (void *)&ipending);

    if (pull) {
        pthread_create(&validate_thr, &attr, validate_thread, (void *)&ipending);
    }

    if (trigger) {
        pthread_create(&trigger_thr, &attr, trigger_thread, (void *)&ipending);
    }
/*-------------- end change ----------------*/

    pmap_unset (OBTAINPROG, OBTAINVER);

    transp = svcudp_create(RPC_ANYSOCK);
    if (transp == NULL) {
        fprintf (stderr, "%s", "cannot create udp service.");
        exit(1);
    }
    if (!svc_register(transp, OBTAINPROG, OBTAINVER, obtainprog_1, IPPROTO_UDP)) {
        fprintf (stderr, "%s", "unable to register (OBTAINPROG, OBTAINVER, udp).");
        exit(1);
    }

    transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    if (transp == NULL) {
        fprintf (stderr, "%s", "cannot create tcp service.");
        exit(1);
    }
    if (!svc_register(transp, OBTAINPROG, OBTAINVER, obtainprog_1, IPPROTO_TCP)) {
        fprintf (stderr, "%s", "unable to register (OBTAINPROG, OBTAINVER, tcp).");
        exit(1);
    }
    printf("Now ready to serve them\n\n");

    svc_run ();
    fprintf (stderr, "%s", "svc_run returned");
    exit (1);
    /* NOTREACHED */
}
Beispiel #3
0
int
main (int argc, char **argv)
{
    char *peerfile;
    register SVCXPRT *transp;
	FILE *fd;
	int i, ret;
	char tmp[MAXHOSTNAME+2];
    pthread_t reaper;
	

    if (argc != 3) {
        usage(argv[0]);
        return (1);
    }

	if ( gethostname(localhostname, sizeof(localhost)) != 0) {
        printf("Unable to get the local hostname ! errno = %d\n", errno);
        return (1);
    }

	peerfile = argv[1];
	sharedir = argv[2];

    if (strlen(peerfile) == 0 || strlen(sharedir) == 0) {
        usage(argv[0]);
        return (1);
    }

#ifdef DEBUG
    printf("localhostname : %s\n", localhostname);
#endif

	if (parse_peers(peerfile) != 0) {
		printf("Failed to parse the peers file. Quitting !\n");
		return (1);
	}

    /*
     * Initialize the pending queue mutex.
     */
    pthread_mutex_init(&(pending.lock), NULL);

    /*
     * Create the reaper_thread.
     */
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_create(&reaper, &attr, reaper_thread, NULL);

    /*
     * Register the files in the given directory with the index-server.
     */
    if (register_files(localhostname, sharedir) != 0) {
        printf("Failed to register with the index server on %s\n", argv[2]);
        printf("Quitting :(\n");
        return (1);
    }


    pmap_unset (OBTAINPROG, OBTAINVER);

    transp = svcudp_create(RPC_ANYSOCK);
    if (transp == NULL) {
        fprintf (stderr, "%s", "cannot create udp service.");
        exit(1);
    }
    if (!svc_register(transp, OBTAINPROG, OBTAINVER, obtainprog_1, IPPROTO_UDP)) {
        fprintf (stderr, "%s", "unable to register (OBTAINPROG, OBTAINVER, udp).");
        exit(1);
    }

    transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    if (transp == NULL) {
        fprintf (stderr, "%s", "cannot create tcp service.");
        exit(1);
    }
    if (!svc_register(transp, OBTAINPROG, OBTAINVER, obtainprog_1, IPPROTO_TCP)) {
        fprintf (stderr, "%s", "unable to register (OBTAINPROG, OBTAINVER, tcp).");
        exit(1);
    }
    printf("Now ready to serve them\n\n");

    svc_run ();
    fprintf (stderr, "%s", "svc_run returned");
    exit (1);
    /* NOTREACHED */
}