Skip to content

Commit

Permalink
core: make sockaddr_union* and socket_info* arguments, vars and membe…
Browse files Browse the repository at this point in the history
…rs 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.
  • Loading branch information
sobomax committed Dec 2, 2023
1 parent 5dc91d3 commit bb300ae
Show file tree
Hide file tree
Showing 116 changed files with 593 additions and 545 deletions.
7 changes: 4 additions & 3 deletions core_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ static int fixup_f_send_sock(void** param)
str host, host_nt;
int proto=PROTO_NONE, port;
struct hostent* he;
struct socket_info* si;
const struct socket_info* si;
struct ip_addr ip;

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

pkg_free(host_nt.s);

*param = si;
const void **_param = (const void **)param;
*_param = si;
return 0;

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

static int w_f_send_sock(struct sip_msg *msg, struct socket_info *si)
{
msg->force_send_socket=si;
msg->force_send_socket=(const struct socket_info *)si;

return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions dset.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct branch
unsigned int path_len;

int q; /* Preference of the contact among contact within the array */
struct socket_info* force_send_socket;
const struct socket_info* force_send_socket;
unsigned int flags;
};

Expand Down Expand Up @@ -220,7 +220,7 @@ void set_dset_state(unsigned char enable)
* more branches
*/
char* get_branch(unsigned int idx, int* len, qvalue_t* q, str* dst_uri,
str* path, unsigned int *flags, struct socket_info** force_socket)
str* path, unsigned int *flags, const struct socket_info** force_socket)
{
struct dset_ctx *dsct = get_dset_ctx();
struct branch *branches;
Expand Down Expand Up @@ -275,7 +275,7 @@ void clear_branches(void)
* Add a new branch to current transaction
*/
int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
qvalue_t q, unsigned int flags, struct socket_info* force_socket)
qvalue_t q, unsigned int flags, const struct socket_info* force_socket)
{
str luri;
int nr_branches;
Expand Down Expand Up @@ -382,7 +382,7 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
* Updates one or more fields of an already appended branch
*/
int update_branch(unsigned int idx, str** uri, str** dst_uri, str** path,
qvalue_t* q, unsigned int* flags, struct socket_info** force_socket)
qvalue_t* q, unsigned int* flags, const struct socket_info** force_socket)
{
struct dset_ctx *dsct = get_dset_ctx();
struct branch *branches;
Expand Down
6 changes: 3 additions & 3 deletions dset.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ int init_dset(void);
* Add a new branch to current transaction
*/
int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
qvalue_t q, unsigned int flags, struct socket_info* force_socket);
qvalue_t q, unsigned int flags, const struct socket_info* force_socket);



/* ! \brief
* Updates an already created branches
*/
int update_branch(unsigned int idx, str** uri, str** dst_uri, str** path,
qvalue_t* q, unsigned int* flags, struct socket_info** force_socket);
qvalue_t* q, unsigned int* flags, const struct socket_info** force_socket);


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


