]>
git.proxmox.com Git - mirror_frr.git/blob - lib/imsg-buffer.c
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 int ibuf_realloc(struct ibuf
*, size_t);
25 void ibuf_enqueue(struct msgbuf
*, struct ibuf
*);
26 void ibuf_dequeue(struct msgbuf
*, struct ibuf
*);
33 if ((buf
= calloc(1, sizeof(struct ibuf
))) == NULL
)
35 if ((buf
->buf
= malloc(len
)) == NULL
) {
39 buf
->size
= buf
->max
= len
;
46 ibuf_dynamic(size_t len
, size_t max
)
53 if ((buf
= ibuf_open(len
)) == NULL
)
63 ibuf_realloc(struct ibuf
*buf
, size_t len
)
67 /* on static buffers max is eq size and so the following fails */
68 if (buf
->wpos
+ len
> buf
->max
) {
73 b
= realloc(buf
->buf
, buf
->wpos
+ len
);
77 buf
->size
= buf
->wpos
+ len
;
83 ibuf_add(struct ibuf
*buf
, const void *data
, size_t len
)
85 if (buf
->wpos
+ len
> buf
->size
)
86 if (ibuf_realloc(buf
, len
) == -1)
89 memcpy(buf
->buf
+ buf
->wpos
, data
, len
);
95 ibuf_reserve(struct ibuf
*buf
, size_t len
)
99 if (buf
->wpos
+ len
> buf
->size
)
100 if (ibuf_realloc(buf
, len
) == -1)
103 b
= buf
->buf
+ buf
->wpos
;
109 ibuf_seek(struct ibuf
*buf
, size_t pos
, size_t len
)
111 /* only allowed to seek in already written parts */
112 if (pos
+ len
> buf
->wpos
)
115 return (buf
->buf
+ pos
);
119 ibuf_size(struct ibuf
*buf
)
125 ibuf_left(struct ibuf
*buf
)
127 return (buf
->max
- buf
->wpos
);
131 ibuf_close(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
133 ibuf_enqueue(msgbuf
, buf
);
137 ibuf_write(struct msgbuf
*msgbuf
)
139 struct iovec iov
[IOV_MAX
];
144 memset(&iov
, 0, sizeof(iov
));
145 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
148 iov
[i
].iov_base
= buf
->buf
+ buf
->rpos
;
149 iov
[i
].iov_len
= buf
->wpos
- buf
->rpos
;
154 if ((n
= writev(msgbuf
->fd
, iov
, i
)) == -1) {
157 if (errno
== ENOBUFS
)
162 if (n
== 0) { /* connection closed */
167 msgbuf_drain(msgbuf
, n
);
173 ibuf_free(struct ibuf
*buf
)
182 msgbuf_init(struct msgbuf
*msgbuf
)
186 TAILQ_INIT(&msgbuf
->bufs
);
190 msgbuf_drain(struct msgbuf
*msgbuf
, size_t n
)
192 struct ibuf
*buf
, *next
;
194 for (buf
= TAILQ_FIRST(&msgbuf
->bufs
); buf
!= NULL
&& n
> 0;
196 next
= TAILQ_NEXT(buf
, entry
);
197 if (buf
->rpos
+ n
>= buf
->wpos
) {
198 n
-= buf
->wpos
- buf
->rpos
;
199 ibuf_dequeue(msgbuf
, buf
);
208 msgbuf_clear(struct msgbuf
*msgbuf
)
212 while ((buf
= TAILQ_FIRST(&msgbuf
->bufs
)) != NULL
)
213 ibuf_dequeue(msgbuf
, buf
);
217 msgbuf_write(struct msgbuf
*msgbuf
)
219 struct iovec iov
[IOV_MAX
];
224 struct cmsghdr
*cmsg
;
227 char buf
[CMSG_SPACE(sizeof(int))];
230 memset(&iov
, 0, sizeof(iov
));
231 memset(&msg
, 0, sizeof(msg
));
232 memset(&cmsgbuf
, 0, sizeof(cmsgbuf
));
233 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
236 iov
[i
].iov_base
= buf
->buf
+ buf
->rpos
;
237 iov
[i
].iov_len
= buf
->wpos
- buf
->rpos
;
246 if (buf
!= NULL
&& buf
->fd
!= -1) {
247 msg
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
248 msg
.msg_controllen
= sizeof(cmsgbuf
.buf
);
249 cmsg
= CMSG_FIRSTHDR(&msg
);
250 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
251 cmsg
->cmsg_level
= SOL_SOCKET
;
252 cmsg
->cmsg_type
= SCM_RIGHTS
;
253 memcpy(CMSG_DATA(cmsg
), &buf
->fd
, sizeof(int));
257 if ((n
= sendmsg(msgbuf
->fd
, &msg
, 0)) == -1) {
260 if (errno
== ENOBUFS
)
265 if (n
== 0) { /* connection closed */
271 * assumption: fd got sent if sendmsg sent anything
272 * this works because fds are passed one at a time
274 if (buf
!= NULL
&& buf
->fd
!= -1) {
279 msgbuf_drain(msgbuf
, n
);
285 ibuf_enqueue(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
287 TAILQ_INSERT_TAIL(&msgbuf
->bufs
, buf
, entry
);
292 ibuf_dequeue(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
294 TAILQ_REMOVE(&msgbuf
->bufs
, buf
, entry
);