sockwrap.c 5.19 KB
Newer Older
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
/* sockwrap.c */

/* Berkley/WinSock socket API wrappers */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2002 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

38
#include <stdlib.h>		/* malloc/free on FreeBSD */
39
#include <string.h>		/* bzero (for FD_ZERO) on FreeBSD */
40
#include <errno.h>		/* ENOMEM */
41
#include <stdio.h>		/* SEEK_SET */
42
#include <string.h>
43

44
#include "genwrap.h"	/* SLEEP */
45
#include "gen_defs.h"	/* BOOL */
46
47
48
49
50
#include "sockwrap.h"	/* sendsocket */
#include "filewrap.h"	/* filelength */

int sendfilesocket(int sock, int file, long *offset, long count)
{
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* sendfile() on Linux may or may not work with non-blocking sockets ToDo */
#if defined(__FreeBSD__)
	off_t	total=0;
	off_t	wr=0;
	int		i;
	long	len;

	len=filelength(file);
	if(count<1 || count>len) {
		count=len;
		count-=offset==NULL?0:*offset;
	}
	while((i=sendfile(file,sock,(offset==NULL?0:*offset)+total,count-total,NULL,&wr,0))==-1 && errno==EAGAIN)  {
		total+=wr;
		SLEEP(1);
	}
	if(i==0)
		return((int)count);
	return(i);
#else
71
72
73
74
	char*	buf;
	long	len;
	int		rd;
	int		wr;
75
	int		total;
76
77
78
79
80

	if(offset!=NULL)
		if(lseek(file,*offset,SEEK_SET)<0)
			return(-1);

81
82
83
	len=filelength(file);

	if(count<1 || count>len) {
84
		count=len;
85
86
87
88
89
90
91
		count-=tell(file);		/* don't try to read beyond EOF */
	}

	if((buf=malloc(count))==NULL) {
		errno=ENOMEM;
		return(-1);
	}
92
93
94
95
96
97
98

	rd=read(file,buf,count);
	if(rd!=count) {
		free(buf);
		return(-1);
	}

99
100
101
102
103
104
105
106
107
108
109
110
	for(total=wr=0;total<count;total+=wr) {
		wr=sendsocket(sock,buf+total,count-total);
		if(wr>0)
			continue;
		if(wr==SOCKET_ERROR && ERROR_VALUE==EWOULDBLOCK) {
			wr=0;
			SLEEP(1);
			continue;
		}
		break;
	}

111
112
113
	free(buf);

	if(offset!=NULL)
114
115
116
117
		(*offset)+=total;

	if(wr<1)
		return(wr);
118

119
	return(total);
120
#endif
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
int recvfilesocket(int sock, int file, long *offset, long count)
{
	/* Writes a file from a socket -
	 *
	 * sock		- Socket to read from
	 * file		- File descriptior to write to
	 *				MUST be open and writeable
	 * offset	- pointer to file offset to start writing at
	 *				is set to offset writing STOPPED
	 *				on return
	 * count	- number of bytes to read/write
	 *
	 * returns -1 if an error occurse, otherwise
	 * returns number ob bytes written and sets offset
	 * to the new offset
	 */
	 
	char*	buf;
	int		rd;
	int		wr;

	if(count<1) {
		errno=ERANGE;
		return(-1);
	}
		
	if((buf=malloc(count))==NULL) {
		errno=ENOMEM;
		return(-1);
	}

	if(offset!=NULL)
		if(lseek(file,*offset,SEEK_SET)<0)
			return(-1);

	rd=read(sock,buf,count);
	if(rd!=count) {
		free(buf);
		return(-1);
	}

	wr=write(file,buf,rd);
	free(buf);

	if(offset!=NULL)
		(*offset)+=wr;

	return(wr);
}


174
/* Return true if connected, optionally sets *rd_p to true if read data available */
175
BOOL socket_check(SOCKET sock, BOOL* rd_p, BOOL* wr_p, DWORD timeout)
176
177
178
{
	char	ch;
	int		i,rd;
179
180
181
	fd_set	rd_set;
	fd_set	wr_set;
	fd_set*	wr_set_p=NULL;
182
183
184
185
186
	struct	timeval tv;

	if(rd_p!=NULL)
		*rd_p=FALSE;

187
188
189
	if(wr_p!=NULL)
		*wr_p=FALSE;

190
191
192
	if(sock==INVALID_SOCKET)
		return(FALSE);

193
194
195
196
197
198
	FD_ZERO(&rd_set);
	FD_SET(sock,&rd_set);
	if(wr_p!=NULL) {
		wr_set_p=&wr_set;
		FD_ZERO(wr_set_p);
		FD_SET(sock,wr_set_p);
199
200
		if(rd_p==NULL)
			FD_CLR(sock,&rd_set);
201
	}
202

203
204
205
	/* Convert timeout from ms to sec/usec */
	tv.tv_sec=timeout/1000;
	tv.tv_usec=(timeout%1000)*1000;
206

207
	i=select(sock+1,&rd_set,wr_set_p,NULL,&tv);
208
209
210
211
212
213
	if(i==SOCKET_ERROR)
		return(FALSE);

	if(i==0) 
		return(TRUE);

214
215
216
217
218
219
	if(wr_p!=NULL && FD_ISSET(sock,wr_set_p)) {
		*wr_p=TRUE;
		if(i==1)
			return(TRUE);
	}

220
221
222
223
224
225
226
227
	if(rd_p !=NULL)  {
		rd=recv(sock,&ch,1,MSG_PEEK);
		if(rd==1 
			|| (rd==SOCKET_ERROR && ERROR_VALUE==EMSGSIZE)) {
			if(rd_p!=NULL)
				*rd_p=TRUE;
			return(TRUE);
		}
228
229
230
231
232
	}

	return(FALSE);
}

233