/*! \brief
Expand Down
16 changes: 8 additions & 8 deletions forward.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@
* be very likely noticeably slower, but it can deal better with
* multihomed hosts
*/
struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
const struct socket_info* get_out_socket(const union sockaddr_union* to, int proto)
{
int temp_sock;
socklen_t len;
union sockaddr_union from;
struct socket_info* si;
const struct socket_info* si;
struct ip_addr ip, ip_dst;

if (proto!=PROTO_UDP) {
Expand Down Expand Up @@ -142,10 +142,10 @@ struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
*
* \note if msg!=null and msg->force_send_socket, the force_send_socket will be used
*/
struct socket_info* get_send_socket(struct sip_msg *msg,
union sockaddr_union* to, int proto)
const struct socket_info* get_send_socket(struct sip_msg *msg,
const union sockaddr_union* to, int proto)
{
struct socket_info* send_sock;
const struct socket_info* send_sock;

/* check if send interface is not forced */
if (msg && msg->force_send_socket){
Expand Down Expand Up @@ -308,8 +308,8 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
{
union sockaddr_union to;
str buf;
struct socket_info* send_sock;
struct socket_info* last_sock;
const struct socket_info* send_sock;
const struct socket_info* last_sock;

buf.s=NULL;

Expand Down Expand Up @@ -473,7 +473,7 @@ int forward_reply(struct sip_msg* msg)
struct sr_module *mod;
int proto;
unsigned int id; /* used only by tcp*/
struct socket_info *send_sock;
const struct socket_info *send_sock;
char* s;
int len;

Expand Down
8 changes: 4 additions & 4 deletions forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
#include "net/trans.h"
#include "socket_info.h"

struct socket_info* get_send_socket(struct sip_msg* msg,
union sockaddr_union* su, int proto);
struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
const struct socket_info* get_send_socket(struct sip_msg* msg,
const union sockaddr_union* su, int proto);
const struct socket_info* get_out_socket(const union sockaddr_union* to, int proto);
int check_self(str* host, unsigned short port, unsigned short proto);
int forward_request( struct sip_msg* msg, struct proxy_l* p);
int update_sock_struct_from_via( union sockaddr_union* to,
Expand Down Expand Up @@ -81,7 +81,7 @@ int forward_reply( struct sip_msg* msg);
* \param len - the length of the message to be sent
* \return 0 if ok, -1 on error
*/
static inline int msg_send( struct socket_info* send_sock, int proto,
static inline int msg_send( const struct socket_info* send_sock, int proto,
union sockaddr_union* to, unsigned int id,
char* buf, int len, struct sip_msg* msg)
{
Expand Down
2 changes: 1 addition & 1 deletion globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ int mcast_ttl = -1; /* if -1, don't touch it, use the default (usually 1) */

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

struct socket_info* bind_address=NULL; /* pointer to the crt. proc.
const struct socket_info* bind_address=NULL; /* pointer to the crt. proc.
listening address*/

/* if aliases should be automatically discovered and added
Expand Down
2 changes: 1 addition & 1 deletion globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern char *stat_file;
extern char* pid_file;
extern char* pgid_file;

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

extern int auto_aliases;

Expand Down
6 changes: 3 additions & 3 deletions ip_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

char _ip_addr_A_buffs[IP_ADDR2STR_BUF_NO][IP_ADDR_MAX_STR_SIZE];

struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)
struct net* mk_net(const struct ip_addr* ip, struct ip_addr* mask)
{
struct net* n;
int warning;
Expand Down Expand Up @@ -78,7 +78,7 @@ struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)



struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
struct net* mk_net_bitlen(const struct ip_addr* ip, unsigned int bitlen)
{
struct ip_addr mask;
unsigned int r;
Expand All @@ -100,7 +100,7 @@ struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)



void print_ip(char* p, struct ip_addr* ip, char *s)
void print_ip(char* p, const struct ip_addr* ip, char *s)
{
switch(ip->af){
case AF_INET:
Expand Down
20 changes: 10 additions & 10 deletions ip_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct receive_info {
unsigned int proto_reserved1; /*!< tcp stores the connection id here */
unsigned int proto_reserved2;
union sockaddr_union src_su; /*!< useful for replies*/
struct socket_info* bind_address; /*!< sock_info structure on which the msg was received*/
const struct socket_info* bind_address; /*!< sock_info structure on which the msg was received*/
/* no need for dst_su yet */
};

Expand All @@ -109,7 +109,7 @@ struct dest_info {
int proto;
unsigned int proto_reserved1; /*!< tcp stores the connection id here */
union sockaddr_union to;
struct socket_info* send_sock;
const struct socket_info* send_sock;
};


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

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

void print_ip(char* prefix, struct ip_addr* ip, char* suffix);
void print_ip(char* prefix, const struct ip_addr* ip, char* suffix);
void stdout_print_ip(struct ip_addr* ip);
void print_net(struct net* net);

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


/*! \brief inits an ip_addr pointer from a sockaddr structure*/
static inline void sockaddr2ip_addr(struct ip_addr* ip, struct sockaddr* sa)
static inline void sockaddr2ip_addr(struct ip_addr* ip, const struct sockaddr* sa)
{
void *copyfrom;

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


/*! \brief gets the port number (host byte order) */
static inline unsigned short su_getport(union sockaddr_union* su)
static inline unsigned short su_getport(const union sockaddr_union* su)
{
if(su==0)
return 0;
Expand Down Expand Up @@ -298,7 +298,7 @@ static inline void su_setport(union sockaddr_union* su, unsigned short port)
}

/*! \brief inits an ip_addr pointer from a sockaddr_union ip address */
static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
static inline void su2ip_addr(struct ip_addr* ip, const union sockaddr_union* su)
{
switch(su->s.sa_family){
case AF_INET:
Expand Down Expand Up @@ -326,7 +326,7 @@ static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
* \return 0 if ok, -1 on error (unknown address family)
* \note the port number is in host byte order */
static inline int init_su( union sockaddr_union* su,
struct ip_addr* ip,
const struct ip_addr* ip,
unsigned short port )
{
memset(su, 0, sizeof(union sockaddr_union));/*needed on freebsd*/
Expand Down Expand Up @@ -360,7 +360,7 @@ static inline int init_su( union sockaddr_union* su,
* WARNING: no index overflow checks!
* \return 0 if ok, -1 on error (unknown address family) */
static inline int hostent2su( union sockaddr_union* su,
struct hostent* he,
const struct hostent* he,
unsigned int idx,
unsigned short port )
{
Expand Down
2 changes: 1 addition & 1 deletion mi/mi_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ char* generate_correlation_id(int* len)
}


int trace_mi_message(union sockaddr_union* src, union sockaddr_union* dst,
int trace_mi_message(const union sockaddr_union* src, const union sockaddr_union* dst,
struct mi_trace_param* pld_param, str* correlation_val, trace_dest trace_dst)
{
/* FIXME is this the case for all mi impelementations?? */
Expand Down
6 changes: 3 additions & 3 deletions mi/mi_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extern struct mi_trace_param mi_tparam;

void try_load_trace_api(void);

int trace_mi_message(union sockaddr_union* src, union sockaddr_union* dst,
int trace_mi_message(const union sockaddr_union* src, const union sockaddr_union* dst,
struct mi_trace_param* pld_param, str* correlation_value, trace_dest trace_dst);

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

static inline void mi_trace_reply( union sockaddr_union* src, union sockaddr_union* dst,
static inline void mi_trace_reply( const union sockaddr_union* src, const union sockaddr_union* dst,
str* message, trace_dest t_dst)
{
/* trace disabled */
Expand All @@ -91,7 +91,7 @@ static inline void mi_trace_reply( union sockaddr_union* src, union sockaddr_uni
}


static inline void mi_trace_request( union sockaddr_union* src, union sockaddr_union* dst,
static inline void mi_trace_request( const union sockaddr_union* src, const union sockaddr_union* dst,
char* command, int len, mi_item_t *params, str* backend, trace_dest t_dst )
{
str comm_s = { command, len };
Expand Down
4 changes: 2 additions & 2 deletions modules/b2b_entities/b2be_clustering.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ void replicate_entity_delete(b2b_dlg_t *dlg, int etype, unsigned int hash_index,
return;
}

static struct socket_info *fetch_socket_info(str *addr)
static const struct socket_info *fetch_socket_info(str *addr)
{
struct socket_info *sock;
const struct socket_info *sock;
if (!addr || !addr->len)
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions modules/b2b_entities/b2be_load.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ typedef struct client_info
str local_contact;
unsigned int cseq;
unsigned int maxfwd;
struct socket_info* send_sock;
struct socket_info* pref_sock;
const struct socket_info* send_sock;
const struct socket_info* pref_sock;
struct usr_avp *avps;
}client_info_t;

Expand Down
2 changes: 1 addition & 1 deletion modules/b2b_entities/dlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ typedef struct b2b_dlg
struct cell* prack_tran;
struct cell* cancel_tm_tran;
dlg_leg_t* legs;
struct socket_info* send_sock;
const struct socket_info* send_sock;
unsigned int last_reply_code;
int db_flag;
int replicated;
Expand Down
2 changes: 1 addition & 1 deletion modules/b2b_logic/logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void b2b_mark_todel( b2bl_tuple_t* tuple)
int b2b_get_local_contact(struct sip_msg *msg, str *from_uri, str *local_contact)
{
struct sip_uri ct_uri;
struct socket_info *send_sock = msg ?
const struct socket_info *send_sock = msg ?
(msg->force_send_socket?msg->force_send_socket:msg->rcv.bind_address):NULL;

if (server_address.len) {
Expand Down
4 changes: 2 additions & 2 deletions modules/b2b_sdp_demux/b2b_sdp_demux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1673,8 +1673,8 @@ static int b2b_sdp_demux_start(struct sip_msg *msg, str *uri,
struct list_head *it;
struct b2b_sdp_client *client;
client_info_t ci;
struct socket_info *si;
str *sess_ip;
const struct socket_info *si;
const str *sess_ip;

if (!msg->force_send_socket) {
si = uri2sock(msg, uri, &tmp_su, PROTO_NONE);
Expand Down
2 changes: 1 addition & 1 deletion modules/clusterer/node_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct cluster_info {
int no_nodes; /* number of nodes in the cluster */
struct node_info *node_list;
struct node_info *current_node; /* current node's info in this cluster */
struct socket_info *send_sock;
const struct socket_info *send_sock;

gen_lock_t *lock;

Expand Down

0 comments on commit bb300ae

Please sign in to comment.