1*76404edcSAsim Jamshed #include "network_backends.h"
2*76404edcSAsim Jamshed 
3*76404edcSAsim Jamshed #include "network.h"
4*76404edcSAsim Jamshed #include "fdevent.h"
5*76404edcSAsim Jamshed #include "log.h"
6*76404edcSAsim Jamshed #include "stat_cache.h"
7*76404edcSAsim Jamshed 
8*76404edcSAsim Jamshed #include "sys-socket.h"
9*76404edcSAsim Jamshed 
10*76404edcSAsim Jamshed #include <sys/types.h>
11*76404edcSAsim Jamshed #include <sys/stat.h>
12*76404edcSAsim Jamshed #include <sys/time.h>
13*76404edcSAsim Jamshed #include <errno.h>
14*76404edcSAsim Jamshed #include <fcntl.h>
15*76404edcSAsim Jamshed #include <unistd.h>
16*76404edcSAsim Jamshed #include <string.h>
17*76404edcSAsim Jamshed #include <stdlib.h>
18*76404edcSAsim Jamshed 
19*76404edcSAsim Jamshed #ifdef HAVE_SYS_FILIO_H
20*76404edcSAsim Jamshed # include <sys/filio.h>
21*76404edcSAsim Jamshed #endif
22*76404edcSAsim Jamshed 
23*76404edcSAsim Jamshed #ifdef HAVE_SYS_RESOURCE_H
24*76404edcSAsim Jamshed # include <sys/resource.h>
25*76404edcSAsim Jamshed #endif
26*76404edcSAsim Jamshed 
network_write_chunkqueue_write(server * srv,connection * con,int fd,chunkqueue * cq,off_t max_bytes)27*76404edcSAsim Jamshed int network_write_chunkqueue_write(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes) {
28*76404edcSAsim Jamshed 	chunk *c;
29*76404edcSAsim Jamshed 
30*76404edcSAsim Jamshed 	for(c = cq->first; (max_bytes > 0) && (NULL != c); c = c->next) {
31*76404edcSAsim Jamshed 		int chunk_finished = 0;
32*76404edcSAsim Jamshed 
33*76404edcSAsim Jamshed 		switch(c->type) {
34*76404edcSAsim Jamshed 		case MEM_CHUNK: {
35*76404edcSAsim Jamshed 			char * offset;
36*76404edcSAsim Jamshed 			off_t toSend;
37*76404edcSAsim Jamshed 			ssize_t r;
38*76404edcSAsim Jamshed 
39*76404edcSAsim Jamshed 			if (c->mem->used == 0) {
40*76404edcSAsim Jamshed 				chunk_finished = 1;
41*76404edcSAsim Jamshed 				break;
42*76404edcSAsim Jamshed 			}
43*76404edcSAsim Jamshed 
44*76404edcSAsim Jamshed 			offset = c->mem->ptr + c->offset;
45*76404edcSAsim Jamshed 			toSend = c->mem->used - 1 - c->offset;
46*76404edcSAsim Jamshed 			if (toSend > max_bytes) toSend = max_bytes;
47*76404edcSAsim Jamshed 
48*76404edcSAsim Jamshed #ifdef __WIN32
49*76404edcSAsim Jamshed 			if ((r = send(fd, offset, toSend, 0)) < 0) {
50*76404edcSAsim Jamshed 				/* no error handling for windows... */
51*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "ssd", "send failed: ", strerror(errno), fd);
52*76404edcSAsim Jamshed 
53*76404edcSAsim Jamshed 				return -1;
54*76404edcSAsim Jamshed 			}
55*76404edcSAsim Jamshed #else
56*76404edcSAsim Jamshed 			if ((r = write(fd, offset, toSend)) < 0) {
57*76404edcSAsim Jamshed 				switch (errno) {
58*76404edcSAsim Jamshed 				case EAGAIN:
59*76404edcSAsim Jamshed 				case EINTR:
60*76404edcSAsim Jamshed 					r = 0;
61*76404edcSAsim Jamshed 					break;
62*76404edcSAsim Jamshed 				case EPIPE:
63*76404edcSAsim Jamshed 				case ECONNRESET:
64*76404edcSAsim Jamshed 					return -2;
65*76404edcSAsim Jamshed 				default:
66*76404edcSAsim Jamshed 					log_error_write(srv, __FILE__, __LINE__, "ssd",
67*76404edcSAsim Jamshed 						"write failed:", strerror(errno), fd);
68*76404edcSAsim Jamshed 
69*76404edcSAsim Jamshed 					return -1;
70*76404edcSAsim Jamshed 				}
71*76404edcSAsim Jamshed 			}
72*76404edcSAsim Jamshed #endif
73*76404edcSAsim Jamshed 
74*76404edcSAsim Jamshed 			c->offset += r;
75*76404edcSAsim Jamshed 			cq->bytes_out += r;
76*76404edcSAsim Jamshed 			max_bytes -= r;
77*76404edcSAsim Jamshed 
78*76404edcSAsim Jamshed 			if (c->offset == (off_t)c->mem->used - 1) {
79*76404edcSAsim Jamshed 				chunk_finished = 1;
80*76404edcSAsim Jamshed 			}
81*76404edcSAsim Jamshed 
82*76404edcSAsim Jamshed 			break;
83*76404edcSAsim Jamshed 		}
84*76404edcSAsim Jamshed 		case FILE_CHUNK: {
85*76404edcSAsim Jamshed #ifdef USE_MMAP
86*76404edcSAsim Jamshed 			char *p = NULL;
87*76404edcSAsim Jamshed #endif
88*76404edcSAsim Jamshed 			ssize_t r;
89*76404edcSAsim Jamshed 			off_t offset;
90*76404edcSAsim Jamshed 			off_t toSend;
91*76404edcSAsim Jamshed 			stat_cache_entry *sce = NULL;
92*76404edcSAsim Jamshed 			int ifd;
93*76404edcSAsim Jamshed 
94*76404edcSAsim Jamshed 			if (HANDLER_ERROR == stat_cache_get_entry(srv, con, c->file.name, &sce)) {
95*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "sb",
96*76404edcSAsim Jamshed 						strerror(errno), c->file.name);
97*76404edcSAsim Jamshed 				return -1;
98*76404edcSAsim Jamshed 			}
99*76404edcSAsim Jamshed 
100*76404edcSAsim Jamshed 			offset = c->file.start + c->offset;
101*76404edcSAsim Jamshed 			toSend = c->file.length - c->offset;
102*76404edcSAsim Jamshed 
103*76404edcSAsim Jamshed 			if (toSend > max_bytes) toSend = max_bytes;
104*76404edcSAsim Jamshed 
105*76404edcSAsim Jamshed 			if (offset > sce->st.st_size) {
106*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "sb", "file was shrinked:", c->file.name);
107*76404edcSAsim Jamshed 
108*76404edcSAsim Jamshed 				return -1;
109*76404edcSAsim Jamshed 			}
110*76404edcSAsim Jamshed 
111*76404edcSAsim Jamshed 			if (-1 == (ifd = open(c->file.name->ptr, O_RDONLY))) {
112*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
113*76404edcSAsim Jamshed 
114*76404edcSAsim Jamshed 				return -1;
115*76404edcSAsim Jamshed 			}
116*76404edcSAsim Jamshed 
117*76404edcSAsim Jamshed #ifdef USE_MMAP
118*76404edcSAsim Jamshed 			if (MAP_FAILED == (p = mmap(0, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0))) {
119*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "ss", "mmap failed: ", strerror(errno));
120*76404edcSAsim Jamshed 
121*76404edcSAsim Jamshed 				close(ifd);
122*76404edcSAsim Jamshed 
123*76404edcSAsim Jamshed 				return -1;
124*76404edcSAsim Jamshed 			}
125*76404edcSAsim Jamshed 			close(ifd);
126*76404edcSAsim Jamshed 
127*76404edcSAsim Jamshed 			if ((r = write(fd, p + offset, toSend)) <= 0) {
128*76404edcSAsim Jamshed 				switch (errno) {
129*76404edcSAsim Jamshed 				case EAGAIN:
130*76404edcSAsim Jamshed 				case EINTR:
131*76404edcSAsim Jamshed 					r = 0;
132*76404edcSAsim Jamshed 					break;
133*76404edcSAsim Jamshed 				case EPIPE:
134*76404edcSAsim Jamshed 				case ECONNRESET:
135*76404edcSAsim Jamshed 					munmap(p, sce->st.st_size);
136*76404edcSAsim Jamshed 					return -2;
137*76404edcSAsim Jamshed 				default:
138*76404edcSAsim Jamshed 					log_error_write(srv, __FILE__, __LINE__, "ssd",
139*76404edcSAsim Jamshed 						"write failed:", strerror(errno), fd);
140*76404edcSAsim Jamshed 					munmap(p, sce->st.st_size);
141*76404edcSAsim Jamshed 
142*76404edcSAsim Jamshed 					return -1;
143*76404edcSAsim Jamshed 				}
144*76404edcSAsim Jamshed 			}
145*76404edcSAsim Jamshed 
146*76404edcSAsim Jamshed 			munmap(p, sce->st.st_size);
147*76404edcSAsim Jamshed #else /* USE_MMAP */
148*76404edcSAsim Jamshed 			buffer_prepare_copy(srv->tmp_buf, toSend);
149*76404edcSAsim Jamshed 
150*76404edcSAsim Jamshed 			lseek(ifd, offset, SEEK_SET);
151*76404edcSAsim Jamshed 			if (-1 == (toSend = read(ifd, srv->tmp_buf->ptr, toSend))) {
152*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "ss", "read: ", strerror(errno));
153*76404edcSAsim Jamshed 				close(ifd);
154*76404edcSAsim Jamshed 
155*76404edcSAsim Jamshed 				return -1;
156*76404edcSAsim Jamshed 			}
157*76404edcSAsim Jamshed 			close(ifd);
158*76404edcSAsim Jamshed 
159*76404edcSAsim Jamshed #ifdef __WIN32
160*76404edcSAsim Jamshed 			if ((r = send(fd, srv->tmp_buf->ptr, toSend, 0)) < 0) {
161*76404edcSAsim Jamshed 				/* no error handling for windows... */
162*76404edcSAsim Jamshed 				log_error_write(srv, __FILE__, __LINE__, "ssd", "send failed: ", strerror(errno), fd);
163*76404edcSAsim Jamshed 
164*76404edcSAsim Jamshed 				return -1;
165*76404edcSAsim Jamshed 			}
166*76404edcSAsim Jamshed #else /* __WIN32 */
167*76404edcSAsim Jamshed 			if ((r = write(fd, srv->tmp_buf->ptr, toSend)) < 0) {
168*76404edcSAsim Jamshed 				switch (errno) {
169*76404edcSAsim Jamshed 				case EAGAIN:
170*76404edcSAsim Jamshed 				case EINTR:
171*76404edcSAsim Jamshed 					r = 0;
172*76404edcSAsim Jamshed 					break;
173*76404edcSAsim Jamshed 				case EPIPE:
174*76404edcSAsim Jamshed 				case ECONNRESET:
175*76404edcSAsim Jamshed 					return -2;
176*76404edcSAsim Jamshed 				default:
177*76404edcSAsim Jamshed 					log_error_write(srv, __FILE__, __LINE__, "ssd",
178*76404edcSAsim Jamshed 						"write failed:", strerror(errno), fd);
179*76404edcSAsim Jamshed 
180*76404edcSAsim Jamshed 					return -1;
181*76404edcSAsim Jamshed 				}
182*76404edcSAsim Jamshed 			}
183*76404edcSAsim Jamshed #endif /* __WIN32 */
184*76404edcSAsim Jamshed #endif /* USE_MMAP */
185*76404edcSAsim Jamshed 
186*76404edcSAsim Jamshed 			c->offset += r;
187*76404edcSAsim Jamshed 			cq->bytes_out += r;
188*76404edcSAsim Jamshed 			max_bytes -= r;
189*76404edcSAsim Jamshed 
190*76404edcSAsim Jamshed 			if (c->offset == c->file.length) {
191*76404edcSAsim Jamshed 				chunk_finished = 1;
192*76404edcSAsim Jamshed 			}
193*76404edcSAsim Jamshed 
194*76404edcSAsim Jamshed 			break;
195*76404edcSAsim Jamshed 		}
196*76404edcSAsim Jamshed 		default:
197*76404edcSAsim Jamshed 
198*76404edcSAsim Jamshed 			log_error_write(srv, __FILE__, __LINE__, "ds", c, "type not known");
199*76404edcSAsim Jamshed 
200*76404edcSAsim Jamshed 			return -1;
201*76404edcSAsim Jamshed 		}
202*76404edcSAsim Jamshed 
203*76404edcSAsim Jamshed 		if (!chunk_finished) {
204*76404edcSAsim Jamshed 			/* not finished yet */
205*76404edcSAsim Jamshed 
206*76404edcSAsim Jamshed 			break;
207*76404edcSAsim Jamshed 		}
208*76404edcSAsim Jamshed 	}
209*76404edcSAsim Jamshed 
210*76404edcSAsim Jamshed 	return 0;
211*76404edcSAsim Jamshed }
212*76404edcSAsim Jamshed 
213*76404edcSAsim Jamshed #if 0
214*76404edcSAsim Jamshed network_write_init(void) {
215*76404edcSAsim Jamshed 	p->write = network_write_write_chunkset;
216*76404edcSAsim Jamshed }
217*76404edcSAsim Jamshed #endif
218