-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_setup.cpp
165 lines (134 loc) · 3.37 KB
/
bt_setup.cpp
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
#include "bt_setup.h"
void bt_setup::usage(FILE* file) {
}
void bt_setup::parse_args(bt_args_t* bt_args, int argc, char** argv) {
int ch; //ch for each flag
int n_peers = 0;
int i;
struct hostent * hostinfo;
/* 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;
bt_args->bindCheck =0;
while ((ch = getopt(argc, argv, "hp:s:b: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
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] = new peer_t;
//parse peers
__parse_peer(bt_args->peers[n_peers], optarg);
break;
case 'I':
bt_args->id = atoi(optarg);
break;
case 'b':
bt_args->bindCheck = 1;
if (!(hostinfo = gethostbyname(argv[2]))){
fprintf(stderr, "ERROR: Invalid host name");
usage(stderr);
exit(1);
}
bt_args->bindToThis.sin_family = hostinfo->h_addrtype;
bcopy((char *) hostinfo->h_addr,
(char *) &(bt_args->bindToThis.sin_addr.s_addr),
hostinfo->h_length);
bt_args->bindToThis.sin_port = htons(6060);
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);
return;
}
void bt_setup::__parse_peer(peer_t* peer, char* peer_st) {
char * parse_str;
bt_lib bt;
char * word;
unsigned short port;
char * ip;
char id[20];
char sep[] = ":";
int i;
//need to copy becaus strtok mangels things
parse_str = new char[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);
break;
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
bt.calc_id(ip, port, id);
//memcpy(&peer->id,&id,20);
//build the object we need
bt.init_peer(peer, id, ip, port);
//free extra memory
delete[] parse_str;
return;
}