• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/lighttpd-1.4.39/src/
1#include "network_backends.h"
2
3#include "network.h"
4#include "fdevent.h"
5#include "log.h"
6#include "stat_cache.h"
7
8#include "sys-socket.h"
9
10#include <sys/time.h>
11#include <stdlib.h>
12
13#include <fcntl.h>
14#include <sys/stat.h>
15#include <unistd.h>
16
17#include <errno.h>
18#include <string.h>
19
20#define DBE 0
21
22int network_open_file_chunk(server *srv, connection *con, chunkqueue *cq) {
23	chunk* const c = cq->first;
24	off_t file_size, offset, toSend;
25
26	force_assert(NULL != c);
27	force_assert(FILE_CHUNK == c->type || SMB_CHUNK == c->type);
28	force_assert(c->offset >= 0 && c->offset <= c->file.length);
29
30	//Cdbg(1,"0 c->file.start=%lld, c->offset=%lld, c->file.length=%lld", c->file.start, c->offset, c->file.length);
31	offset = c->file.start + c->offset;
32	toSend = c->file.length - c->offset;
33
34	if (-1 == c->file.fd) {
35		stat_cache_entry *sce = NULL;
36
37		if (HANDLER_ERROR == stat_cache_get_entry(srv, con, c->file.name, &sce)) {
38			log_error_write(srv, __FILE__, __LINE__, "ssb", "stat-cache failed:", strerror(errno), c->file.name);
39			return -1;
40		}
41
42		if( c->type == SMB_CHUNK ){
43			if (-1 == (c->file.fd = smbc_wrapper_open(con,c->file.name->ptr, O_RDONLY, 0755))) {
44				log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
45				return -1;
46			}
47		}
48		else{
49			if (-1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY|O_NOCTTY))) {
50				log_error_write(srv, __FILE__, __LINE__, "ssb", "open failed:", strerror(errno), c->file.name);
51				return -1;
52			}
53
54			fd_close_on_exec(c->file.fd);
55		}
56
57		file_size = sce->st.st_size;
58	}
59	else {
60		struct stat st;
61
62		if( c->type == SMB_CHUNK ){
63			if (-1 == smbc_wrapper_stat(con, c->file.name->ptr, &st)) {
64				log_error_write(srv, __FILE__, __LINE__, "ss", "smbc_wrapper_stat failed:", strerror(errno));
65				return -1;
66			}
67		}
68		else{
69			if (-1 == fstat(c->file.fd, &st)) {
70				log_error_write(srv, __FILE__, __LINE__, "ss", "fstat failed:", strerror(errno));
71				return -1;
72			}
73		}
74
75		file_size = st.st_size;
76	}
77	//Cdbg(1,"1 file_size=%d, toSend=%d, offset=%d", file_size, toSend, offset);
78
79	if (offset > file_size || toSend > file_size || offset > file_size - toSend) {
80		//Cdbg(1,"2 file_size=%d", file_size);
81		log_error_write(srv, __FILE__, __LINE__, "sb", "file was shrinked:", c->file.name);
82		return -1;
83	}
84
85	return 0;
86}
87
88int network_write_file_chunk_no_mmap(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
89	chunk* const c = cq->first;
90	off_t offset, toSend;
91	ssize_t r;
92
93	force_assert(NULL != c);
94	force_assert(FILE_CHUNK == c->type);
95	force_assert(c->offset >= 0 && c->offset <= c->file.length);
96
97	offset = c->file.start + c->offset;
98	toSend = c->file.length - c->offset;
99	if (toSend > 64*1024) toSend = 64*1024; /* max read 64kb in one step */
100	if (toSend > *p_max_bytes) toSend = *p_max_bytes;
101
102	if (0 == toSend) {
103		chunkqueue_remove_finished_chunks(cq);
104		return 0;
105	}
106
107	if (0 != network_open_file_chunk(srv, con, cq)) return -1;
108
109	buffer_string_prepare_copy(srv->tmp_buf, toSend);
110
111	if (-1 == lseek(c->file.fd, offset, SEEK_SET)) {
112		log_error_write(srv, __FILE__, __LINE__, "ss", "lseek: ", strerror(errno));
113		return -1;
114	}
115	if (-1 == (toSend = read(c->file.fd, srv->tmp_buf->ptr, toSend))) {
116		log_error_write(srv, __FILE__, __LINE__, "ss", "read: ", strerror(errno));
117		return -1;
118	}
119
120#if defined(__WIN32)
121	if ((r = send(fd, srv->tmp_buf->ptr, toSend, 0)) < 0) {
122		int lastError = WSAGetLastError();
123		switch (lastError) {
124		case WSAEINTR:
125		case WSAEWOULDBLOCK:
126			break;
127		case WSAECONNRESET:
128		case WSAETIMEDOUT:
129		case WSAECONNABORTED:
130			return -2;
131		default:
132			log_error_write(srv, __FILE__, __LINE__, "sdd",
133				"send failed: ", lastError, fd);
134			return -1;
135		}
136	}
137#else /* __WIN32 */
138	if ((r = write(fd, srv->tmp_buf->ptr, toSend)) < 0) {
139		switch (errno) {
140		case EAGAIN:
141		case EINTR:
142			break;
143		case EPIPE:
144		case ECONNRESET:
145			return -2;
146		default:
147			log_error_write(srv, __FILE__, __LINE__, "ssd",
148				"write failed:", strerror(errno), fd);
149			return -1;
150		}
151	}
152#endif /* __WIN32 */
153
154	if (r >= 0) {
155		*p_max_bytes -= r;
156		chunkqueue_mark_written(cq, r);
157	}
158
159	return (r > 0 && r == toSend) ? 0 : -3;
160}
161