Skip to content

Commit 8f2c16a

Browse files
committed
Add -a, -t and -u options.
1 parent b7504d3 commit 8f2c16a

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

mag2tor.cpp

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <stddef.h> // NULL
2+
#include <unistd.h> // optind
3+
#include <getopt.h> // getopt_long()
14
#include <chrono>
25
#include <exception>
36
#include <stdexcept> // __gnu_cxx::__verbose_terminate_handler
@@ -16,8 +19,15 @@
1619
#include <libtorrent/torrent_info.hpp>
1720

1821
static int usage(const char *argv0) {
19-
std::cerr << "usage: " << argv0 << " <magnet-url>" <<
22+
std::cerr << "usage: [options] " << argv0 << " <magnet-url>" <<
2023
std::endl;
24+
std::cerr << "where options are:" << std::endl;
25+
std::cerr << " -a, --anonymous anonymous mode" << std::endl;
26+
std::cerr << " -t, --tcp TCP mode" << std::endl;
27+
std::cerr << " -u, --udp UDP mode (specifying both"
28+
<< " -t and -u enables both protocols" << std::endl;
29+
std::cerr << " with UDP trackers preferred)"
30+
<< std::endl;
2131
return 1;
2232
}
2333

@@ -26,14 +36,61 @@ int main(int argc, char *argv[]) {
2636
std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
2737

2838
lt::session_settings sset;
29-
sset.prefer_udp_trackers = false;
39+
lt::add_torrent_params atp;
40+
int tcp_udp = 0;
41+
char *magnet_url = NULL;
3042

31-
if (argc != 2) return usage(argv[0]);
43+
for (;;) {
44+
static struct option lopts[] = {
45+
{"anonymous", no_argument, NULL, 'a'},
46+
{"tcp", no_argument, NULL, 't'},
47+
{"udp", no_argument, NULL, 'u'},
48+
};
49+
int loptind = -1;
50+
int c = getopt_long(argc, argv, "atu", lopts, &loptind);
51+
if (c == -1) break;
52+
switch (c) {
53+
case 'a': sset.anonymous_mode = true; break;
54+
case 't': tcp_udp |= 1; break;
55+
case 'u': tcp_udp |= 2; break;
56+
default: return usage(argv[0]);;
57+
}
58+
}
59+
if (argc - optind != 1) return usage(argv[0]);
60+
atp.url = argv[optind];
61+
switch (tcp_udp) {
62+
case 0:
63+
sset.prefer_udp_trackers = false;
64+
sset.enable_outgoing_tcp = true;
65+
sset.enable_incoming_tcp = true;
66+
sset.enable_outgoing_utp = true;
67+
sset.enable_incoming_utp = true;
68+
break;
69+
case 1:
70+
sset.prefer_udp_trackers = false;
71+
sset.enable_outgoing_tcp = true;
72+
sset.enable_incoming_tcp = true;
73+
sset.enable_outgoing_utp = false;
74+
sset.enable_incoming_utp = false;
75+
break;
76+
case 2:
77+
sset.prefer_udp_trackers = true;
78+
sset.enable_outgoing_tcp = false;
79+
sset.enable_incoming_tcp = false;
80+
sset.enable_outgoing_utp = true;
81+
sset.enable_incoming_utp = true;
82+
break;
83+
case 3:
84+
sset.prefer_udp_trackers = true;
85+
sset.enable_outgoing_tcp = true;
86+
sset.enable_incoming_tcp = true;
87+
sset.enable_outgoing_utp = true;
88+
sset.enable_incoming_utp = true;
89+
break;
90+
}
3291

3392
lt::session sess;
3493
sess.set_settings(sset);
35-
lt::add_torrent_params atp;
36-
atp.url = argv[1];
3794
atp.upload_mode = true;
3895
atp.auto_managed = false;
3996
atp.paused = false;

0 commit comments

Comments
 (0)