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
/*
 *  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"

void do_transmit(ifstream *fp, int sockfd, enum proto default_proto, struct sockaddr_in server) {
	char sendline[MAXLINE];


	fp->seekg(0, std::ios::end);
	//int64_t size = fp->tellg();
	fp->seekg(0);

	int64_t size = 4294967295;
	int64_t read = 0;
	int64_t unitsize = 0;
	int64_t toTransfer = 4294967295;

	//writen(sockfd, (char *) &size, sizeof(int64_t), default_proto, server);
	//writen(sockfd, (const void *)"989", MAXLINE, default_proto, server);
	//while (fgets(sendline, MAXLINE, fp) != NULL) {
	while (toTransfer > 0) {
		unitsize = ((toTransfer >= MAXLINE) ? MAXLINE : toTransfer);

		fp->read(sendline, MAXLINE);
		//out << sendline << endl;
		writen(sockfd, sendline, strlen(sendline), default_proto, server);

		//read += MAXLINE;
		toTransfer -= unitsize;
		if (errno)
			printerrno("do_transmit");
	}

	toTransfer = size;
}

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

	int ind = 1;
		while (ind < argc) {
			switch (argv[ind][1]) {
				// protocol
				case 'P':
					test = string(argv[++ind]);
					if (strcmp(test.c_str(),"TCP")==0) {
						default_proto = TCP;
						printf("Using TCP\n");
					} else if (strcmp(test.c_str(),"DCCP")==0) {
						default_proto = DCCP;
						printf("Using DCCP\n");
					} else if (strcmp(test.c_str(),"UDP")==0) {
						default_proto = UDP;
						printf("Using UDP\n");
					} else if (strcmp(test.c_str(),"UDT")==0) {
						default_proto = PUDT;
						printf("Using UDT\n");
					} else {
						goto usage;
					}
					break;
				// port
				case 'p':
					port = string(argv[++ind]);
					cout << "Connecting to port " << port.c_str() << endl;
					break;
				// server
				case 'h':
					host = argv[++ind];
					cout << "Connecting to host " << host << endl;
					break;
				// file
				case 'f':
					file = string(argv[++ind]);
					cout << "Transfering file " << file.c_str() << endl;
					break;
				default:
					goto usage;
					break;
			}

			ind++;
		}


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

//	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	hp = gethostbyname(host);
	   if (hp==0) printerrno("Unknown host");
	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;
	}
	/* above use of 5001 in string is a hack - fix */
		
/*	if (inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) <= 0) {
		printerrno("inet_pton");
		return -1;
	} */
	socket_options(sockfd);
	if (errno) {
		printerrno("socket_options");
		return 1;
	}

	if (default_proto != UDP) {
		connect(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr));
		//connect(sockfd, hostinfo->ai_addr, hostinfo->ai_addrlen);
		if (errno) {
			printerrno("connect");
			return 1;
		}
	}

	printf("Begin transfer\n");

	ifstream *fp;

	fp = new ifstream(file.c_str(), ios::in | ios::binary);;//
	if (!fp) {
		cout << "fehler" << endl;
	}

	/*
	if (file == "") {
		fp = stdin;
	}
	else {
		fp = fopen(file.c_str(), "r");
	}
	*/

	do_transmit(fp, sockfd,(enum proto) default_proto, servaddr);
	return 0;
usage:
	fprintf(stderr,"Put the hostname to connect to as last argument\n");
	fprintf(stderr,"-p proto defines the protocol to use where proto\n");
	fprintf(stderr,"can be TCP or DCCP\n");
	return 1;
}