Skip to content

Commit bb300ae

Browse files
committed
core: make sockaddr_union* and socket_info* arguments, vars and members const
Make the said pointers const in most places where appropriate. This basically ensures that code in modules cannot mess with them intentionally or by a mistake. It should also have some positive effect on the performance of the code, as with those being const * the optimizer would have more room to speculate about what code might and might not do. There might be also some improvement of static analysis front. Most of the change is pretty mechanical, the only functional change is the allocation of the socket_info.last_real_port, since those are expected to be updated by the underlying protos/modules. Work around that by making a separate smaller struct last_real_ports and co-allocate it along with the socket_info main backing storage, exposing the non-const pointer to that instead. Move the prev / next into the semi-private part of the struct. Rework net/api_proto_net.h to not require casting handler functions to a generic type. Next step would be make str members of socket_info into str_const. Tested with voiptests.
1 parent 5dc91d3 commit bb300ae

File tree

116 files changed

+593
-545
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+593
-545
lines changed

core_cmds.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static int fixup_f_send_sock(void** param)
498498
str host, host_nt;
499499
int proto=PROTO_NONE, port;
500500
struct hostent* he;
501-
struct socket_info* si;
501+
const struct socket_info* si;
502502
struct ip_addr ip;
503503

504504
if (parse_phostport(s->s, s->len, &host.s, &host.len, &port, &proto) != 0) {
@@ -526,7 +526,8 @@ static int fixup_f_send_sock(void** param)
526526

527527
pkg_free(host_nt.s);
528528

529-
*param = si;
529+
const void **_param = (const void **)param;
530+
*_param = si;
530531
return 0;
531532

532533
error:
@@ -1056,7 +1057,7 @@ static int w_set_adv_port(struct sip_msg *msg, str *adv_port)
10561057

10571058
static int w_f_send_sock(struct sip_msg *msg, struct socket_info *si)
10581059
{
1059-
msg->force_send_socket=si;
1060+
msg->force_send_socket=(const struct socket_info *)si;
10601061

10611062
return 1;
10621063
}

dset.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct branch
6262
unsigned int path_len;
6363

6464
int q; /* Preference of the contact among contact within the array */
65-
struct socket_info* force_send_socket;
65+
const struct socket_info* force_send_socket;
6666
unsigned int flags;
6767
};
6868

@@ -220,7 +220,7 @@ void set_dset_state(unsigned char enable)
220220
* more branches
221221
*/
222222
char* get_branch(unsigned int idx, int* len, qvalue_t* q, str* dst_uri,
223-
str* path, unsigned int *flags, struct socket_info** force_socket)
223+
str* path, unsigned int *flags, const struct socket_info** force_socket)
224224
{
225225
struct dset_ctx *dsct = get_dset_ctx();
226226
struct branch *branches;
@@ -275,7 +275,7 @@ void clear_branches(void)
275275
* Add a new branch to current transaction
276276
*/
277277
int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
278-
qvalue_t q, unsigned int flags, struct socket_info* force_socket)
278+
qvalue_t q, unsigned int flags, const struct socket_info* force_socket)
279279
{
280280
str luri;
281281
int nr_branches;
@@ -382,7 +382,7 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
382382
* Updates one or more fields of an already appended branch
383383
*/
384384
int update_branch(unsigned int idx, str** uri, str** dst_uri, str** path,
385-
qvalue_t* q, unsigned int* flags, struct socket_info** force_socket)
385+
qvalue_t* q, unsigned int* flags, const struct socket_info** force_socket)
386386
{
387387
struct dset_ctx *dsct = get_dset_ctx();
388388
struct branch *branches;

dset.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@ int init_dset(void);
4343
* Add a new branch to current transaction
4444
*/
4545
int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
46-
qvalue_t q, unsigned int flags, struct socket_info* force_socket);
46+
qvalue_t q, unsigned int flags, const struct socket_info* force_socket);
4747

4848

4949

5050
/* ! \brief
5151
* Updates an already created branches
5252
*/
5353
int update_branch(unsigned int idx, str** uri, str** dst_uri, str** path,
54-
qvalue_t* q, unsigned int* flags, struct socket_info** force_socket);
54+
qvalue_t* q, unsigned int* flags, const struct socket_info** force_socket);
5555

5656

