Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Data Structures | Functions | Variables
pjsip_global_headers.c File Reference
#include "asterisk.h"
#include <pjsip.h>
#include <pjlib.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/linkedlists.h"
#include "include/res_pjsip_private.h"
Include dependency graph for pjsip_global_headers.c:

Go to the source code of this file.

Data Structures

struct  header
struct  header_list


static int add_header (struct header_list *headers, const char *name, const char *value, int replace)
static void add_headers_to_message (struct header_list *headers, pjsip_tx_data *tdata)
static pj_status_t add_request_headers (pjsip_tx_data *tdata)
static pj_status_t add_response_headers (pjsip_tx_data *tdata)
static struct headeralloc_header (const char *name, const char *value)
int ast_sip_add_global_request_header (const char *name, const char *value, int replace)
int ast_sip_add_global_response_header (const char *name, const char *value, int replace)
void ast_sip_destroy_global_headers (void)
void ast_sip_initialize_global_headers (void)
static void destroy_header (struct header *to_destroy)
static void destroy_headers (struct header_list *headers)
static void remove_header (struct header_list *headers, const char *to_remove)


static pjsip_module global_header_mod
static unsigned int handled_id = 0xCA115785
 Indicator we've already handled a specific request/response. More...
static struct header_list request_headers
static struct header_list response_headers

Function Documentation

◆ add_header()

