-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.c
275 lines (216 loc) · 5.95 KB
/
comm.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/***********************************************************************/
/* This File implements the common code used by myftp client and server*/
/* it mainly contains all the socket code for myftp and some support */
/* functions. */
/***********************************************************************/
#include "comm.h"
#include <errno.h>
extern int errno;
/*--- Open a socket connection and populate the connection structure ---
*/
int open_sock_connection( char * server,
unsigned short port,
connection * myconn )
{
struct hostent * hostinfo = NULL;
if ( ( hostinfo = gethostbyname(server) ) == NULL ) {
printlog("gethostbyname for %s FAILED\n",server);
exit(h_errno);
}
printlog("%s","after gethostbyname\n");
printlog(" sizeof(s_Addr) = %d, h_length = %d\n",
sizeof(myconn->saddr.sin_addr.s_addr),
hostinfo->h_length);
memcpy( (char *)&(myconn->saddr.sin_addr.s_addr),
hostinfo->h_addr_list[0],
hostinfo->h_length );
printlog("%s","after memcpy\n");
printlog ( "addr = %s\n", inet_ntoa(myconn->saddr.sin_addr));
myconn->saddr.sin_port = htons(port);
myconn->saddr.sin_family = AF_INET;
if ( ( myconn->sock = socket( AF_INET, SOCK_STREAM, 0)) < 0 ) {
printlog ( "%s","socket failed \n" );
exit(1);
}
if (connect(myconn->sock,( SA * )&myconn->saddr,sizeof( SI )) < 0 ) {
perror("connect failed");
exit(1);
}
printlog ( "%s","connect successful\n" );
return(myconn->sock);
}
/* ----------- Get the server response -----------------
*/
int get_reply( connection myconn, char * buf, int len)
{
int rlen = 0;
if ( (rlen = recv( myconn.sock, buf, len, 0)) < 0 ) {
printlog ( "%s","recv failed\n" );
return(-1);
}
return(rlen);
}
/* ----------------Send request to the server----------------
*/
int send_request( connection conn, char * buf, int len)
{
int sent = 0;
int rlen = len;
while(len > 0) {
if ( ( sent = send( conn.sock, buf, len, 0)) < 0 ) {
printlog("%s","send failed\n");
return -1;
}
len = len - sent;
}
return rlen;
}
/* -------------------Create a listening socket----------------------
used by the server for control and by client for data connection.
*/
int create_listen_socket(connection *listenfd)
{
int len;
int optval;
len = sizeof(SI);
listenfd->sock = socket(AF_INET, SOCK_STREAM, 0);
optval = 1;
setsockopt(listenfd->sock, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(int));
if (listenfd->saddr.sin_addr.s_addr == 0)
listenfd->saddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (listenfd->saddr.sin_family == 0)
listenfd->saddr.sin_family = AF_INET;
/* This statement not needed as u can see
*/
if (listenfd->saddr.sin_port == 0)
listenfd->saddr.sin_port = htons(0);
if (bind(listenfd->sock, (SA *)&listenfd->saddr, len) < 0)
{
printlog("%s","bind failed\n");
return -1;
}
listen(listenfd->sock, SOMAXCONN);
getsockname(listenfd->sock, (SA *)&listenfd->saddr, &len);
printlog("port = %d\n",ntohs(listenfd->saddr.sin_port));
return listenfd->sock;
}
/* ----------convert struct sockaddr to "," seperated string format----------
used by the client to convert its data socket info to string to be sent
to the server with the PORT command.
*/
int get_port_info(connection conn, char *info)
{
char hostname[512];
char hostip[512];
struct hostent *hostinfo = NULL;
int port;
int i;
struct in_addr temp;
gethostname(hostname, 512);
if ( ( hostinfo = gethostbyname(hostname) ) == NULL ) {
printlog("gethostbyname for %s FAILED\n",hostname);
return -1;
}
memcpy( (char *)&temp, hostinfo->h_addr_list[0], hostinfo->h_length );
strcpy(hostip, inet_ntoa(temp));
port = ntohs(conn.saddr.sin_port);
for(i=0; hostip[i] != '\0'; i++) {
if ( hostip[i] == '.' )
hostip[i] = ',';
}
sprintf(info, "%s,%d,%d",hostip,port/256,port%256);
return 0;
}
/* -----------implementation of accept taking care of interrupts -----------
*/
int accept_connection(connection *lconn, connection *aconn)
{
int len;
len = sizeof(struct sockaddr_in);
aconn->sock = -1;
while (aconn->sock < 0) {
aconn->sock = accept(lconn->sock, (SA *)&aconn->saddr, &len);
if (aconn->sock == -1)
if (errno == EINTR)
continue;
else
break;
}
printlog("accepted : %d\n",aconn->sock);
return aconn->sock;
}
/* -------------get data from connection ---------------
*/
int get_data(connection conn, int fd)
{
return read_write_data(conn.sock, fd);
}
/* -------------put data to connection ---------------
*/
int put_data(connection conn, int fd)
{
return read_write_data(fd, conn.sock);
}
/* -------------generic read write funtion----------------
called by get_data() and put_data() for data transfer
*/
int read_write_data(int inputfd, int outputfd)
{
char buf[1024];
int nread = 0;
int total = 0;
int nwrite = 0;
while( ( nread = read(inputfd, buf, sizeof(buf))) > 0) {
total = total + nread;
while(nread > 0) {
nwrite = write(outputfd, buf, nread);
if(nwrite < 0) {
printlog("%s","write error\n");
return -1;
}
nread = nread - nwrite;
}
}
if ( nread == 0 )
return total;
else
return -1;
}
/* --------------convert string port info to int---------------
used by the server to convert the data sent by client along with
the PORT command.
*/
int get_data_connection_info( char *portstr, char * ip, int *port)
{
int i,j;
char *tmp;
char tmpport[4];
tmp = portstr;
for(i=0; tmp[i] == ' ' && tmp[i] != '\0' ; i++)
tmp++;
i = 0;j = 0;
while(i<4) {
if(*tmp==',') {
ip[j] = '.';
i++;
}
else
ip[j] = *tmp;
tmp++;
j++;
}
ip[j-1] = '\0';
j=0;
while(*tmp != ',') {
tmpport[j] = *tmp;
tmp++;
j++;
}
tmp++;
tmpport[j] = '\0';
*port = atoi(tmpport) * 256 + atoi(tmp);
#ifdef DEBUG
printf("str = %s, ip = %s, port = %d.\n",portstr,ip,*port);
#endif
}