Change the FSF address and update LICENSE with the new address and some texts
[MonkeyD.git] / src / iov.c
blob6a72e7ed89a6de83e97394b70c600e526c35400a
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
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);
111 if (n < 0) {
113 perror("writev");
114 switch(errno){
115 case EBADF:
116 printf("\nEBADF");
117 break;
118 case EFAULT:
119 printf("\nEFAULT");
120 break;
121 case EFBIG:
122 printf("\nEFBIG");
123 break;
124 case EINTR:
125 printf("\nEINTR");
126 break;
127 case EINVAL:
128 printf("\nEINVAL");
129 break;
130 case EIO:
131 printf("\nEIO");
132 break;
133 case ENOMEM:
134 printf("\nENOMEM");
135 break;
136 case ENOSPC:
137 printf("\nENOSPC");
138 break;
139 case ENXIO:
140 printf("\nENXIO");
141 break;
144 fflush(stdout);
146 return n;
149 else if (to == MK_IOV_SEND_TO_PIPE) {
150 /* for some reason, vmsplice is not working as expected for us,
151 * maybe we need to fix something here, at the moment
152 * we will keep using writev to push the iovec struct to the pipe
155 * n = vmsplice(fd,
156 * (const struct iovec *) mk_io->io,
157 * mk_io->iov_idx,
158 * SPLICE_F_GIFT);
159 * return n;
162 n = writev(fd, mk_io->io, mk_io->iov_idx);
164 if (n < 0) {
165 perror("writev");
169 return n;
172 void mk_iov_free(struct mk_iov *mk_io)
174 mk_iov_free_marked(mk_io);
175 mk_mem_free(mk_io->buf_to_free);
176 mk_mem_free(mk_io->io);
177 mk_mem_free(mk_io);
180 void mk_iov_free_marked(struct mk_iov *mk_io)
182 int i, limit = 0;
184 limit = mk_io->buf_idx;
186 for (i = 0; i < limit; i++) {
188 #ifdef DEBUG_IOV
189 printf("\nDEBUG IOV :: going free (idx: %i/%i): %s", i,
190 limit, mk_io->buf_to_free[i]);
191 fflush(stdout);
192 #endif
193 mk_mem_free(mk_io->buf_to_free[i]);
196 mk_io->iov_idx = 0;
197 mk_io->buf_idx = 0;
200 void mk_iov_print(struct mk_iov *mk_io)
202 int i;
204 for (i = 0; i < mk_io->iov_idx; i++) {
205 printf("\n%i len=%i)\n'%s'", i, mk_io->io[i].iov_len,
206 (char *) mk_io->io[i].iov_base);
207 fflush(stdout);
211 void mk_iov_separators_init()
213 mk_pointer_set(&mk_iov_crlf, MK_IOV_CRLF);
214 mk_pointer_set(&mk_iov_lf, MK_IOV_LF);
215 mk_pointer_set(&mk_iov_space, MK_IOV_SPACE);
216 mk_pointer_set(&mk_iov_header_value, MK_IOV_HEADER_VALUE);
217 mk_pointer_set(&mk_iov_slash, MK_IOV_SLASH);
218 mk_pointer_set(&mk_iov_none, MK_IOV_NONE);
219 mk_pointer_set(&mk_iov_equal, MK_IOV_EQUAL);