-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbt_setup.cpp
More file actions
208 lines (179 loc) · 5.87 KB
/
bt_setup.cpp
File metadata and controls
208 lines (179 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "bt_setup.h"
#include "bt_lib.h"
#include "bencode.h"
/**
* usage(FILE * file) -> void
*
* print the usage of this program to the file stream file
*
**/
void usage(FILE * file) {
if (file == NULL) {
file = stdout;
}
fprintf(file,
"bt-client [OPTIONS] file.torrent\n"
" -h \t Print this help screen\n"
" -b ip \t Bind to this ip for incoming connections, ports\n"
" \t are selected automatically\n"
" -s save_file \t Save the torrent in directory save_dir (dflt: .)\n"
" -l log_file \t Save logs to log_filw (dflt: bt-client.log)\n"
" -p ip:port \t Instead of contacing the tracker for a peer list,\n"
" \t use this peer instead, ip:port (ip or hostname)\n"
" \t (include multiple -p for more than 1 peer)\n"
" -I id \t Set the node identifier to id (dflt: random)\n"
" -v \t verbose, print additional verbose info\n");
}
/**
* __parse_peer(peer_t * peer, char peer_st) -> void
*
* parse a peer string, peer_st and store the parsed result in peer
*
* ERRORS: Will exit on various errors
**/
void __parse_peer(peer_t * peer, char * peer_st) {
char * word;
unsigned short port;
char * ip;
char id[20];
char sep[] = ":";
int i;
char * parse_str;
//need to copy because strtok mangels things
parse_str = (char *) malloc(strlen(peer_st) + 1);
strncpy(parse_str, peer_st, strlen(peer_st) + 1);
//only can have 2 tokens max, but may have less
for (word = strtok(parse_str, sep), i = 0;
(word && i < 3);
word = strtok(NULL, sep), i++) {
printf("%d:%s\n", i, word);
switch (i) {
case 0://id
ip = word;
break;
case 1://ip
port = atoi(word);
default:
break;
}
}
if (i < 2) {
fprintf(stderr, "ERROR: Parsing Peer: Not enough values in '%s'\n", peer_st);
usage(stderr);
exit(1);
}
if (word) {
fprintf(stderr, "ERROR: Parsing Peer: Too many values in '%s'\n", peer_st);
usage(stderr);
exit(1);
}
//calculate the id, value placed in id
struct hostent * hostinfo;
if ((hostinfo = gethostbyname(ip)) == NULL) {
perror("gethostbyname failure, no such host?");
herror("gethostbyname");
exit(1);
}
char * ip_net = (char *)malloc(hostinfo->h_length);
bcopy((char *) (hostinfo->h_addr),
(char *)ip_net,
hostinfo->h_length);
calc_id(ip_net, port, id);
//build the object we need
printf("init peer\n");
int c = init_peer(peer, id, ip_net, port);
printf("end init\n");
//free extra memory
free(parse_str);
printf("init peer %d\n", c);
printf("ip: %s\n", ip);
printf("port: %u\n", peer->port);
return;
}
/**
* pars_args(bt_args_t * bt_args, int argc, char * argv[]) -> void
*
* parse the command line arguments to bt_client using getopt and
* store the result in bt_args.
*
* ERRORS: Will exit on various errors
*
**/
void parse_args(bt_args_t * bt_args, int argc, char * argv[]) {
int ch; //ch for each flag
int n_peers = 0;
int i;
/* set the default args */
bt_args->verbose = 0; //no verbosity
//null save_file, log_file and torrent_file
memset(bt_args->save_file, 0x00, FILE_NAME_MAX);
memset(bt_args->torrent_file, 0x00, FILE_NAME_MAX);
memset(bt_args->log_file, 0x00, FILE_NAME_MAX);
//null out file pointers
bt_args->f_save = NULL;
//null bt_info pointer, should be set once torrent file is read
bt_args->bt_info = NULL;
//default lag file
strncpy(bt_args->log_file, "bt-client.log", FILE_NAME_MAX);
for (i = 0; i < MAX_CONNECTIONS; i++) {
bt_args->peers[i] = NULL; //initially NULL
}
bt_args->id = 0;
while ((ch = getopt(argc, argv, "hp:s:l:vI:")) != -1) {
switch (ch) {
case 'h': //help
usage(stdout);
exit(0);
break;
case 'v': //verbose
bt_args->verbose = 1;
break;
case 's': //save file
strncpy(bt_args->save_file, optarg, FILE_NAME_MAX);
break;
case 'l': //log file
strncpy(bt_args->log_file, optarg, FILE_NAME_MAX);
break;
case 'p': //peer
n_peers++;
//check if we are going to overflow
printf("trying to add peer");
if (n_peers > MAX_CONNECTIONS) {
fprintf(stderr, "ERROR: Can only support %d initial peers", MAX_CONNECTIONS);
usage(stderr);
exit(1);
}
printf("npeers-1%d\n", n_peers - 1);
bt_args->peers[n_peers - 1] = (peer_t *) malloc(sizeof (peer_t));
//parse peers
__parse_peer(bt_args->peers[n_peers - 1], optarg);
printf("please work%u\n", bt_args->peers[n_peers - 1]->port);
break;
case 'I':
bt_args->id = atoi(optarg);
break;
default:
fprintf(stderr, "ERROR: Unknown option '-%c'\n", ch);
usage(stdout);
exit(1);
}
}
argc -= optind;
argv += optind;
if (argc == 0) {
fprintf(stderr, "ERROR: Require torrent file\n");
usage(stderr);
exit(1);
}
//copy torrent file over
strncpy(bt_args->torrent_file, argv[0], FILE_NAME_MAX);
//copy file name if specified
if (argv[1]){
strncpy(bt_args->seedFile, argv[1], FILE_NAME_MAX);
}
return;
}