summaryrefslogtreecommitdiff
path: root/src/network_backends.h
blob: db03a589ec02f7131b2fbdf874c19fd04c85e4c2 (plain)
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
#ifndef _NETWORK_BACKENDS_H_
#define _NETWORK_BACKENDS_H_
#include "first.h"

#include "settings.h"

#include <sys/types.h>

/* on linux 2.4.x you get either sendfile or LFS */
#if defined HAVE_SYS_SENDFILE_H && defined HAVE_SENDFILE && (!defined _LARGEFILE_SOURCE || defined HAVE_SENDFILE64) && defined(__linux__) && !defined HAVE_SENDFILE_BROKEN
# ifdef USE_SENDFILE
#  error "can't have more than one sendfile implementation"
# endif
# define USE_SENDFILE "linux-sendfile"
# define USE_LINUX_SENDFILE
#endif

#if defined HAVE_SENDFILE && (defined(__FreeBSD__) || defined(__DragonFly__))
# ifdef USE_SENDFILE
#  error "can't have more than one sendfile implementation"
# endif
# define USE_SENDFILE "freebsd-sendfile"
# define USE_FREEBSD_SENDFILE
#endif

#if defined HAVE_SENDFILE && defined(__APPLE__)
# ifdef USE_SENDFILE
#  error "can't have more than one sendfile implementation"
# endif
# define USE_SENDFILE "darwin-sendfile"
# define USE_DARWIN_SENDFILE
#endif

#if defined HAVE_SYS_SENDFILE_H && defined HAVE_SENDFILEV && defined(__sun)
# ifdef USE_SENDFILE
#  error "can't have more than one sendfile implementation"
# endif
# define USE_SENDFILE "solaris-sendfilev"
# define USE_SOLARIS_SENDFILEV
#endif

/* not supported so far
#if defined HAVE_SEND_FILE && defined(__aix)
# ifdef USE_SENDFILE
#  error "can't have more than one sendfile implementation"
# endif
# define USE_SENDFILE "aix-sendfile"
# define USE_AIX_SENDFILE
#endif
*/

#if defined HAVE_SYS_UIO_H && defined HAVE_WRITEV
# define USE_WRITEV
#endif

#if defined HAVE_SYS_MMAN_H && defined HAVE_MMAP && defined ENABLE_MMAP
# define USE_MMAP
#endif

#include "base.h"

/* return values:
 * >= 0 : no error
 *   -1 : error (on our side)
 *   -2 : remote close
 */

int network_write_chunkqueue_write(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes);

#if defined(USE_WRITEV)
int network_write_chunkqueue_writev(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes); /* fallback to write */
#endif

#if defined(USE_SENDFILE)
int network_write_chunkqueue_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes); /* fallback to write */
#endif

/* write next chunk(s); finished chunks are removed afterwards after successful writes.
 * return values: similar as backends (0 succes, -1 error, -2 remote close, -3 try again later (EINTR/EAGAIN)) */
/* next chunk must be MEM_CHUNK. use write()/send() */
int network_write_mem_chunk(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes);

#if defined(USE_WRITEV)
/* next chunk must be MEM_CHUNK. send multiple mem chunks using writev() */
int network_writev_mem_chunks(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes);
#else
/* fallback to write()/send() */
static inline int network_writev_mem_chunks(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
	return network_write_mem_chunk(srv, con, fd, cq, p_max_bytes);
}
#endif

/* next chunk must be FILE_CHUNK. use temporary buffer (srv->tmp_buf) to read into, then write()/send() it */
int network_write_file_chunk_no_mmap(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes);

off_t mmap_align_offset(off_t start);
#if defined(USE_MMAP)
/* next chunk must be FILE_CHUNK. send mmap()ed file with write() */
int network_write_file_chunk_mmap(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes);
#else
/* fallback to no_mmap */
static inline int network_write_file_chunk_mmap(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
	return network_write_file_chunk_no_mmap(srv, con, fd, cq, p_max_bytes);
}
#endif

#if defined(USE_SENDFILE)
int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes);
#else
/* fallback to mmap */
static inline int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
	return network_write_file_chunk_mmap(srv, con, fd, cq, p_max_bytes);
}
#endif

/* next chunk must be FILE_CHUNK. return values: 0 success (=> -1 != cq->first->file.fd), -1 error */
int network_open_file_chunk(server *srv, connection *con, chunkqueue *cq);

#endif