Minor fixes
[MonkeyD.git] / src / iov.c
blobb67f841ef1c9b79c1de2d03efd128af9641e8a12
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2008, Eduardo Silva P.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE
23 #include <fcntl.h>
25 #include <sys/uio.h>
26 #include <sys/mman.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <limits.h>
31 #include "monkey.h"
33 #include "header.h"
34 #include "memory.h"
35 #include "iov.h"
37 struct mk_iov *mk_iov_create(int n, int offset)
39 struct mk_iov *iov;
41 iov = mk_mem_malloc(sizeof(struct mk_iov));
42 iov->iov_idx = offset;
43 iov->io = mk_mem_malloc_z(n * sizeof(struct iovec));
44 iov->buf_to_free = mk_mem_malloc(n * sizeof(char *));
45 iov->buf_idx = 0;
46 iov->total_len = 0;
47 iov->size = n;
49 return iov;
52 int mk_iov_add_entry(struct mk_iov *mk_io, char *buf, int len,
53 mk_pointer sep, int free)
55 if (buf) {
56 mk_io->io[mk_io->iov_idx].iov_base = (unsigned char *) buf;
57 mk_io->io[mk_io->iov_idx].iov_len = len;
58 mk_io->iov_idx++;
59 mk_io->total_len += len;
62 #ifdef DEBUG_IOV
63 if (mk_io->iov_idx > mk_io->size) {
64 printf("\nDEBUG IOV :: ERROR, Broke array size in:");
65 printf("\n '''%s'''", buf);
66 fflush(stdout);
68 #endif
70 /* Add separator */
71 if (sep.len > 0) {
72 mk_io->io[mk_io->iov_idx].iov_base = sep.data;
73 mk_io->io[mk_io->iov_idx].iov_len = sep.len;
74 mk_io->iov_idx++;
75 mk_io->total_len += sep.len;
78 if (free == MK_IOV_FREE_BUF) {
79 _mk_iov_set_free(mk_io, buf);
82 return mk_io->iov_idx;
85 int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
86 int free, int idx)
88 mk_io->io[idx].iov_base = buf;
89 mk_io->io[idx].iov_len = len;
90 mk_io->total_len += len;
92 if (free == MK_IOV_FREE_BUF) {
93 _mk_iov_set_free(mk_io, buf);
96 return 0;
99 void _mk_iov_set_free(struct mk_iov *mk_io, char *buf)
101 mk_io->buf_to_free[mk_io->buf_idx] = (char *) buf;
102 mk_io->buf_idx++;
105 ssize_t mk_iov_send(int fd, struct mk_iov *mk_io, int to)
107 ssize_t n = -1;
109 if (to == MK_IOV_SEND_TO_SOCKET) {
110 n = writev(fd, mk_io->io, mk_io->iov_idx);
112 if (n < 0) {
114 perror("writev");
115 switch(errno){
116 case EBADF:
117 printf("\nEBADF");
118 break;
119 case EFAULT:
120 printf("\nEFAULT");
121 break;
122 case EFBIG:
123 printf("\nEFBIG");
124 break;
125 case EINTR:
126 printf("\nEINTR");
127 break;
128 case EINVAL:
129 printf("\nEINVAL");
130 break;
131 case EIO:
132 printf("\nEIO");
133 break;
134 case ENOMEM:
135 printf("\nENOMEM");
136 break;
137 case ENOSPC:
138 printf("\nENOSPC");
139 break;
140 case ENXIO:
141 printf("\nENXIO");
142 break;
145 fflush(stdout);
147 return n;
150 else if (to == MK_IOV_SEND_TO_PIPE) {
151 /* for some reason, vmsplice is not working as expected for us,
152 * maybe we need to fix something here, at the moment
153 * we will keep using writev to push the iovec struct to the pipe
156 * n = vmsplice(fd,
157 * (const struct iovec *) mk_io->io,
158 * mk_io->iov_idx,
159 * SPLICE_F_GIFT);
160 * return n;
163 n = writev(fd, mk_io->io, mk_io->iov_idx);
165 if (n < 0) {
166 perror("writev");
170 return n;
173 void mk_iov_free(struct mk_iov *mk_io)
175 mk_iov_free_marked(mk_io);
176 mk_mem_free(mk_io->buf_to_free);
177 mk_mem_free(mk_io->io);
178 mk_mem_free(mk_io);
181 void mk_iov_free_marked(struct mk_iov *mk_io)
183 int i, limit = 0;
185 limit = mk_io->buf_idx;
187 for (i = 0; i < limit; i++) {
189 #ifdef DEBUG_IOV
190 printf("\nDEBUG IOV :: going free (idx: %i/%i): %s", i,
191 limit, mk_io->buf_to_free[i]);
192 fflush(stdout);
193 #endif
194 mk_mem_free(mk_io->buf_to_free[i]);
197 mk_io->iov_idx = 0;
198 mk_io->buf_idx = 0;
201 void mk_iov_print(struct mk_iov *mk_io)
203 int i;
205 for (i = 0; i < mk_io->iov_idx; i++) {
206 printf("\n%i len=%i)\n'%s'", i, mk_io->io[i].iov_len,
207 (char *) mk_io->io[i].iov_base);
208 fflush(stdout);
212 void mk_iov_separators_init()
214 mk_pointer_set(&mk_iov_crlf, MK_IOV_CRLF);
215 mk_pointer_set(&mk_iov_lf, MK_IOV_LF);
216 mk_pointer_set(&mk_iov_space, MK_IOV_SPACE);
217 mk_pointer_set(&mk_iov_header_value, MK_IOV_HEADER_VALUE);
218 mk_pointer_set(&mk_iov_slash, MK_IOV_SLASH);
219 mk_pointer_set(&mk_iov_none, MK_IOV_NONE);
220 mk_pointer_set(&mk_iov_equal, MK_IOV_EQUAL);