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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 *  Copyright (c) 2005 Ian McDonald <[email protected]>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "common.hpp"

int do_transmit(ifstream *fp, int sockfd, enum proto default_proto, struct sockaddr_in server) {
	char * sendline = (char*) calloc(MAXLINE, sizeof(char));

	int64_t read, unitsize, toTransfer, readBytes;
	int length = sizeof(struct sockaddr_in);
	int ssize, nwritten;

	int mtu_size = 256;
	socklen_t sizeMTU = (socklen_t) sizeof(mtu_size);
	if (default_proto == DCCP) {
		getsockopt(sockfd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS,(socklen_t*) &mtu_size, &sizeMTU);
	}

	read = 0;
	unitsize = 0;
	toTransfer = fileSize;

	while (toTransfer > 0) {
		if (default_proto == DCCP && mtu_size < MAXLINE) {
			unitsize = ((toTransfer >= mtu_size) ? mtu_size : toTransfer);
		}
		else {
			unitsize = ((toTransfer >= MAXLINE) ? MAXLINE : toTransfer);
		}

		fp->read(sendline, unitsize);

		ssize = 0;
		nwritten = 0;

		while (ssize < unitsize) {
			switch(default_proto) {
				case TCP:
					if ((nwritten = send(sockfd, sendline + ssize, unitsize - ssize, 0)) < 0) {
						return -1;
					}
					break;

				case DCCP:
					do {
						nwritten = send(sockfd, sendline + ssize, unitsize - ssize, 0);
					} while (nwritten < 0 && errno == EAGAIN);
					break;

				case UDP:
					if ((nwritten = sendto(sockfd,sendline + ssize,unitsize - ssize, 0, (const sockaddr*) &server, length)) < 0) {
						return -1;
					}
					break;

				case PUDT:
					if (UDT::ERROR == (nwritten = UDT::send(sockfd, sendline + ssize, unitsize - ssize, 0))) {
						cout << "send:" << UDT::getlasterror().getErrorMessage() << endl;
						return -1;
					}
					break;
			}

			ssize += nwritten;
		}

		toTransfer -= unitsize;
	}

	fp->close();
	delete fp;
	free(sendline);

	return 0;
}

int main(int argc, char **argv) {
	int sockfd;
	struct sockaddr_in servaddr;
	struct addrinfo *hostinfo;
	char *host;
	struct hostent *hp;
	string protoName;

	int ind = 1;
	while (ind < argc) {
		switch (argv[ind][1]) {
			// protocol
			case 'P':
				protoName = string(argv[++ind]);
				if (strcmp(protoName.c_str(),"TCP")==0) {
					default_proto = TCP;
				}
				else if (strcmp(protoName.c_str(),"DCCP")==0) {
					default_proto = DCCP;
				}
				else if (strcmp(protoName.c_str(),"UDP")==0) {
					default_proto = UDP;
				}
				else if (strcmp(protoName.c_str(),"UDT")==0) {
					default_proto = PUDT;
				}
				else {
					goto usage;
				}
				break;

			// port
			case 'p':
				port = string(argv[++ind]);
				break;

			// server
			case 's':
				host = argv[++ind];

				break;
			// file
			case 'f':
				file = string(argv[++ind]);
				break;

			case 'h':
				goto usage;
				break;

			default:
				goto usage;
				break;
		}

		ind++;
	}

	if (default_proto == PUDT) {
		UDT::startup();
	}

	cout << "Using protocol " << usedProto(default_proto).c_str() << endl;
	cout << "Connecting to host " << host << " on port " << port.c_str() << endl;

	if ((sockfd = make_socket()) < 0) {
		printerrno("socket");
		return 1;
	}

	servaddr.sin_family = AF_INET;
	hp = gethostbyname(host);

	if (hp == 0) {
		printerrno("Unknown host");
		return -1;
	}

	bcopy((char *)hp->h_addr, (char *)&servaddr.sin_addr, hp->h_length);
	servaddr.sin_port = htons(str_to_int(port));

	if (getaddrinfo(host, port.c_str(), NULL, &hostinfo)) {
		printerrno("getaddrinfo");
		return 1;
	}

	socket_options(sockfd);	

	if (errno) {
		printerrno("socket_options");
		return 1;
	}

	if (default_proto != UDP) {

#ifdef DCCP_SOCKOPT_CCID
		if (default_proto == DCCP) {
			int ccid = 3;
			setsockopt(sockfd, SOL_DCCP, DCCP_SOCKOPT_CCID,(uint8_t*) &ccid, sizeof(uint8_t));
			if (errno) {
                        	printerrno("CCID");
                        	return -1;
                	}
                        cout << "Using CCID " << ccid << endl;
		}
#endif

		if (default_proto != PUDT) {
			connect(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr));
		}
		else {
			if (UDT::ERROR == UDT::connect(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr)))
			{
				cout << "connect: " << UDT::getlasterror().getErrorMessage() << endl;
				return -1;
			}
		}

		if (errno) {
			printerrno("connect");
			return 1;
		}
	}

	cout << "Begin to transfer file: " << file.c_str() << endl;

	ifstream *fp;
	fp = new ifstream(file.c_str(), ios::in | ios::binary);;

	if (!fp) {
		cout << "couldn't open file" << endl;
		return -1;
	}

	do_transmit(fp, sockfd,(enum proto) default_proto, servaddr);
	
	if (default_proto == PUDT) {
		UDT::close(sockfd);
		UDT::cleanup();
	}

	return 0;

usage:
	cerr << "-P protocol name defines the protocol to use where proto" << endl;
	cerr << "\t can be TCP, DCCP, UDP or UDT" << endl;
	cerr << "-p port defines the port to connect to" << endl;
	cerr << "-s server defines the server address to connect to" << endl;
	cerr << "-f file the file to be send" << endl;
	cerr << "-h prints this help and exits" << endl;

	return 1;
}