File: socket.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (341 lines) | stat: -rw-r--r-- 6,763 bytes parent folder | download | duplicates (3)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* $Id: socket.c,v 1.8 2005/10/05 20:02:09 splicednetworks Exp $
 *
 * socket.c --
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * 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. See COPYING for more details.
 */

#include "syshdr.h"

#include "ftp.h"
#include "socket.h"
#include "xmalloc.h"

Socket *sock_create(void)
{
#if 0
	int on = 1;
#endif
	int tfd;
	Socket *sockp;

	sockp = (Socket *)xmalloc(sizeof(Socket));

	sockp->remote_addr.sin_family = AF_INET;

	/* create a socket file descriptor */
	sockp->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sockp->handle == -1)
		goto err0;
#if 0
	/* enable local address reuse */
	if(setsockopt(sockp->handle, SOL_SOCKET, SO_REUSEADDR,
				  &on, sizeof(on)) == -1)
	{
		goto err1;
	}
#endif
	if (-1 == (tfd = dup(sockp->handle))) {
		goto err1;
 	}
/*	sockp->sin = fdopen(sockp->handle, "r");
	if(sockp->sin == 0) {
		goto err1;
	}*/

	if(0 == (sockp->sin = fdopen(tfd, "r"))) {
		close(tfd);
		goto err1;
 	}

/*	sockp->sout = fdopen(sockp->handle, "w");
	if(sockp->sout == 0) {
		goto err1;
	}*/

	if (-1 == (tfd = dup(sockp->handle))) {
		goto err2;
	}
	if (0 == (sockp->sout = fdopen(tfd, "w"))) {
		close(tfd);
		goto err2;
	}
	
	return sockp;

err2:
	fclose(sockp->sin);
err1:
	close(sockp->handle);
err0:
	free(sockp);
	return 0;
}

void sock_destroy(Socket *sockp)
{
	if(!sockp)
		return;

	fclose(sockp->sin);
	if (sockp->sin != sockp->sout)		/* 853836 */
		fclose(sockp->sout);
	close(sockp->handle);
	free(sockp);
}

int sock_connect_addr(Socket *sockp, struct sockaddr_in *sa)
{
	/* connect to the socket */
	if(connect(sockp->handle, (struct sockaddr *)sa,
			   sizeof(struct sockaddr)) == -1)
	{
		perror("connect()");
		close(sockp->handle);
		return -1;
	}

	if(sock_getsockname(sockp, &sockp->local_addr) == -1) {
		perror("getsockname()");
		close(sockp->handle);
		return -1;
	}
	sockp->connected = true;

	return 0;
}

int sock_connect(Socket *sockp)
{
	return sock_connect_addr(sockp, &sockp->remote_addr);
}

int sock_connect_host(Socket *sockp, Host *hp)
{
	sockp->remote_addr.sin_port = hp->port;
	if(hp->hep) {
		memcpy((void *)&sockp->remote_addr.sin_addr,
			   hp->hep->h_addr,
			   hp->hep->h_length);
	} else {
		/* we don't have the hostname, only an ip number */
		memcpy((void *)&sockp->remote_addr.sin_addr,
			   &hp->alt_h_addr,   /* this is a struct in_addr */
			   hp->alt_h_length);
	}
	sockp->remote_addr.sin_family = AF_INET;

	return sock_connect(sockp);
}

void sock_copy(Socket *tosock, const Socket *fromsock)
{
	memcpy(&tosock->remote_addr, &fromsock->remote_addr,
		   sizeof(tosock->remote_addr));
	memcpy(&tosock->local_addr, &fromsock->local_addr,
		   sizeof(tosock->local_addr));
	tosock->connected = sock_connected(fromsock);
}

bool sock_connected(const Socket *sockp)
{
	return sockp && sockp->connected;
}