static int add_header ( struct header_list headers,
const char *  name,
const char *  value,
int  replace 

Definition at line 130 of file pjsip_global_headers.c.

131 {
132  struct header *to_add = NULL;
134  if (!ast_strlen_zero(value)) {
135  to_add = alloc_header(name, value);
136  if (!to_add) {
137  return -1;
138  }
139  }
141  AST_RWLIST_WRLOCK(headers);
142  if (replace) {
143  remove_header(headers, name);
144  }
145  if (to_add) {
146  AST_LIST_INSERT_TAIL(headers, to_add, next);
147  }
148  AST_RWLIST_UNLOCK(headers);
150  return 0;
151 }
static const char name[]
Definition: format_mp3.c:68
static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_strings.c:815
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
static struct header * alloc_header(const char *name, const char *value)
static void remove_header(struct header_list *headers, const char *to_remove)
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
struct header * next
int value
Definition: syslog.c:37

References alloc_header(), AST_LIST_INSERT_TAIL, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strlen_zero(), name, header::next, NULL, remove_header(), replace(), and value.

Referenced by ast_sip_add_global_request_header(), and ast_sip_add_global_response_header().

◆ add_headers_to_message()

static void add_headers_to_message ( struct header_list headers,
pjsip_tx_data *  tdata 

Definition at line 82 of file pjsip_global_headers.c.

83 {
84  struct header *iter;
86  if (tdata->mod_data[] == &handled_id) {
87  return;
88  }
89  AST_LIST_TRAVERSE(headers, iter, next) {
90  pj_str_t name;
91  pjsip_generic_string_hdr *hdr;
93  hdr = pjsip_msg_find_hdr_by_name(tdata->msg, pj_cstr(&name, iter->name), NULL);
94  if (hdr) {
95  continue;
96  }
98  ast_sip_add_header(tdata, iter->name, iter->value);
99  };
100  tdata->mod_data[] = &handled_id;
101 }
ast_mutex_t lock
Definition: app_meetme.c:1093
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc)
Scoped Locks.
Definition: lock.h:581
static pjsip_module global_header_mod
static unsigned int handled_id
Indicator we've already handled a specific request/response.
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition: res_pjsip.c:1858
const ast_string_field value
const ast_string_field name

References AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_sip_add_header(), global_header_mod, handled_id, lock, name, header::name, header::next, NULL, SCOPED_LOCK, and header::value.

Referenced by add_request_headers(), and add_response_headers().

◆ add_request_headers()

static pj_status_t add_request_headers ( pjsip_tx_data *  tdata)

Definition at line 103 of file pjsip_global_headers.c.

104 {
107  return PJ_SUCCESS;
108 }
static void add_headers_to_message(struct header_list *headers, pjsip_tx_data *tdata)
static struct header_list request_headers

References add_headers_to_message(), and request_headers.

◆ add_response_headers()

static pj_status_t add_response_headers ( pjsip_tx_data *  tdata)

Definition at line 110 of file pjsip_global_headers.c.

111 {
114  return PJ_SUCCESS;
115 }
static struct header_list response_headers

References add_headers_to_message(), and response_headers.

◆ alloc_header()

static struct header* alloc_header ( const char *  name,
const char *  value 

Definition at line 55 of file pjsip_global_headers.c.

56 {
57  struct header *alloc;
59  alloc = ast_calloc_with_stringfields(1, struct header, 32);
61  if (!alloc) {
62  return NULL;
63  }
68  return alloc;
69 }
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
Definition: stringfields.h:432
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521

References ast_calloc_with_stringfields, ast_string_field_set, name, NULL, and value.

Referenced by add_header().

◆ ast_sip_add_global_request_header()

int ast_sip_add_global_request_header ( const char *  name,
const char *  value,
int  replace 

Definition at line 153 of file pjsip_global_headers.c.

154 {
156 }
static int add_header(struct header_list *headers, const char *name, const char *value, int replace)

References add_header(), name, replace(), request_headers, and value.

Referenced by global_apply().

◆ ast_sip_add_global_response_header()

int ast_sip_add_global_response_header ( const char *  name,
const char *  value,
int  replace 

Definition at line 158 of file pjsip_global_headers.c.

159 {
161 }

References add_header(), name, replace(), response_headers, and value.

Referenced by global_apply().

◆ ast_sip_destroy_global_headers()

void ast_sip_destroy_global_headers ( void  )

Definition at line 181 of file pjsip_global_headers.c.

182 {
187 }
static void destroy_headers(struct header_list *headers)
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:126

References ast_sip_unregister_service(), destroy_headers(), global_header_mod, request_headers, and response_headers.

Referenced by unload_pjsip().

◆ ast_sip_initialize_global_headers()

void ast_sip_initialize_global_headers ( void  )

Definition at line 163 of file pjsip_global_headers.c.

164 {
169 }
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:639
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:110

References AST_RWLIST_HEAD_INIT, ast_sip_register_service(), global_header_mod, request_headers, and response_headers.

Referenced by load_module().

◆ destroy_header()

static void destroy_header ( struct header to_destroy)

Definition at line 71 of file pjsip_global_headers.c.

72 {
73  ast_string_field_free_memory(to_destroy);
74  ast_free(to_destroy);
75 }
#define ast_free(a)
Definition: astmm.h:180
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374

References ast_free, and ast_string_field_free_memory.

Referenced by destroy_headers(), and remove_header().

◆ destroy_headers()

static void destroy_headers ( struct header_list headers)

Definition at line 171 of file pjsip_global_headers.c.

172 {
173  struct header *iter;
175  while ((iter = AST_RWLIST_REMOVE_HEAD(headers, next))) {
176  destroy_header(iter);
177  }
179 }
Definition: linkedlists.h:844
Destroys an rwlist head structure.
Definition: linkedlists.h:667
static void destroy_header(struct header *to_destroy)

References AST_RWLIST_HEAD_DESTROY, AST_RWLIST_REMOVE_HEAD, destroy_header(), and header::next.

Referenced by ast_sip_destroy_global_headers().

◆ remove_header()

static void remove_header ( struct header_list headers,
const char *  to_remove 

Definition at line 117 of file pjsip_global_headers.c.

118 {
119  struct header *iter;
120  AST_LIST_TRAVERSE_SAFE_BEGIN(headers, iter, next) {
121  if (!strcasecmp(iter->name, to_remove)) {
123  destroy_header(iter);
124  break;
125  }
126  }
128 }
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557

References AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, destroy_header(), header::name, and header::next.

Referenced by add_header().

Variable Documentation

◆ global_header_mod

pjsip_module global_header_mod

◆ handled_id

unsigned int handled_id = 0xCA115785

Indicator we've already handled a specific request/response.

PJSIP tends to reuse requests and responses. If we already have added headers to a request or response, we mark the message with this value so that we know not to re-add the headers again.

Definition at line 38 of file pjsip_global_headers.c.

Referenced by add_headers_to_message().

◆ request_headers

struct header_list request_headers

◆ response_headers

struct header_list response_headers