Coverage Report

Created: 2025-07-11 06:28

/src/opensips/context.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2014 OpenSIPS Solutions
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 *
20
 * History:
21
 * --------
22
 *  2014-10-30 initial version (liviu)
23
 */
24
25
/*
26
 * This header exposes the basic operations with an OpenSIPS context.
27
 *
28
 * A "context" is:
29
 *    - a data storage buffer
30
 *    - typically allocated next to the intended structure
31
 *      e.g. | struct cell | CONTEXT_BUFFER |
32
 *
33
 * !! All data registrations must be done in the pre-forking phases !!
34
 *    (see the register functions below)
35
 */
36
37
#ifndef __CONTEXT_H
38
#define __CONTEXT_H
39
40
#include <stdlib.h>
41
42
#include "dprint.h"
43
44
typedef void * context_p;
45
enum osips_context {
46
  CONTEXT_GLOBAL,
47
  CONTEXT_TRAN,
48
  CONTEXT_DIALOG,
49
  CONTEXT_B2B_LOGIC,
50
51
  CONTEXT_COUNT,
52
};
53
54
enum osips_context_val {
55
  CONTEXT_INT_TYPE,
56
  CONTEXT_STR_TYPE,
57
  CONTEXT_PTR_TYPE,
58
59
  CONTEXT_COUNT_TYPE
60
};
61
62
#define context_of(entity_p) ((context_p)((entity_p) + 1))
63
2
#define context_size(enum_ctx) (context_sizes[enum_ctx])
64
65
extern context_p current_processing_ctx;
66
0
#define set_global_context(__ctx) current_processing_ctx = __ctx
67
68
extern unsigned int context_sizes[];
69
extern unsigned int type_sizes[CONTEXT_COUNT][CONTEXT_COUNT_TYPE];
70
extern unsigned int type_offsets[CONTEXT_COUNT][CONTEXT_COUNT_TYPE];
71
72
/*
73
 * allocate a new context in pkg mem
74
 *
75
 * Note: this will not change the "current_processing_ctx"
76
 */
77
context_p context_alloc(enum osips_context type);
78
0
#define   context_free(context_p) pkg_free(context_p)
79
80
81
/*
82
 * ensure the global context is non-NULL
83
 *
84
 * Return: context pointer on success, NULL on internal error
85
 */
86
int ensure_global_context(void);
87
88
89
/* pushes or pops a context level from internal stack
90
 */
91
int push_new_global_context(void);
92
int pop_pushed_global_context(void);
93
94
95
/* free and reset the global context */
96
void clear_global_context(void);
97
98
99
/*
100
 * destroys a context by calling each callback registered
101
 * Note: @ctx will *not* be freed!
102
 */
103
void context_destroy(enum osips_context type, context_p ctx);
104
105
106
/*
107
 * - register a different function for each field you add in the context
108
 * - each function will be called exactly once, every time a context is freed
109
 *
110
 * Note: for int and (str *) types, you must perform the appropriate casting
111
 */
112
typedef void (*context_destroy_f)(void *);
113
114
/*
115
 * - the register functions should be called before any forks are made
116
 *    (mod_init(), function fixups)
117
 *
118
 * - they reserve and return a position in the context buffer of the given type
119
 */
120
int context_register_int(enum osips_context type, context_destroy_f f);
121
int context_register_str(enum osips_context type, context_destroy_f f);
122
int context_register_ptr(enum osips_context type, context_destroy_f f);
123
124
   /****************************** SETTERS ********************************/
125
126
static inline void context_put_int(enum osips_context type, context_p ctx,
127
                   int pos, int data)
128
0
{
129
#ifdef DBG_MALLOC
130
  if (pos < 0 || pos >= type_sizes[type][CONTEXT_INT_TYPE]) {
131
    LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_INT_TYPE]);
132
    abort();
133
  }
134
#endif
135
136
0
  ((int *)ctx)[pos] = data;
137
0
}
Unexecuted instantiation: fuzz_uri_parser.c:context_put_int
Unexecuted instantiation: context.c:context_put_int
Unexecuted instantiation: dset.c:context_put_int
Unexecuted instantiation: action.c:context_put_int
Unexecuted instantiation: proto_tcp.c:context_put_int
Unexecuted instantiation: proto_udp.c:context_put_int
Unexecuted instantiation: receive.c:context_put_int
Unexecuted instantiation: forward.c:context_put_int
Unexecuted instantiation: blacklists.c:context_put_int
Unexecuted instantiation: msg_translator.c:context_put_int
Unexecuted instantiation: core_cmds.c:context_put_int
138
139
static inline void context_put_str(enum osips_context type, context_p ctx,
140
                   int pos, str *data)
141
0
{
142
0
#ifdef DBG_MALLOC
143
0
  if (pos < 0 || pos >= type_sizes[type][CONTEXT_STR_TYPE]) {
144
0
    LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_STR_TYPE]);
145
0
    abort();
146
0
  }
