Skip to content

Commit 0209363

Browse files
committed
Change 'union sockaddr_union' to 'struct sockaddr_union_struct'
This is so that we can pass hostnames around attached to sockaddr's, so that hostnames in TLS certificates can be validated. The sockaddr_union definition changes from: union sockaddr_union { struct sockaddr s; struct sockaddr_in sin; struct sockaddr_in6 sin6; }; to: struct sockaddr_union_struct { union { struct sockaddr s; struct sockaddr_in sin; struct sockaddr_in6 sin6; } u; char hostname[MAX_DNS_NAME]; /* remote side hostname (used for TLS certificate hostname verification) */ }; And all code that uses a sockaddr_union now has a sockaddr_union_struct, and field accesses go through the "u" field.
1 parent 075789f commit 0209363

File tree

111 files changed

+431
-430
lines changed

Some content is hidden

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

111 files changed

+431
-430
lines changed

blacklists.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int check_against_blacklist(struct ip_addr *ip, str *text, unsigned short port,
8686
unsigned short proto);
8787

8888
static inline int check_blacklists( unsigned short proto,
89-
union sockaddr_union *to, char *body_s, int body_len)
89+
struct sockaddr_union_struct *to, char *body_s, int body_len)
9090
{
9191
str body;
9292
struct ip_addr ip;

core_cmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,13 @@ static int w_forward(struct sip_msg *msg, struct proxy_l *dest)
682682
static int w_send(struct sip_msg *msg, struct proxy_l *dest, str *headers)
683683
{
684684
int ret;
685-
union sockaddr_union* to;
685+
struct sockaddr_union_struct* to;
686686
struct proxy_l* p;
687687
int len;
688688
char* tmp;
689689

690-
to=(union sockaddr_union*)
691-
pkg_malloc(sizeof(union sockaddr_union));
690+
to=(struct sockaddr_union_struct*)
691+
pkg_malloc(sizeof(struct sockaddr_union_struct));
692692
if (to==0){
693693
LM_ERR("memory allocation failure\n");
694694
return E_OUT_OF_MEM;

evi/evi_transport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
/* sockets */
4747
typedef union {
48-
union sockaddr_union udp_addr;
48+
struct sockaddr_union_struct udp_addr;
4949
struct sockaddr_un unix_addr;
5050
} sockaddr_reply;
5151

forward.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@
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+
struct socket_info* get_out_socket(struct sockaddr_union_struct* to, int proto)
9494
{
9595
int temp_sock;
9696
socklen_t len;
97-
union sockaddr_union from;
97+
struct sockaddr_union_struct from;
9898
struct socket_info* si;
9999
struct ip_addr ip, ip_dst;
100100

@@ -103,17 +103,17 @@ struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
103103
return 0;
104104
}
105105

106-
temp_sock=socket(to->s.sa_family, SOCK_DGRAM, 0 );
106+
temp_sock=socket(to->u.s.sa_family, SOCK_DGRAM, 0 );
107107
if (temp_sock==-1) {
108108
LM_ERR("socket() failed: %s\n", strerror(errno));
109109
return 0;
110110
}
111-
if (connect(temp_sock, &to->s, sockaddru_len(*to))==-1) {
111+
if (connect(temp_sock, &to->u.s, sockaddru_len(*to))==-1) {
112112
LM_ERR("connect failed: %s\n", strerror(errno));
113113
goto error;
114114
}
115115
len=sizeof(from);
116-
if (getsockname(temp_sock, &from.s, &len)==-1) {
116+
if (getsockname(temp_sock, &from.u.s, &len)==-1) {
117117
LM_ERR("getsockname failed: %s\n", strerror(errno));
118118
goto error;
119119
}
@@ -143,7 +143,7 @@ struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
143143
* \note if msg!=null and msg->force_send_socket, the force_send_socket will be used
144144
*/
145145
struct socket_info* get_send_socket(struct sip_msg *msg,
146-
union sockaddr_union* to, int proto)
146+
struct sockaddr_union_struct* to, int proto)
147147
{
148148
struct socket_info* send_sock;
149149

@@ -181,7 +181,7 @@ struct socket_info* get_send_socket(struct sip_msg *msg,
181181
switch(proto){
182182
case PROTO_UDP:
183183
if (msg && msg->rcv.bind_address &&
184-
msg->rcv.bind_address->address.af==to->s.sa_family &&
184+
msg->rcv.bind_address->address.af==to->u.s.sa_family &&
185185
msg->rcv.bind_address->proto==PROTO_UDP) {
186186
send_sock = msg->rcv.bind_address;
187187
break;
@@ -191,7 +191,7 @@ struct socket_info* get_send_socket(struct sip_msg *msg,
191191
/* we don't really know the sending address (we can find it out,
192192
* but we'll need also to see if we listen on it, and if yes on
193193
* which port -> too complicated*/
194-
send_sock = (to->s.sa_family==AF_INET) ?
194+
send_sock = (to->u.s.sa_family==AF_INET) ?
195195
protos[proto].sendipv4 : protos[proto].sendipv6;
196196
}
197197
return send_sock;
@@ -306,7 +306,7 @@ static inline int set_sl_branch(struct sip_msg* msg)
306306

307307
int forward_request( struct sip_msg* msg, struct proxy_l * p)
308308
{
309-
union sockaddr_union to;
309+
struct sockaddr_union_struct to;
310310
str buf;
311311
struct socket_info* send_sock;
312312
struct socket_info* last_sock;
@@ -337,7 +337,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
337337
send_sock=get_send_socket( msg, &to, p->proto);
338338
if (send_sock==0){
339339
LM_ERR("cannot forward to af %d, proto %d no corresponding"
340-
"listening socket\n", to.s.sa_family, p->proto);
340+
"listening socket\n", to.u.s.sa_family, p->proto);
341341
ser_error=E_NO_SOCKET;
342342
continue;
343343
}
@@ -402,7 +402,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
402402

403403

404404

405-
int update_sock_struct_from_via( union sockaddr_union* to,
405+
int update_sock_struct_from_via( struct sockaddr_union_struct* to,
406406
struct sip_msg* msg,
407407
struct via_body* via )
408408
{
@@ -468,7 +468,7 @@ int update_sock_struct_from_via( union sockaddr_union* to,
468468
int forward_reply(struct sip_msg* msg)
469469
{
470470
char* new_buf;
471-
union sockaddr_union* to;
471+
struct sockaddr_union_struct* to;
472472
unsigned int new_len;
473473
struct sr_module *mod;
474474
int proto;
@@ -513,7 +513,7 @@ int forward_reply(struct sip_msg* msg)
513513
goto error;
514514
}
515515

516-
to=(union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
516+
to=(struct sockaddr_union_struct*)pkg_malloc(sizeof(struct sockaddr_union_struct));
517517
if (to==0){
518518
LM_ERR("out of pkg memory\n");
519519
goto error;

forward.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
#include "socket_info.h"
4949

5050
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);
51+
struct sockaddr_union_struct* su, int proto);
52+
struct socket_info* get_out_socket(struct sockaddr_union_struct* 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);
55-
int update_sock_struct_from_via( union sockaddr_union* to,
55+
int update_sock_struct_from_via( struct sockaddr_union_struct* to,
5656
struct sip_msg* msg,
5757
struct via_body* via );
5858

@@ -82,7 +82,7 @@ int forward_reply( struct sip_msg* msg);
8282
* \return 0 if ok, -1 on error
8383
*/
8484
static inline int msg_send( struct socket_info* send_sock, int proto,
85-
union sockaddr_union* to, unsigned int id,
85+
struct sockaddr_union_struct* to, unsigned int id,
8686
char* buf, int len, struct sip_msg* msg)
8787
{
8888
str out_buff;

ip_addr.h

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ struct net{
8080
struct ip_addr mask;
8181
};
8282

83-
union sockaddr_union{
83+
struct sockaddr_union_struct{
84+
union {
8485
struct sockaddr s;
8586
struct sockaddr_in sin;
8687
struct sockaddr_in6 sin6;
88+
} u;
89+
char hostname[256]; /* remote side hostname (used for TLS certificate hostname verification) */
8790
};
8891

89-
90-
9192
enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
9293
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32 };
9394

@@ -99,7 +100,7 @@ struct receive_info {
99100
int proto;
100101
unsigned int proto_reserved1; /*!< tcp stores the connection id here */
101102
unsigned int proto_reserved2;
102-
union sockaddr_union src_su; /*!< useful for replies*/
103+
struct sockaddr_union_struct src_su; /*!< useful for replies*/
103104
struct socket_info* bind_address; /*!< sock_info structure on which the msg was received*/
104105
/* no need for dst_su yet */
105106
};
@@ -108,7 +109,7 @@ struct receive_info {
108109
struct dest_info {
109110
int proto;
110111
unsigned int proto_reserved1; /*!< tcp stores the connection id here */
111-
union sockaddr_union to;
112+
struct sockaddr_union_struct to;
112113
struct socket_info* send_sock;
113114
};
114115

@@ -130,10 +131,10 @@ struct socket_id {
130131

131132
/* len of the sockaddr */
132133
#ifdef HAVE_SOCKADDR_SA_LEN
133-
#define sockaddru_len(su) ((su).s.sa_len)
134+
#define sockaddru_len(su) ((su).u.s.sa_len)
134135
#else
135136
#define sockaddru_len(su) \
136-
(((su).s.sa_family==AF_INET6)?sizeof(struct sockaddr_in6):\
137+
(((su).u.s.sa_family==AF_INET6)?sizeof(struct sockaddr_in6):\
137138
sizeof(struct sockaddr_in))
138139
#endif /* HAVE_SOCKADDR_SA_LEN*/
139140

@@ -154,7 +155,7 @@ struct socket_id {
154155
struct ip_addr __ip; \
155156
sockaddr2ip_addr( &__ip, (struct sockaddr*)_su ); \
156157
_ip_char = ip_addr2a(&__ip); \
157-
_port_no = su_getport( (union sockaddr_union*)(void *)_su); \
158+
_port_no = su_getport( (struct sockaddr_union_struct*)(void *)_su); \
158159
} while(0)
159160

160161

@@ -246,73 +247,73 @@ static inline void sockaddr2ip_addr(struct ip_addr* ip, struct sockaddr* sa)
246247

247248

248249
/*! \brief compare 2 sockaddr_unions */
249-
static inline int su_cmp(union sockaddr_union* s1, union sockaddr_union* s2)
250+
static inline int su_cmp(struct sockaddr_union_struct* s1, struct sockaddr_union_struct* s2)
250251
{
251-
if (s1->s.sa_family!=s2->s.sa_family) return 0;
252-
switch(s1->s.sa_family){
252+
if (s1->u.s.sa_family!=s2->u.s.sa_family) return 0;
253+
switch(s1->u.s.sa_family){
253254
case AF_INET:
254-
return (s1->sin.sin_port==s2->sin.sin_port)&&
255-
(memcmp(&s1->sin.sin_addr, &s2->sin.sin_addr, 4)==0);
255+
return (s1->u.sin.sin_port==s2->u.sin.sin_port)&&
256+
(memcmp(&s1->u.sin.sin_addr, &s2->u.sin.sin_addr, 4)==0);
256257
case AF_INET6:
257-
return (s1->sin6.sin6_port==s2->sin6.sin6_port)&&
258-
(memcmp(&s1->sin6.sin6_addr, &s2->sin6.sin6_addr, 16)==0);
258+
return (s1->u.sin6.sin6_port==s2->u.sin6.sin6_port)&&
259+
(memcmp(&s1->u.sin6.sin6_addr, &s2->u.sin6.sin6_addr, 16)==0);
259260
default:
260261
LM_CRIT("unknown address family %d\n",
261-
s1->s.sa_family);
262+
s1->u.s.sa_family);
262263
return 0;
263264
}
264265
}
265266

266267

267268

268269
/*! \brief gets the port number (host byte order) */
269-
static inline unsigned short su_getport(union sockaddr_union* su)
270+
static inline unsigned short su_getport(struct sockaddr_union_struct* su)
270271
{
271272
if(su==0)
272273
return 0;
273274

274-
switch(su->s.sa_family){
275+
switch(su->u.s.sa_family){
275276
case AF_INET:
276-
return ntohs(su->sin.sin_port);
277+
return ntohs(su->u.sin.sin_port);
277278
case AF_INET6:
278-
return ntohs(su->sin6.sin6_port);
279+
return ntohs(su->u.sin6.sin6_port);
279280
default:
280-
LM_CRIT("unknown address family %d\n", su->s.sa_family);
281+
LM_CRIT("unknown address family %d\n", su->u.s.sa_family);
281282
return 0;
282283
}
283284
}
284285

285286
/*! \brief sets the port number (host byte order) */
286-
static inline void su_setport(union sockaddr_union* su, unsigned short port)
287+
static inline void su_setport(struct sockaddr_union_struct* su, unsigned short port)
287288
{
288-
switch(su->s.sa_family){
289+
switch(su->u.s.sa_family){
289290
case AF_INET:
290-
su->sin.sin_port=htons(port);
291+
su->u.sin.sin_port=htons(port);
291292
break;
292293
case AF_INET6:
293-
su->sin6.sin6_port=htons(port);
294+
su->u.sin6.sin6_port=htons(port);
294295
break;
295296
default:
296-
LM_CRIT("unknown address family %d\n", su->s.sa_family);
297+
LM_CRIT("unknown address family %d\n", su->u.s.sa_family);
297298
}
298299
}
299300

300301
/*! \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)
302+
static inline void su2ip_addr(struct ip_addr* ip, struct sockaddr_union_struct* su)
302303
{
303-
switch(su->s.sa_family){
304+
switch(su->u.s.sa_family){
304305
case AF_INET:
305306
ip->af=AF_INET;
306307
ip->len=4;
307-
memcpy(ip->u.addr, &su->sin.sin_addr, 4);
308+
memcpy(ip->u.addr, &su->u.sin.sin_addr, 4);
308309
break;
309310
case AF_INET6:
310311
ip->af=AF_INET6;
311312
ip->len=16;
312-
memcpy(ip->u.addr, &su->sin6.sin6_addr, 16);
313+
memcpy(ip->u.addr, &su->u.sin6.sin6_addr, 16);
313314
break;
314315
default:
315-
LM_CRIT("Unknown address family %d\n", su->s.sa_family);
316+
LM_CRIT("Unknown address family %d\n", su->u.s.sa_family);
316317
ip->af=0;
317318
ip->len=0;
318319
}
@@ -325,26 +326,26 @@ static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
325326
/*! \brief inits a struct sockaddr_union from a struct ip_addr and a port no
326327
* \return 0 if ok, -1 on error (unknown address family)
327328
* \note the port number is in host byte order */
328-
static inline int init_su( union sockaddr_union* su,
329+
static inline int init_su( struct sockaddr_union_struct* su,
329330
struct ip_addr* ip,
330331
unsigned short port )
331332
{
332-
memset(su, 0, sizeof(union sockaddr_union));/*needed on freebsd*/
333-
su->s.sa_family=ip->af;
333+
memset(su, 0, sizeof(struct sockaddr_union_struct));/*needed on freebsd*/
334+
su->u.s.sa_family=ip->af;
334335
switch(ip->af){
335336
case AF_INET6:
336-
memcpy(&su->sin6.sin6_addr, ip->u.addr, ip->len);
337+
memcpy(&su->u.sin6.sin6_addr, ip->u.addr, ip->len);
337338
#ifdef HAVE_SOCKADDR_SA_LEN
338-
su->sin6.sin6_len=sizeof(struct sockaddr_in6);
339+
su->u.sin6.sin6_len=sizeof(struct sockaddr_in6);
339340
#endif
340-
su->sin6.sin6_port=htons(port);
341+
su->u.sin6.sin6_port=htons(port);
341342
break;
342343
case AF_INET:
343-
memcpy(&su->sin.sin_addr, ip->u.addr, ip->len);
344+
memcpy(&su->u.sin.sin_addr, ip->u.addr, ip->len);
344345
#ifdef HAVE_SOCKADDR_SA_LEN
345-
su->sin.sin_len=sizeof(struct sockaddr_in);
346+
su->u.sin.sin_len=sizeof(struct sockaddr_in);
346347
#endif
347-
su->sin.sin_port=htons(port);
348+
su->u.sin.sin_port=htons(port);
348349
break;
349350
default:
350351
LM_CRIT("unknown address family %d\n", ip->af);
@@ -359,27 +360,27 @@ static inline int init_su( union sockaddr_union* su,
359360
* the hostent structure and a port no. (host byte order)
360361
* WARNING: no index overflow checks!
361362
* \return 0 if ok, -1 on error (unknown address family) */
362-
static inline int hostent2su( union sockaddr_union* su,
363+
static inline int hostent2su( struct sockaddr_union_struct* su,
363364
struct hostent* he,
364365
unsigned int idx,
365366
unsigned short port )
366367
{
367-
memset(su, 0, sizeof(union sockaddr_union)); /*needed on freebsd*/
368-
su->s.sa_family=he->h_addrtype;
368+
memset(su, 0, sizeof(struct sockaddr_union_struct)); /*needed on freebsd*/
369+
su->u.s.sa_family=he->h_addrtype;
369370
switch(he->h_addrtype){
370371
case AF_INET6:
371-
memcpy(&su->sin6.sin6_addr, he->h_addr_list[idx], he->h_length);
372+
memcpy(&su->u.sin6.sin6_addr, he->h_addr_list[idx], he->h_length);
372373
#ifdef HAVE_SOCKADDR_SA_LEN
373-
su->sin6.sin6_len=sizeof(struct sockaddr_in6);
374+
su->u.sin6.sin6_len=sizeof(struct sockaddr_in6);
374375
#endif
375-
su->sin6.sin6_port=htons(port);
376+
su->u.sin6.sin6_port=htons(port);
376377
break;
377378
case AF_INET:
378-
memcpy(&su->sin.sin_addr, he->h_addr_list[idx], he->h_length);
379+
memcpy(&su->u.sin.sin_addr, he->h_addr_list[idx], he->h_length);
379380
#ifdef HAVE_SOCKADDR_SA_LEN
380-
su->sin.sin_len=sizeof(struct sockaddr_in);
381+
su->u.sin.sin_len=sizeof(struct sockaddr_in);
381382
#endif
382-
su->sin.sin_port=htons(port);
383+
su->u.sin.sin_port=htons(port);
383384
break;
384385
default:
385386
LM_CRIT("unknown address family %d\n", he->h_addrtype);

0 commit comments

Comments
 (0)