Coverage Report

Created: 2026-11-12 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openiked-portable/iked/imsg_util.c
Line
Count
Source
1
/*  $OpenBSD: imsg_util.c,v 1.22 2023/12/12 15:52:58 claudio Exp $  */
2
3
/*
4
 * Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org>
5
 *
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.
9
 *
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.
17
 */
18
19
#include <sys/queue.h>
20
#include <sys/socket.h>
21
#include <sys/uio.h>
22
23
#include <netdb.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <unistd.h>
27
#include <string.h>
28
#include <errno.h>
29
#include <fcntl.h>
30
#include <ctype.h>
31
#include <event.h>
32
33
#include "iked.h"
34
35
/*
36
 * Extending the imsg buffer API for internal use
37
 */
38
39
struct ibuf *
40
ibuf_new(const void *data, size_t len)
41
7.57k
{
42
7.57k
  struct ibuf *buf;
43
44
7.57k
  if ((buf = ibuf_dynamic(len,
45
7.57k
      IKED_MSGBUF_MAX)) == NULL)
46
0
    return (NULL);
47
48
7.57k
  if (len == 0)
49
2.51k
    return (buf);
50
51
5.06k
  if (data == NULL) {
52
20
    if (ibuf_add_zero(buf, len) != 0) {
53
0
      ibuf_free(buf);
54
0
      return (NULL);
55
0
    }
56
5.04k
  } else {
57
5.04k
    if (ibuf_add(buf, data, len) != 0) {
58
0
      ibuf_free(buf);
59
0
      return (NULL);
60
0
    }
61
5.04k
  }
62
63
5.06k
  return (buf);
64
5.06k
}
65
66
struct ibuf *
67
ibuf_static(void)
68
0
{
69
0
  return ibuf_open(IKED_MSGBUF_MAX);
70
0
}
71
72
size_t
73
ibuf_length(struct ibuf *buf)
74
0
{
75
0
  if (buf == NULL)
76
0
    return (0);
77
0
  return (ibuf_size(buf));
78
0
}
79
80
struct ibuf *
81
ibuf_getdata(struct ibuf *buf, size_t len)
82
0
{
83
0
  struct ibuf tmp;
84
85
0
  if (ibuf_get_ibuf(buf, len, &tmp) == -1)
86
0
    return (NULL);
87
88
0
  return (ibuf_new(ibuf_data(&tmp), ibuf_size(&tmp)));
89
0
}
90
91
struct ibuf *
92
ibuf_dup(struct ibuf *buf)
93
0
{
94
0
  if (buf == NULL)
95
0
    return (NULL);
96
0
  return (ibuf_new(ibuf_data(buf), ibuf_size(buf)));
97
0
}
98
99
struct ibuf *
100
ibuf_random(size_t len)
101
0
{
102
0
  struct ibuf *buf;
103
0
  void    *ptr;
104
105
0
  if ((buf = ibuf_open(len)) == NULL)
106
0
    return (NULL);
107
0
  if ((ptr = ibuf_reserve(buf, len)) == NULL) {
108
0
    ibuf_free(buf);
109
0
    return (NULL);
110
0
  }
111
0
  arc4random_buf(ptr, len);
112
0
  return (buf);
113
0
}
114
115
int
116
ibuf_setsize(struct ibuf *buf, size_t len)
117
0
{
118
0
  if (len > buf->size)
119
0
    return (-1);
120
0
  buf->wpos = len;
121
0
  return (0);
122
0
}