147
0
#endif
148
0
  ((str *)(void *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos] = *data;
149
0
}
Unexecuted instantiation: fuzz_uri_parser.c:context_put_str
Unexecuted instantiation: context.c:context_put_str
Unexecuted instantiation: dset.c:context_put_str
Unexecuted instantiation: action.c:context_put_str
Unexecuted instantiation: proto_tcp.c:context_put_str
Unexecuted instantiation: proto_udp.c:context_put_str
Unexecuted instantiation: receive.c:context_put_str
Unexecuted instantiation: forward.c:context_put_str
Unexecuted instantiation: blacklists.c:context_put_str
Unexecuted instantiation: msg_translator.c:context_put_str
Unexecuted instantiation: core_cmds.c:context_put_str
150
151
static inline void context_put_ptr(enum osips_context type, context_p ctx,
152
           int pos, void *data)
153
0
{
154
#ifdef DBG_MALLOC
155
  if (pos < 0 || pos >= type_sizes[type][CONTEXT_PTR_TYPE]) {
156
    LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_PTR_TYPE]);
157
    abort();
158
  }
159
#endif
160
161
0
  ((void **)(void *)((char *)ctx + type_offsets[type][CONTEXT_PTR_TYPE]))[pos] = data;
162
0
}
Unexecuted instantiation: fuzz_uri_parser.c:context_put_ptr
Unexecuted instantiation: context.c:context_put_ptr
Unexecuted instantiation: dset.c:context_put_ptr
Unexecuted instantiation: action.c:context_put_ptr
Unexecuted instantiation: proto_tcp.c:context_put_ptr
Unexecuted instantiation: proto_udp.c:context_put_ptr
Unexecuted instantiation: receive.c:context_put_ptr
Unexecuted instantiation: forward.c:context_put_ptr
Unexecuted instantiation: blacklists.c:context_put_ptr
Unexecuted instantiation: msg_translator.c:context_put_ptr
Unexecuted instantiation: core_cmds.c:context_put_ptr
163
164
   /****************************** GETTERS ********************************/
165
166
static inline int context_get_int(enum osips_context type,
167
                  context_p ctx, int pos)
168
0
{
169
#ifdef DBG_MALLOC
170
  if (pos < 0 || pos >= type_sizes[type][CONTEXT_INT_TYPE]) {
171
    LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_INT_TYPE]);
172
    abort();
173
  }
174
#endif
175
176
0
  return ((int *)ctx)[pos];
177
0
}
Unexecuted instantiation: fuzz_uri_parser.c:context_get_int
Unexecuted instantiation: context.c:context_get_int
Unexecuted instantiation: dset.c:context_get_int
Unexecuted instantiation: action.c:context_get_int
Unexecuted instantiation: proto_tcp.c:context_get_int
Unexecuted instantiation: proto_udp.c:context_get_int
Unexecuted instantiation: receive.c:context_get_int
Unexecuted instantiation: forward.c:context_get_int
Unexecuted instantiation: blacklists.c:context_get_int
Unexecuted instantiation: msg_translator.c:context_get_int
Unexecuted instantiation: core_cmds.c:context_get_int
178
179
static inline str *context_get_str(enum osips_context type,
180
                   context_p ctx, int pos)
181
0
{
182
#ifdef DBG_MALLOC
183
  if (pos < 0 || pos >= type_sizes[type][CONTEXT_STR_TYPE]) {
184
    LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_STR_TYPE]);
185
    abort();
186
  }
187
#endif
188
189
0
  return &((str *)(void *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos];
190
0
}
Unexecuted instantiation: fuzz_uri_parser.c:context_get_str
Unexecuted instantiation: context.c:context_get_str
Unexecuted instantiation: dset.c:context_get_str
Unexecuted instantiation: action.c:context_get_str
Unexecuted instantiation: proto_tcp.c:context_get_str
Unexecuted instantiation: proto_udp.c:context_get_str
Unexecuted instantiation: receive.c:context_get_str
Unexecuted instantiation: forward.c:context_get_str
Unexecuted instantiation: blacklists.c:context_get_str
Unexecuted instantiation: msg_translator.c:context_get_str
Unexecuted instantiation: core_cmds.c:context_get_str
191
192
static inline void *context_get_ptr(enum osips_context type,
193
                  context_p ctx, int pos)
194
0
{
195
#ifdef DBG_MALLOC
196
  if (pos < 0 || pos >= type_sizes[type][CONTEXT_PTR_TYPE]) {
197
    LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_PTR_TYPE]);
198
    abort();
199
  }
200
#endif
201
202
0
  return ((void **)(void *)((char *)ctx +
203
0
                    type_offsets[type][CONTEXT_PTR_TYPE]))[pos];
204
0
}
Unexecuted instantiation: fuzz_uri_parser.c:context_get_ptr
Unexecuted instantiation: context.c:context_get_ptr
Unexecuted instantiation: dset.c:context_get_ptr
Unexecuted instantiation: action.c:context_get_ptr
Unexecuted instantiation: proto_tcp.c:context_get_ptr
Unexecuted instantiation: proto_udp.c:context_get_ptr
Unexecuted instantiation: receive.c:context_get_ptr
Unexecuted instantiation: forward.c:context_get_ptr
Unexecuted instantiation: blacklists.c:context_get_ptr
Unexecuted instantiation: msg_translator.c:context_get_ptr
Unexecuted instantiation: core_cmds.c:context_get_ptr
205
206
#endif /* __CONTEXT_H */