Wed, 28 Oct 2015 17:59:34 +0100
using readdir_r instead of readdir
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
1 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
29 | #ifdef __gnu_linux__ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
30 | #define _GNU_SOURCE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
31 | #endif |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
32 | |
1 | 33 | #include <unistd.h> |
34 | #include <stdlib.h> | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
35 | #include <sys/uio.h> |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
36 | #include <sys/uio.h> |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
37 | #ifndef BSD |
54 | 38 | #include <sys/sendfile.h> |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
39 | #endif |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
40 | #include <limits.h> /* asprintf */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
41 | |
54 | 42 | #include "../daemon/vfs.h" |
1 | 43 | #include "io.h" |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
44 | #include "pool.h" |
1 | 45 | |
46 | IOStream native_io_funcs = { | |
47 | system_write, | |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
48 | system_writev, |
54 | 49 | system_read, |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
50 | NULL, |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
51 | NULL, |
54 | 52 | NULL |
1 | 53 | }; |
54 | ||
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
55 | IOStream net_io_funcs = { |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
56 | (io_write_f)net_stream_write, |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
57 | (io_writev_f)net_stream_writev, |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
58 | (io_read_f)net_stream_read, |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
59 | (io_sendfile_f)net_stream_sendfile, |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
60 | (io_close_f)net_stream_close, |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
61 | (io_finish_f)net_stream_finish |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
62 | }; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
63 | |
1 | 64 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
65 | IOStream* stream_new_from_fd(pool_handle_t *pool, int fd) { |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
66 | SystemIOStream *st = pool_malloc(pool, sizeof(SystemIOStream)); |
1 | 67 | st->st = native_io_funcs; |
68 | st->fd = fd; | |
69 | return (IOStream*)st; | |
70 | } | |
71 | ||
72 | ssize_t system_write(IOStream *st, void *buf, size_t nbytes) { | |
73 | return write(((SystemIOStream*)st)->fd, buf, nbytes); | |
74 | } | |
75 | ||
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
76 | ssize_t system_writev(IOStream *st, struct iovec *iovec, int iovcnt) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
77 | return writev(((SystemIOStream*)st)->fd, iovec, iovcnt); |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
78 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
79 | |
1 | 80 | ssize_t system_read(IOStream *st, void *buf, size_t nbytes) { |
81 | return read(((SystemIOStream*)st)->fd, buf, nbytes); | |
82 | } | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
83 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
84 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
85 | IOStream* net_stream_from_fd(pool_handle_t *pool, int fd) { |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
86 | NetIOStream *st = pool_malloc(pool, sizeof(NetIOStream)); |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
87 | st->st = net_io_funcs; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
88 | st->fd = fd; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
89 | st->max_read = 0; |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
90 | st->read = 0; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
91 | st->chunked_enc = 0; |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
92 | st->buffered = 0; |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
93 | return (IOStream*)st; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
94 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
95 | |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
96 | ssize_t net_stream_write(NetIOStream *st, void *buf, size_t nbytes) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
97 | if(st->chunked_enc) { |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
98 | // TODO: on some plattforms iov_len is smaller than size_t |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
99 | struct iovec io[2]; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
100 | char chunk_len[16]; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
101 | io[0].iov_base = chunk_len; |
71 | 102 | io[0].iov_len = snprintf(chunk_len, 16, "\r\n%zx\r\n", nbytes); |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
103 | io[1].iov_base = buf; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
104 | io[1].iov_len = nbytes; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
105 | ssize_t r = writev(st->fd, io, 2); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
106 | return r - io[0].iov_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
107 | } else { |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
108 | return write(st->fd, buf, nbytes); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
109 | } |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
110 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
111 | |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
112 | ssize_t net_stream_writev(NetIOStream *st, struct iovec *iovec, int iovcnt) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
113 | if(st->chunked_enc) { |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
114 | struct iovec *io = calloc(iovcnt + 1, sizeof(struct iovec)); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
115 | char chunk_len[16]; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
116 | io[0].iov_base = chunk_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
117 | size_t len = 0; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
118 | for(int i=0;i<iovcnt;i++) { |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
119 | len += iovec[i].iov_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
120 | } |
71 | 121 | io[0].iov_len = snprintf(chunk_len, 16, "\r\n%zx\r\n", len); |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
122 | memcpy(io + 1, iovec, iovcnt * sizeof(struct iovec)); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
123 | ssize_t r = writev(st->fd, io, iovcnt + 1); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
124 | return r - io[0].iov_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
125 | } else { |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
126 | return writev(st->fd, iovec, iovcnt); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
127 | } |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
128 | } |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
129 | |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
130 | ssize_t net_stream_read(NetIOStream *st, void *buf, size_t nbytes) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
131 | if(st->max_read != 0 && st->read >= st->max_read) { |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
132 | return 0; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
133 | } |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
134 | ssize_t r = read(st->fd, buf, nbytes); |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
135 | st->read += r; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
136 | return r; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
137 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
138 | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
139 | ssize_t net_stream_sendfile(NetIOStream *st, sendfiledata *sfd) { |
54 | 140 | ssize_t ret = 0; |
141 | off_t fileoffset = sfd->offset; | |
142 | if(sfd->fd->fd != -1) { | |
71 | 143 | #ifdef BSD |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
144 | struct iovec hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
145 | hdvec.iov_base = (void*)sfd->header; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
146 | hdvec.iov_len = sfd->hlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
147 | struct iovec trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
148 | trvec.iov_base = (void*)sfd->trailer; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
149 | trvec.iov_len = sfd->tlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
150 | struct sf_hdtr hdtr; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
151 | hdtr.headers = &hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
152 | hdtr.hdr_cnt = 1; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
153 | hdtr.trailers = &trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
154 | hdtr.trl_cnt = 1; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
155 | |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
156 | off_t len = sfd->len; |
71 | 157 | #ifdef OSX |
158 | ret = sendfile(sfd->fd->fd, st->fd, fileoffset, &len, &hdtr, 0); | |
159 | #else // BSD | |
160 | ret = sendfile( | |
161 | sfd->fd->fd, | |
162 | st->fd, | |
163 | fileoffset, | |
164 | sfd->len, | |
165 | &hdtr, | |
166 | NULL, | |
167 | 0); | |
168 | #endif | |
169 | #else // Solaris/Linux | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
170 | if(sfd->header) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
171 | ret += write(st->fd, sfd->header, sfd->hlen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
172 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
173 | ret += sendfile(st->fd, sfd->fd->fd, &fileoffset, sfd->len); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
174 | if(sfd->trailer) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
175 | ret += write(st->fd, sfd->trailer, sfd->tlen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
176 | } |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
177 | #endif |
54 | 178 | } else { |
179 | // TODO: regular copy | |
180 | fprintf(stderr, "sendfile not implemented for SYS_FILE\n"); | |
181 | } | |
182 | ||
183 | return ret; | |
184 | } | |
185 | ||
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
186 | void net_stream_close(NetIOStream *st) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
187 | close(st->fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
188 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
189 | |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
190 | void net_stream_finish(NetIOStream *st) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
191 | if(st->chunked_enc) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
192 | write(st->fd, "0\r\n\r\n", 5); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
193 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
194 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
195 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
196 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
197 | ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
198 | ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
199 | if(r == 0) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
200 | return IO_EOF; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
201 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
202 | return r; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
203 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
204 | |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
205 | ssize_t net_write(SYS_NETFD fd, void *buf, size_t nbytes) { |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
206 | ssize_t r = ((IOStream*)fd)->write(fd, buf, nbytes); |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
207 | if(r < 0) { |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
208 | return IO_ERROR; |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
209 | } |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
210 | return r; |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
211 | } |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
212 | |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
213 | ssize_t net_writev(SYS_NETFD fd, struct iovec *iovec, int iovcnt) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
214 | ssize_t r = ((IOStream*)fd)->writev(fd, iovec, iovcnt); |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
215 | if(r < 0) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
216 | return IO_ERROR; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
217 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
218 | return r; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
219 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
220 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
221 | ssize_t net_printf(SYS_NETFD fd, char *format, ...) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
222 | va_list arg; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
223 | va_start(arg, format); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
224 | char *buf; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
225 | ssize_t len = vasprintf(&buf, format, arg); |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
226 | ssize_t r = net_write(fd, buf, len); |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
227 | free(buf); |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
228 | return r; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
229 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
230 | |
54 | 231 | ssize_t net_sendfile(SYS_NETFD fd, sendfiledata *sfd) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
232 | NetIOStream *out = fd; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
233 | if(out->st.sendfile && sfd->fd && sfd->fd->fd != -1) { |
54 | 234 | ssize_t r = ((IOStream*)fd)->sendfile(fd, sfd); |
235 | if(r < 0) { | |
236 | return IO_ERROR; | |
237 | } | |
238 | } else { | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
239 | // stream/file does not support sendfile |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
240 | // do regular copy |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
241 | char *buf = malloc(4096); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
242 | if(!buf) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
243 | // TODO: out of memory error |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
244 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
245 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
246 | char *header = (char*)sfd->header; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
247 | int hlen = sfd->hlen; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
248 | char *trailer = (char*)sfd->trailer; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
249 | int tlen = sfd->tlen; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
250 | if(header == NULL) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
251 | hlen = 0; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
252 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
253 | if(trailer == NULL) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
254 | tlen = 0; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
255 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
256 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
257 | ssize_t r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
258 | while(hlen > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
259 | r = out->st.write(fd, header, hlen); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
260 | header += r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
261 | hlen -= r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
262 | if(r <= 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
263 | free(buf); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
264 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
265 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
266 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
267 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
268 | if(system_lseek(sfd->fd, sfd->offset, SEEK_SET) == -1) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
269 | free(buf); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
270 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
271 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
272 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
273 | size_t length = sfd->len; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
274 | while(length > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
275 | if((r = system_fread(sfd->fd, buf, 4096)) <= 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
276 | break; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
277 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
278 | char *write_buf = buf; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
279 | while(r > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
280 | ssize_t w = out->st.write(fd, write_buf, r); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
281 | r -= w; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
282 | length -= w; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
283 | write_buf += w; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
284 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
285 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
286 | free(buf); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
287 | if(length > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
288 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
289 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
290 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
291 | while(tlen > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
292 | r = out->st.write(fd, trailer, tlen); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
293 | trailer += r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
294 | tlen -= r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
295 | if(r <= 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
296 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
297 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
298 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
299 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
300 | return sfd->hlen + sfd->len + sfd->tlen; |
54 | 301 | } |
302 | return IO_ERROR; | |
303 | } | |
304 | ||
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
305 | int net_flush(SYS_NETFD sd) { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
306 | // TODO: implement |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
307 | return 0; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
308 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
309 | |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
310 | void net_close(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
311 | ((IOStream*)fd)->close(fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
312 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
313 | |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
314 | void net_finish(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
315 | ((IOStream*)fd)->finish(fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
316 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
317 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
318 | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
319 | /* iovec buffer */ |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
320 | iovec_buf_t *iovec_buf_create(pool_handle_t *pool) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
321 | iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t)); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
322 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
323 | buf->pool = pool; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
324 | buf->iov = pool_calloc(pool, 32, sizeof(struct iovec)); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
325 | buf->maxiovec = 32; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
326 | buf->iovctn = 0; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
327 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
328 | return buf; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
329 | } |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
330 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
331 | void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
332 | if(io->iovctn >= io->maxiovec) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
333 | io->iov = pool_realloc( |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
334 | io->pool, |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
335 | io->iov, |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
336 | (io->maxiovec + 16) * sizeof(struct iovec)); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
337 | } |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
338 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
339 | io->iov[io->iovctn].iov_base = buf; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
340 | io->iov[io->iovctn].iov_len = nbyte; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
341 | io->iovctn++; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
342 | } |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
343 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
344 | ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
345 | return writev(fd, io->iov, io->iovctn); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
346 | } |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
347 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
348 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
349 | /* TODO: add asprintf to new file */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
350 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
351 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
352 | * asprintf implementation for Solaris 10 |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
353 | * source from OpenSolaris |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
354 | * file: /onnv/onnv-gate/usr/src/lib/libc/port/print/asprintf.c |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
355 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
356 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
357 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
358 | * Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
359 | * Use is subject to license terms. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
360 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
361 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
362 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
363 | * Copyright (c) 2004 Darren Tucker. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
364 | * |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
365 | * Based originally on asprintf.c from OpenBSD: |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
366 | * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
367 | * |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
368 | * Permission to use, copy, modify, and distribute this software for any |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
369 | * purpose with or without fee is hereby granted, provided that the above |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
370 | * copyright notice and this permission notice appear in all copies. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
371 | * |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
372 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
373 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
374 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
375 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
376 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
377 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
378 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
379 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
380 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
381 | #ifdef __SunOS_5_10 |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
382 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
383 | #define INIT_SZ 128 |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
384 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
385 | /* VARARGS2 */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
386 | int |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
387 | vasprintf(char **str, const char *format, va_list ap) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
388 | { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
389 | char string[INIT_SZ]; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
390 | char *newstr; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
391 | int ret; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
392 | size_t len; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
393 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
394 | *str = NULL; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
395 | ret = vsnprintf(string, INIT_SZ, format, ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
396 | if (ret < 0) /* retain the value of errno from vsnprintf() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
397 | return (-1); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
398 | if (ret < INIT_SZ) { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
399 | len = ret + 1; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
400 | if ((newstr = malloc(len)) == NULL) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
401 | return (-1); /* retain errno from malloc() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
402 | (void) strlcpy(newstr, string, len); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
403 | *str = newstr; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
404 | return (ret); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
405 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
406 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
407 | * We will perform this loop more than once only if some other |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
408 | * thread modifies one of the vasprintf() arguments after our |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
409 | * previous call to vsnprintf(). |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
410 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
411 | for (;;) { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
412 | if (ret == INT_MAX) { /* Bad length */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
413 | errno = ENOMEM; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
414 | return (-1); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
415 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
416 | len = ret + 1; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
417 | if ((newstr = malloc(len)) == NULL) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
418 | return (-1); /* retain errno from malloc() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
419 | ret = vsnprintf(newstr, len, format, ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
420 | if (ret < 0) { /* retain errno from vsnprintf() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
421 | free(newstr); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
422 | return (-1); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
423 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
424 | if (ret < len) { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
425 | *str = newstr; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
426 | return (ret); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
427 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
428 | free(newstr); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
429 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
430 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
431 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
432 | int |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
433 | asprintf(char **str, const char *format, ...) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
434 | { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
435 | va_list ap; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
436 | int ret; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
437 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
438 | *str = NULL; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
439 | va_start(ap, format); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
440 | ret = vasprintf(str, format, ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
441 | va_end(ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
442 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
443 | return (ret); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
444 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
445 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
446 | #endif |