-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_setup.c
225 lines (192 loc) · 5.27 KB
/
bt_setup.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "bt_setup.h"
#include "bt_lib.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 * parse_str;
char * word;
unsigned short port;
char * ip, *t_ip;
char id[20];
memset(id, 0, sizeof(id));
char sep[] = ":";
int i=0;
int tem = 0, p=0;
//need to copy becaus strtok mangels things
parse_str = 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);
}
strcpy(t_ip,ip);
if (0 == inet_pton(AF_INET, t_ip, &tem))
{
fprintf(stderr, "ERROR: Parsing Peer: Invalid IP '%s'\n",t_ip);
usage(stderr);
exit(1);
}
p = port;
if(p<0 || p>65535)
{
fprintf(stderr, "ERROR: Parsing Peer: Invalid IP '%s'\n",t_ip);
usage(stderr);
exit(1);
}
//calculate the id, value placed in id
calc_id(ip,port,id);
//build the object we need
init_peer(peer, id, ip, port);
//free extra memory
free(parse_str);
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=0;
FILE *fp;
/* set the default args */
bt_args->verbose=0; //no verbosity
bt_args->bind=0;
//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);
strncpy(bt_args->save_file,"output",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, "h:b:p:s:l:vI:")) != -1) {
switch (ch) {
case 'h': //help
usage(stdout);
exit(0);
break;
case 'b': //bind
bt_args->bind = 1;
bt_args->peers[0] = malloc(sizeof(peer_t));
__parse_peer(bt_args->peers[0], optarg);
break;
case 'v': //verbose
bt_args->verbose = 1;
break;
case 's': //save file
if(optarg == NULL)
{
fprintf(stderr,"ERROR: Save file name required");
usage(stderr);
exit(1);
}
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
if(n_peers > MAX_CONNECTIONS){
fprintf(stderr,"ERROR: Can only support %d initial peers",MAX_CONNECTIONS);
usage(stderr);
exit(1);
}
bt_args->peers[n_peers] = malloc(sizeof(peer_t));
//parse peers
__parse_peer(bt_args->peers[n_peers], optarg);
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);
if(strstr(bt_args->torrent_file,".torrent") == NULL)
{
fprintf(stderr,"ERROR: Require .torrent file\n");
usage(stderr);
exit(1);
}
return ;
}