5757
/*! \brief
5858
* Get the next branch in the current transaction
5959
*/
6060
char* get_branch( unsigned int idx, int* len, qvalue_t* q, str* dst_uri,
61-
str* path, unsigned int *flags, struct socket_info** force_socket);
61+
str* path, unsigned int *flags, const struct socket_info** force_socket);
6262

6363

6464
/*! \brief

forward.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@
9090
* be very likely noticeably slower, but it can deal better with
9191
* multihomed hosts
9292
*/
93-
struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
93+
const struct socket_info* get_out_socket(const union sockaddr_union* to, int proto)
9494
{
9595
int temp_sock;
9696
socklen_t len;
9797
union sockaddr_union from;
98-
struct socket_info* si;
98+
const struct socket_info* si;
9999
struct ip_addr ip, ip_dst;
100100

101101
if (proto!=PROTO_UDP) {
@@ -142,10 +142,10 @@ struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
142142
*
143143
* \note if msg!=null and msg->force_send_socket, the force_send_socket will be used
144144
*/
145-
struct socket_info* get_send_socket(struct sip_msg *msg,
146-
union sockaddr_union* to, int proto)
145+
const struct socket_info* get_send_socket(struct sip_msg *msg,
146+
const union sockaddr_union* to, int proto)
147147
{
148-
struct socket_info* send_sock;
148+
const struct socket_info* send_sock;
149149

150150
/* check if send interface is not forced */
151151
if (msg && msg->force_send_socket){
@@ -308,8 +308,8 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
308308
{
309309
union sockaddr_union to;
310310
str buf;
311-
struct socket_info* send_sock;
312-
struct socket_info* last_sock;
311+
const struct socket_info* send_sock;
312+
const struct socket_info* last_sock;
313313

314314
buf.s=NULL;
315315

@@ -473,7 +473,7 @@ int forward_reply(struct sip_msg* msg)
473473
struct sr_module *mod;
474474
int proto;
475475
unsigned int id; /* used only by tcp*/
476-
struct socket_info *send_sock;
476+
const struct socket_info *send_sock;
477477
char* s;
478478
int len;
479479

forward.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
#include "net/trans.h"
4848
#include "socket_info.h"
4949

50-
struct socket_info* get_send_socket(struct sip_msg* msg,
51-
union sockaddr_union* su, int proto);
52-
struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
50+
const struct socket_info* get_send_socket(struct sip_msg* msg,
51+
const union sockaddr_union* su, int proto);
52+
const struct socket_info* get_out_socket(const union sockaddr_union* to, int proto);
5353
int check_self(str* host, unsigned short port, unsigned short proto);
5454
int forward_request( struct sip_msg* msg, struct proxy_l* p);
5555
int update_sock_struct_from_via( union sockaddr_union* to,
@@ -81,7 +81,7 @@ int forward_reply( struct sip_msg* msg);
8181
* \param len - the length of the message to be sent
8282
* \return 0 if ok, -1 on error
8383
*/
84-
static inline int msg_send( struct socket_info* send_sock, int proto,
84+
static inline int msg_send( const struct socket_info* send_sock, int proto,
8585
union sockaddr_union* to, unsigned int id,
8686
char* buf, int len, struct sip_msg* msg)
8787
{

globals.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ int mcast_ttl = -1; /* if -1, don't touch it, use the default (usually 1) */
138138

139139
int tos = IPTOS_LOWDELAY; // lgtm [cpp/short-global-name]
140140

141-
struct socket_info* bind_address=NULL; /* pointer to the crt. proc.
141+
const struct socket_info* bind_address=NULL; /* pointer to the crt. proc.
142142
listening address*/
143143

144144
/* if aliases should be automatically discovered and added

globals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extern char *stat_file;
4444
extern char* pid_file;
4545
extern char* pgid_file;
4646

47-
extern struct socket_info* bind_address; /*!< pointer to the crt. proc. listening address */
47+
extern const struct socket_info* bind_address; /*!< pointer to the crt. proc. listening address */
4848

4949
extern int auto_aliases;
5050

ip_addr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
char _ip_addr_A_buffs[IP_ADDR2STR_BUF_NO][IP_ADDR_MAX_STR_SIZE];
4242

43-
struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)
43+
struct net* mk_net(const struct ip_addr* ip, struct ip_addr* mask)
4444
{
4545
struct net* n;
4646
int warning;
@@ -78,7 +78,7 @@ struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)
7878

7979

8080

81-
struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
81+
struct net* mk_net_bitlen(const struct ip_addr* ip, unsigned int bitlen)
8282
{
8383
struct ip_addr mask;
8484
unsigned int r;
@@ -100,7 +100,7 @@ struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
100100

101101

102102

103-
void print_ip(char* p, struct ip_addr* ip, char *s)
103+
void print_ip(char* p, const struct ip_addr* ip, char *s)
104104
{
105105
switch(ip->af){
106106
case AF_INET:

ip_addr.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct receive_info {
100100
unsigned int proto_reserved1; /*!< tcp stores the connection id here */
101101
unsigned int proto_reserved2;
102102
union sockaddr_union src_su; /*!< useful for replies*/
103-
struct socket_info* bind_address; /*!< sock_info structure on which the msg was received*/
103+
const struct socket_info* bind_address; /*!< sock_info structure on which the msg was received*/
104104
/* no need for dst_su yet */
105105
};
106106

@@ -109,7 +109,7 @@ struct dest_info {
109109
int proto;
110110
unsigned int proto_reserved1; /*!< tcp stores the connection id here */
111111
union sockaddr_union to;
112-
struct socket_info* send_sock;
112+
const struct socket_info* send_sock;
113113
};
114114

115115

@@ -171,12 +171,12 @@ struct socket_id {
171171
* we rely here on the fact at all the SIP protos are in a sequance */
172172
#define is_sip_proto(_proto) (PROTO_UDP<=(_proto) && (_proto)<=PROTO_WSS)
173173

174-
struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask);
175-
struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen);
174+
struct net* mk_net(const struct ip_addr* ip, struct ip_addr* mask);
175+
struct net* mk_net_bitlen(const struct ip_addr* ip, unsigned int bitlen);
176176
/* parse a (struct net) out of a CIDR v4 or v6 address such as 1.2.3.4/28 */
177177
int mk_net_cidr(const str *cidr, struct net *out_net);
178178

179-
void print_ip(char* prefix, struct ip_addr* ip, char* suffix);
179+
void print_ip(char* prefix, const struct ip_addr* ip, char* suffix);
180180
void stdout_print_ip(struct ip_addr* ip);
181181
void print_net(struct net* net);
182182

@@ -212,7 +212,7 @@ inline static int matchnet(struct ip_addr* ip, struct net* net)
212212

213213

214214
/*! \brief inits an ip_addr pointer from a sockaddr structure*/
215-
static inline void sockaddr2ip_addr(struct ip_addr* ip, struct sockaddr* sa)
215+
static inline void sockaddr2ip_addr(struct ip_addr* ip, const struct sockaddr* sa)
216216
{
217217
void *copyfrom;
218218

@@ -266,7 +266,7 @@ static inline int su_cmp(union sockaddr_union* s1, union sockaddr_union* s2)
266266

267267

268268
/*! \brief gets the port number (host byte order) */
269-
static inline unsigned short su_getport(union sockaddr_union* su)
269+
static inline unsigned short su_getport(const union sockaddr_union* su)
270270
{
271271
if(su==0)
272272
return 0;
@@ -298,7 +298,7 @@ static inline void su_setport(union sockaddr_union* su, unsigned short port)
298298
}
299299

300300
/*! \brief inits an ip_addr pointer from a sockaddr_union ip address */
301-
static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
301+
static inline void su2ip_addr(struct ip_addr* ip, const union sockaddr_union* su)
302302
{
303303
switch(su->s.sa_family){
304304
case AF_INET:
@@ -326,7 +326,7 @@ static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
326326
* \return 0 if ok, -1 on error (unknown address family)
327327
* \note the port number is in host byte order */
328328
static inline int init_su( union sockaddr_union* su,
329-
struct ip_addr* ip,
329+
const struct ip_addr* ip,
330330
unsigned short port )
331331
{
332332
memset(su, 0, sizeof(union sockaddr_union));/*needed on freebsd*/
@@ -360,7 +360,7 @@ static inline int init_su( union sockaddr_union* su,
360360
* WARNING: no index overflow checks!
361361
* \return 0 if ok, -1 on error (unknown address family) */
362362
static inline int hostent2su( union sockaddr_union* su,
363-
struct hostent* he,
363+
const struct hostent* he,
364364
unsigned int idx,
365365
unsigned short port )
366366
{

mi/mi_trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ char* generate_correlation_id(int* len)
130130
}
131131

132132

133-
int trace_mi_message(union sockaddr_union* src, union sockaddr_union* dst,
133+
int trace_mi_message(const union sockaddr_union* src, const union sockaddr_union* dst,
134134
struct mi_trace_param* pld_param, str* correlation_val, trace_dest trace_dst)
135135
{
136136
/* FIXME is this the case for all mi impelementations?? */

mi/mi_trace.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern struct mi_trace_param mi_tparam;
5555

5656
void try_load_trace_api(void);
5757

58-
int trace_mi_message(union sockaddr_union* src, union sockaddr_union* dst,
58+
int trace_mi_message(const union sockaddr_union* src, const union sockaddr_union* dst,
5959
struct mi_trace_param* pld_param, str* correlation_value, trace_dest trace_dst);
6060

6161
struct mi_trace_req* build_mi_trace_request(str *cmd, mi_item_t *params,
@@ -65,7 +65,7 @@ str *build_mi_trace_reply(str *rpl_msg);
6565
char* generate_correlation_id(int* len);;
6666
int load_correlation_id(void);
6767

68-
static inline void mi_trace_reply( union sockaddr_union* src, union sockaddr_union* dst,
68+
static inline void mi_trace_reply( const union sockaddr_union* src, const union sockaddr_union* dst,
6969
str* message, trace_dest t_dst)
7070
{
7171
/* trace disabled */
@@ -91,7 +91,7 @@ static inline void mi_trace_reply( union sockaddr_union* src, union sockaddr_uni
9191
}
9292

9393

94-
static inline void mi_trace_request( union sockaddr_union* src, union sockaddr_union* dst,
94+
static inline void mi_trace_request( const union sockaddr_union* src, const union sockaddr_union* dst,
9595
char* command, int len, mi_item_t *params, str* backend, trace_dest t_dst )
9696
{
9797
str comm_s = { command, len };

modules/b2b_entities/b2be_clustering.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ void replicate_entity_delete(b2b_dlg_t *dlg, int etype, unsigned int hash_index,
315315
return;
316316
}
317317

318-
static struct socket_info *fetch_socket_info(str *addr)
318+
static const struct socket_info *fetch_socket_info(str *addr)
319319
{
320-
struct socket_info *sock;
320+
const struct socket_info *sock;
321321
if (!addr || !addr->len)
322322
return NULL;
323323

modules/b2b_entities/b2be_load.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ typedef struct client_info
7575
str local_contact;
7676
unsigned int cseq;
7777
unsigned int maxfwd;
78-
struct socket_info* send_sock;
79-
struct socket_info* pref_sock;
78+
const struct socket_info* send_sock;
79+
const struct socket_info* pref_sock;
8080
struct usr_avp *avps;
8181
}client_info_t;
8282

modules/b2b_entities/dlg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ typedef struct b2b_dlg
110110
struct cell* prack_tran;
111111
struct cell* cancel_tm_tran;
112112
dlg_leg_t* legs;
113-
struct socket_info* send_sock;
113+
const struct socket_info* send_sock;
114114
unsigned int last_reply_code;
115115
int db_flag;
116116
int replicated;

modules/b2b_logic/logic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ void b2b_mark_todel( b2bl_tuple_t* tuple)
475475
int b2b_get_local_contact(struct sip_msg *msg, str *from_uri, str *local_contact)
476476
{
477477
struct sip_uri ct_uri;
478-
struct socket_info *send_sock = msg ?
478+
const struct socket_info *send_sock = msg ?
479479
(msg->force_send_socket?msg->force_send_socket:msg->rcv.bind_address):NULL;
480480

481481
if (server_address.len) {

modules/b2b_sdp_demux/b2b_sdp_demux.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,8 +1673,8 @@ static int b2b_sdp_demux_start(struct sip_msg *msg, str *uri,
16731673
struct list_head *it;
16741674
struct b2b_sdp_client *client;
16751675
client_info_t ci;
1676-
struct socket_info *si;
1677-
str *sess_ip;
1676+
const struct socket_info *si;
1677+
const str *sess_ip;
16781678

16791679
if (!msg->force_send_socket) {
16801680
si = uri2sock(msg, uri, &tmp_su, PROTO_NONE);

modules/clusterer/node_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct cluster_info {
110110
int no_nodes; /* number of nodes in the cluster */
111111
struct node_info *node_list;
112112
struct node_info *current_node; /* current node's info in this cluster */
113-
struct socket_info *send_sock;
113+
const struct socket_info *send_sock;
114114

115115
gen_lock_t *lock;
116116

0 commit comments

Comments
 (0)