/* accept an incoming connection */
int sock_accept(Socket *sockp, const char *mode, bool pasvmode)
{
	int s;
	struct sockaddr sa;
	int l = sizeof(sa);
	int tfd;

	if(!pasvmode) {
		s = accept(sockp->handle, &sa, &l);
		close(sockp->handle);
		if(s == -1) {
			perror("accept()");
			sockp->handle = -1;
			return -1;
		}
		sockp->handle = s;
		memcpy(&sockp->local_addr, &sa, sizeof(sockp->local_addr));
	}
	fclose(sockp->sin);
	fclose(sockp->sout);

	if (-1 == (tfd = dup(sockp->handle))) {
		perror("dup()");
		goto errdup0;
	}
	if (!(sockp->sin = fdopen(tfd, mode))) {
		close(tfd);
		perror("fdopen()");
		goto errdup0;
	}

	if (-1 == (tfd = dup(sockp->handle))) {
		perror("dup()");
		goto errdup1;
	}
	if (!(sockp->sout = fdopen(tfd, mode))) {
		close(tfd);
		perror("fdopen()");
		goto errdup1;
	}

	return 0;

errdup1:
	fclose(sockp->sin);
errdup0:
	close(sockp->handle);
	sockp->handle = -1;
	return -1;
}

int sock_listen(Socket *sockp)
{
	unsigned int l = sizeof(sockp->local_addr);
	sockp->local_addr.sin_port = 0;  /* let system pick port */

	if(bind(sockp->handle, (struct sockaddr *)&sockp->local_addr, l) == -1)
	{
		perror("bind()");
		return -1;
	}

	/* wait for an incoming connection */
	if(listen(sockp->handle, 1) == -1)
		return -1;

	if(sock_getsockname(sockp, &sockp->local_addr) == -1) {
		perror("getsockname()");
		return -1;
	}

	return 0;
}

void sock_throughput(Socket *sockp)
{
#if defined(IPTOS_THROUGHPUT) && defined(HAVE_SETSOCKOPT)
	/* set type of service */
	int tos = IPTOS_THROUGHPUT;
	if(setsockopt(sockp->handle, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == -1)
		perror(_("setsockopt(THROUGHPUT)... ignored"));
#endif
}

void sock_lowdelay(Socket *sockp)
{
#if defined(IPTOS_LOWDELAY) && defined(HAVE_SETSOCKOPT)
	/* set type of service */
	int tos = IPTOS_LOWDELAY;
	if(setsockopt(sockp->handle, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == -1)
		perror(_("setsockopt(LOWDELAY)... ignored"));
#endif
}

const struct sockaddr_in *sock_local_addr(Socket *sockp)
{
	return &sockp->local_addr;
}

const struct sockaddr_in *sock_remote_addr(Socket *sockp)
{
	return &sockp->remote_addr;
}

ssize_t sock_read(Socket *sockp, void *buf, size_t num)
{
	return read(sockp->handle, buf, num);
}

ssize_t sock_write(Socket *sockp, void *buf, size_t num)
{
	return write(sockp->handle, buf, num);
}

int sock_get(Socket *sockp)
{
	return fgetc(sockp->sin);
}

int sock_put(Socket *sockp, int c)
{
	if(fputc(c, sockp->sout) == EOF)
		return EOF;
	return 0;
}

int sock_unget(Socket *sockp, int c)
{
	return ungetc(c, sockp->sin);
}

int sock_vprintf(Socket *sockp, const char *str, va_list ap)
{
	return vfprintf(sockp->sout, str, ap);
}

int sock_krb_vprintf(Socket *sockp, const char *str, va_list ap)
{
#ifdef SECFTP
	if(ftp->sec_complete)
		return sec_vfprintf(sockp->sout, str, ap);
	else
#endif
		return sock_vprintf(sockp, str, ap);
}

int sock_printf(Socket *sockp, const char *str, ...)
{
	va_list ap;
	int r;

	va_start(ap, str);
	r = sock_vprintf(sockp, str, ap);
	va_end(ap);
	return r;
}

int sock_krb_printf(Socket *sockp, const char *str, ...)
{
	va_list ap;
	int r;

	va_start(ap, str);
	r = sock_krb_vprintf(sockp, str, ap);
	va_end(ap);
	return r;
}

/* flushes output */
int sock_flush(Socket *sockp)
{
	return fflush(sockp->sout);
}

int sock_telnet_interrupt(Socket *sockp)
{
	if(send(sockp->handle, "\xFF\xF4\xFF" /* IAC,IP,IAC */, 3, MSG_OOB) != 3)
		return -1;
	if(fputc(242/*DM*/, sockp->sout) == EOF)
		return -1;
	return 0;
}

int sock_getsockname(Socket *sockp, struct sockaddr_in *sa)
{
	unsigned int len = sizeof(struct sockaddr_in);
	if(getsockname(sockp->handle, (struct sockaddr *)sa, &len) == -1)
		return -1;
	return 0;
}