Browse code

core: helper function to get address family name

Daniel-Constantin Mierla authored on 16/09/2022 14:42:16
Showing 1 changed files
... ...
@@ -304,6 +304,8 @@ char* get_proto_name(unsigned int proto);
304 304
 int get_valid_proto_string(unsigned int iproto, int utype, int vtype,
305 305
 		str *sproto);
306 306
 
307
+char* get_af_name(unsigned int af);
308
+
307 309
 #ifdef USE_MCAST
308 310
 /* Returns 1 if the given address is a multicast address */
309 311
 int is_mcast(struct ip_addr* ip);
Browse code

core: new socket global parameter to set listen attributes with a structure style

- alternative to listen when a usual bind address is provided
- example:

socket = {
bind = udp:127.0.0.1:5060;
advertise = 1.2.3.4:5080;
name = "udp0";
}

Daniel-Constantin Mierla authored on 01/06/2022 06:24:50
Showing 1 changed files
... ...
@@ -134,6 +134,16 @@ typedef struct socket_info {
134 134
 #endif /* USE_MCAST */
135 135
 } socket_info_t;
136 136
 
137
+typedef struct socket_attrs {
138
+	int bindproto;
139
+	str bindaddr;
140
+	int bindport;
141
+	str useaddr;
142
+	int useport;
143
+	str sockname;
144
+	int workers;
145
+	int sflags;
146
+} socket_attrs_t;
137 147
 
138 148
 /* send flags */
139 149
 typedef enum send_flags {
Browse code

core: const correctness

benign const qualifiers in inline functions

Liviu Andron authored on 20/05/2022 10:30:46 • Henning Westerholt committed on 20/05/2022 16:38:54
Showing 1 changed files
... ...
@@ -301,7 +301,7 @@ int is_mcast(struct ip_addr* ip);
301 301
 
302 302
 /* returns 1 if the given ip address is INADDR_ANY or IN6ADDR_ANY,
303 303
  * 0 otherwise */
304
-inline static int ip_addr_any(struct ip_addr* ip)
304
+inline static int ip_addr_any(const struct ip_addr* ip)
305 305
 {
306 306
 	int r;
307 307
 	int l;
... ...
@@ -316,7 +316,7 @@ inline static int ip_addr_any(struct ip_addr* ip)
316 316
 
317 317
 /* returns 1 if the given ip address is a loopback address
318 318
  * 0 otherwise */
319
-inline static int ip_addr_loopback(struct ip_addr* ip)
319
+inline static int ip_addr_loopback(const struct ip_addr* ip)
320 320
 {
321 321
 	if (ip->af==AF_INET)
322 322
 		return ip->u.addr32[0]==htonl(INADDR_LOOPBACK);
... ...
@@ -352,7 +352,7 @@ inline static void ip_addr_mk_any(int af, struct ip_addr* ip)
352 352
 
353 353
 /* returns 1 if ip & net.mask == net.ip ; 0 otherwise & -1 on error
354 354
  * [ diff. address families ]) */
355
-inline static int matchnet(struct ip_addr* ip, struct net* net)
355
+inline static int matchnet(const struct ip_addr* ip, const struct net* net)
356 356
 {
357 357
 	unsigned int r;
358 358
 
... ...
@@ -371,7 +371,7 @@ inline static int matchnet(struct ip_addr* ip, struct net* net)
371 371
 
372 372
 
373 373
 /* inits an ip_addr pointer from a sockaddr structure*/
374
-static inline void sockaddr2ip_addr(struct ip_addr* ip, struct sockaddr* sa)
374
+static inline void sockaddr2ip_addr(struct ip_addr* ip, const struct sockaddr* sa)
375 375
 {
376 376
 	switch(sa->sa_family){
377 377
 		case AF_INET:
... ...
@@ -447,7 +447,7 @@ static inline void su_setport(union sockaddr_union* su, unsigned short port)
447 447
 
448 448
 
449 449
 /* inits an ip_addr pointer from a sockaddr_union ip address */
450
-static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
450
+static inline void su2ip_addr(struct ip_addr* ip, const union sockaddr_union* su)
451 451
 {
452 452
 	switch(su->s.sa_family){
453 453
 		case AF_INET:
... ...
@@ -595,7 +595,7 @@ struct hostent* ip_addr2he(str* name, struct ip_addr* ip);
595 595
 
596 596
 /* init a dest_info structure from a recv_info structure */
597 597
 inline static void init_dst_from_rcv(struct dest_info* dst,
598
-		struct receive_info* rcv)
598
+		const struct receive_info* rcv)
599 599
 {
600 600
 	dst->send_sock=rcv->bind_address;
601 601
 	dst->to=rcv->src_su;
Browse code

core: enable updating received data via $rcv(buf)

- on event_route[core:msg-received]

Daniel-Constantin Mierla authored on 08/04/2022 17:15:26
Showing 1 changed files
... ...
@@ -203,6 +203,7 @@ typedef struct ksr_coninfo {
203 203
 
204 204
 typedef struct sr_net_info {
205 205
 	str data;
206
+	unsigned int bufsize;
206 207
 	receive_info_t* rcv;
207 208
 	dest_info_t* dst;
208 209
 } sr_net_info_t;
Browse code

core: count and store global index for listen sockets at startup

Daniel-Constantin Mierla authored on 06/04/2022 11:54:59
Showing 1 changed files
... ...
@@ -110,6 +110,7 @@ typedef struct advertise_info {
110 110
 
111 111
 typedef struct socket_info {
112 112
 	int socket;
113
+	int gindex; /* global index in the lists of all sockets */
113 114
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
114 115
 	struct ip_addr address; /* ip address */
115 116
 	str address_str;        /*ip address converted to string -- optimization*/
Browse code

core: listen can now have a "virtual" flag to check for nonlocal floating IPs.

A new option to "listen" has been added called "virtual". This sets a flag on the listening socket to modify the behaviour of grep_sock_info.
When this flag is set, grep_sock_info will only consider the listening IP a match if the IP is found in the system's current list of
local IP addresses. If the IP is not currently local, then the matching IP is ignored.
If the virtual flag is not set on the socket then existing behaviour used instead.

This is useful in scenarios with an active/active cluster where Kamailio must know if a floating IP is currently local or not.

Rhys Hanrahan authored on 22/12/2021 11:13:47 • Daniel-Constantin Mierla committed on 07/01/2022 11:27:25
Showing 1 changed files
... ...
@@ -84,6 +84,7 @@ typedef enum si_flags {
84 84
 	SI_IS_MCAST     = (1<<2),
85 85
 	SI_IS_ANY       = (1<<3),
86 86
 	SI_IS_MHOMED    = (1<<4),
87
+	SI_IS_VIRTUAL	= (1<<5),
87 88
 } si_flags_t;
88 89
 
89 90
 typedef struct addr_info {
Browse code

core: ip addr - use POSIX INET6_ADDRSTRLEN and INET_ADDRSTRLEN to define sizes

- set the values of IP4_MAX_STR_SIZE and IP6_MAX_STR_SIZE based on them,
if defined, or to 15 and 45
- cope with IPv6 with IPv4 tunneling representation (RFC 4291 section 2.2)
- cover the requirements of using inet_ntop()
- GH #2823

Daniel-Constantin Mierla authored on 13/08/2021 06:57:10
Showing 1 changed files
... ...
@@ -510,8 +510,20 @@ int hostent2su(union sockaddr_union* su,
510 510
 
511 511
 
512 512
 /* maximum size of a str returned by ip_addr2str */
513
-#define IP6_MAX_STR_SIZE 39 /*1234:5678:9012:3456:7890:1234:5678:9012*/
514
-#define IP4_MAX_STR_SIZE 15 /*123.456.789.012*/
513
+/* POSIX INET6_ADDRSTRLEN (RFC 4291 section 2.2) - IPv6 with IPv4 tunneling
514
+ * (39): 1234:5678:9012:3456:7890:1234:5678:9012
515
+ * (45): ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255 */
516
+#ifdef INET6_ADDRSTRLEN
517
+#define IP6_MAX_STR_SIZE (INET6_ADDRSTRLEN-1)
518
+#else
519
+#define IP6_MAX_STR_SIZE 45
520
+#endif
521
+/*123.456.789.123*/
522
+#ifdef INET_ADDRSTRLEN
523
+#define IP4_MAX_STR_SIZE (INET_ADDRSTRLEN-1)
524
+#else
525
+#define IP4_MAX_STR_SIZE 15
526
+#endif
515 527
 
516 528
 /* converts a raw ipv6 addr (16 bytes) to ascii */
517 529
 int ip6tosbuf(unsigned char* ip6, char* buff, int len);
Browse code

core: new event_route[core:pre-routing]

- executed before running config script for SIP messages, if received
from the network, it is not executed if the message was dispatched
internally
- can be used to delegate processing to special workers via sworker
module
- if drop is used, then processing stops for that message, otherwise it
goes forther to request_route or reply_route

Daniel-Constantin Mierla authored on 26/11/2020 12:23:05
Showing 1 changed files
... ...
@@ -147,7 +147,8 @@ typedef struct snd_flags {
147 147
 
148 148
 /* recv flags */
149 149
 typedef enum recv_flags {
150
-	RECV_F_INTERNAL   = (1 << 0), /*  */
150
+	RECV_F_INTERNAL     = (1 << 0), /* message dispatched internally */
151
+	RECV_F_PREROUTING   = (1 << 1), /* message in pre-routing */
151 152
 } recv_flags_t;
152 153
 
153 154
 typedef struct receive_info {
Browse code

core: added flags field for received info structure

Daniel-Constantin Mierla authored on 26/11/2020 08:44:31
Showing 1 changed files
... ...
@@ -145,6 +145,10 @@ typedef struct snd_flags {
145 145
 	unsigned short blst_imask; /* blocklist ignore mask */
146 146
 } snd_flags_t;
147 147
 
148
+/* recv flags */
149
+typedef enum recv_flags {
150
+	RECV_F_INTERNAL   = (1 << 0), /*  */
151
+} recv_flags_t;
148 152
 
149 153
 typedef struct receive_info {
150 154
 	struct ip_addr src_ip;
... ...
@@ -156,6 +160,7 @@ typedef struct receive_info {
156 160
 	union sockaddr_union src_su; /* useful for replies*/
157 161
 	struct socket_info* bind_address; /* sock_info structure on which
158 162
 										* the msg was received */
163
+	recv_flags_t rflags; /* flags */
159 164
 	char proto;
160 165
 #ifdef USE_COMP
161 166
 	char proto_pad0;  /* padding field */
Browse code

core: switch to use blocklist structures and functions

Daniel-Constantin Mierla authored on 30/10/2020 09:25:09
Showing 1 changed files
... ...
@@ -142,7 +142,7 @@ typedef enum send_flags {
142 142
 
143 143
 typedef struct snd_flags {
144 144
 	unsigned short f;          /* snd flags */
145
-	unsigned short blst_imask; /* blacklist ignore mask */
145
+	unsigned short blst_imask; /* blocklist ignore mask */
146 146
 } snd_flags_t;
147 147
 
148 148
 
Browse code

core: typedef'ed several structs, enums and unions

Daniel-Constantin Mierla authored on 20/10/2020 10:36:55
Showing 1 changed files
... ...
@@ -43,12 +43,12 @@
43 43
 
44 44
 extern str ksr_ipv6_hex_style;
45 45
 
46
-enum sip_protos { PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP,
47
-	PROTO_WS, PROTO_WSS, PROTO_OTHER };
46
+typedef enum sip_protos { PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP,
47
+	PROTO_WS, PROTO_WSS, PROTO_OTHER } sip_protos_t;
48 48
 #define PROTO_LAST PROTO_OTHER
49 49
 
50 50
 #ifdef USE_COMP
51
-enum comp_methods { COMP_NONE, COMP_SIGCOMP, COMP_SERGZ };
51
+typedef enum comp_methods { COMP_NONE, COMP_SIGCOMP, COMP_SERGZ } comp_methods_t;
52 52
 #endif
53 53
 
54 54
 typedef struct ip_addr {
... ...
@@ -69,22 +69,22 @@ typedef struct net {
69 69
 	struct ip_addr mask;
70 70
 } sr_net_t;
71 71
 
72
-union sockaddr_union{
72
+typedef union sockaddr_union{
73 73
 	struct sockaddr     s;
74 74
 	struct sockaddr_in  sin;
75 75
 	struct sockaddr_in6 sin6;
76 76
 	struct sockaddr_storage sas;
77
-};
77
+} sr_sockaddr_union_t;
78 78
 
79 79
 
80
-enum si_flags {
80
+typedef enum si_flags {
81 81
 	SI_NONE         = 0,
82 82
 	SI_IS_IP        = (1<<0),
83 83
 	SI_IS_LO        = (1<<1),
84 84
 	SI_IS_MCAST     = (1<<2),
85 85
 	SI_IS_ANY       = (1<<3),
86 86
 	SI_IS_MHOMED    = (1<<4),
87
-};
87
+} si_flags_t;
88 88
 
89 89
 typedef struct addr_info {
90 90
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
... ...
@@ -134,11 +134,11 @@ typedef struct socket_info {
134 134
 
135 135
 
136 136
 /* send flags */
137
-enum send_flags {
137
+typedef enum send_flags {
138 138
 	SND_F_FORCE_CON_REUSE   = (1 << 0), /* reuse an existing connection or fail */
139 139
 	SND_F_CON_CLOSE         = (1 << 1), /* close the connection after sending */
140 140
 	SND_F_FORCE_SOCKET      = (1 << 2), /* send socket in dst is forced */
141
-};
141
+} send_flags_t;
142 142
 
143 143
 typedef struct snd_flags {
144 144
 	unsigned short f;          /* snd flags */
Browse code

core/ip_addr: Convert SND_F_ flags to enum

Easier to recognize the correct flags in the forest of flags.

Alex Hermann authored on 09/06/2020 07:16:16 • Henning Westerholt committed on 20/10/2020 10:17:10
Showing 1 changed files
... ...
@@ -134,9 +134,11 @@ typedef struct socket_info {
134 134
 
135 135
 
136 136
 /* send flags */
137
-#define SND_F_FORCE_CON_REUSE	1 /* reuse an existing connection or fail */
138
-#define SND_F_CON_CLOSE			2 /* close the connection after sending */
139
-#define SND_F_FORCE_SOCKET		4 /* send socket in dst is forced */
137
+enum send_flags {
138
+	SND_F_FORCE_CON_REUSE   = (1 << 0), /* reuse an existing connection or fail */
139
+	SND_F_CON_CLOSE         = (1 << 1), /* close the connection after sending */
140
+	SND_F_FORCE_SOCKET      = (1 << 2), /* send socket in dst is forced */
141
+};
140 142
 
141 143
 typedef struct snd_flags {
142 144
 	unsigned short f;          /* snd flags */
Browse code

core/ip_addr: Reformat and redefine si_flags as bitshifts

Make it a bit easier to read

Alex Hermann authored on 09/06/2020 07:15:26 • Henning Westerholt committed on 20/10/2020 10:17:10
Showing 1 changed files
... ...
@@ -77,8 +77,14 @@ union sockaddr_union{
77 77
 };
78 78
 
79 79
 
80
-enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
81
-	SI_IS_ANY=8, SI_IS_MHOMED=16 };
80
+enum si_flags {
81
+	SI_NONE         = 0,
82
+	SI_IS_IP        = (1<<0),
83
+	SI_IS_LO        = (1<<1),
84
+	SI_IS_MCAST     = (1<<2),
85
+	SI_IS_ANY       = (1<<3),
86
+	SI_IS_MHOMED    = (1<<4),
87
+};
82 88
 
83 89
 typedef struct addr_info {
84 90
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
Browse code

core: relocation and refactoring on ip addr handling code

- many static inline functions moved from ip_addr.h to ip_addr.c, code
size for some of them increased over the time
- ipv6_hex_style can be set to "c" to have IPv6 addresses compacted and
using lowercase hexdigits (rfc5952#section-4.2.1), GH #2510
- when ipv6_hex_style="c", ip6tosbuf() uses POSIX inet_ntop(),
conforming with POSIX.1-2001, POSIX.1-2008

Daniel-Constantin Mierla authored on 19/10/2020 10:32:43
Showing 1 changed files
... ...
@@ -41,7 +41,7 @@
41 41
 
42 42
 #include "dprint.h"
43 43
 
44
-extern char *ksr_ipv6_hex_style;
44
+extern str ksr_ipv6_hex_style;
45 45
 
46 46
 enum sip_protos { PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP,
47 47
 	PROTO_WS, PROTO_WSS, PROTO_OTHER };
... ...
@@ -489,34 +489,10 @@ static inline int init_su( union sockaddr_union* su,
489 489
  * the hostent structure and a port no. (host byte order)
490 490
  * WARNING: no index overflow  checks!
491 491
  * returns 0 if ok, -1 on error (unknown address family) */
492
-static inline int hostent2su( union sockaddr_union* su,
492
+int hostent2su(union sockaddr_union* su,
493 493
 		struct hostent* he,
494 494
 		unsigned int idx,
495
-		unsigned short port )
496
-{
497
-	memset(su, 0, sizeof(union sockaddr_union)); /*needed on freebsd*/
498
-	su->s.sa_family=he->h_addrtype;
499
-	switch(he->h_addrtype){
500
-		case	AF_INET6:
501
-			memcpy(&su->sin6.sin6_addr, he->h_addr_list[idx], he->h_length);
502
-#ifdef HAVE_SOCKADDR_SA_LEN
503
-			su->sin6.sin6_len=sizeof(struct sockaddr_in6);
504
-#endif
505
-			su->sin6.sin6_port=htons(port);
506
-			break;
507
-		case AF_INET:
508
-			memcpy(&su->sin.sin_addr, he->h_addr_list[idx], he->h_length);
509
-#ifdef HAVE_SOCKADDR_SA_LEN
510
-			su->sin.sin_len=sizeof(struct sockaddr_in);
511
-#endif
512
-			su->sin.sin_port=htons(port);
513
-			break;
514
-		default:
515
-			LM_CRIT("unknown address family %d\n", he->h_addrtype);
516
-			return -1;
517
-	}
518
-	return 0;
519
-}
495
+		unsigned short port);
520 496
 
521 497
 
522 498
 /* maximum size of a str returned by ip_addr2str */
... ...
@@ -524,130 +500,11 @@ static inline int hostent2su( union sockaddr_union* su,
524 500
 #define IP4_MAX_STR_SIZE 15 /*123.456.789.012*/
525 501
 
526 502
 /* converts a raw ipv6 addr (16 bytes) to ascii */
527
-static inline int ip6tosbuf(unsigned char* ip6, char* buff, int len)
528
-{
529
-	int offset;
530
-	register unsigned char a,b,c;
531
-	register unsigned char d;
532
-	register unsigned short hex4;
533
-	int r;
534
-
535
-#define HEXDIG(x) (((x)>=10)?(x)-10+ksr_ipv6_hex_style[0]:(x)+'0')
536
-
537
-	offset=0;
538
-	if (unlikely(len<IP6_MAX_STR_SIZE))
539
-		return 0;
540
-	for(r=0;r<7;r++){
541
-		hex4=((unsigned char)ip6[r*2]<<8)+(unsigned char)ip6[r*2+1];
542
-		a=hex4>>12;
543
-		b=(hex4>>8)&0xf;
544
-		c=(hex4>>4)&0xf;
545
-		d=hex4&0xf;
546
-		if (a){
547
-			buff[offset]=HEXDIG(a);
548
-			buff[offset+1]=HEXDIG(b);
549
-			buff[offset+2]=HEXDIG(c);
550
-			buff[offset+3]=HEXDIG(d);
551
-			buff[offset+4]=':';
552
-			offset+=5;
553
-		}else if(b){
554
-			buff[offset]=HEXDIG(b);
555
-			buff[offset+1]=HEXDIG(c);
556
-			buff[offset+2]=HEXDIG(d);
557
-			buff[offset+3]=':';
558
-			offset+=4;
559
-		}else if(c){
560
-			buff[offset]=HEXDIG(c);
561
-			buff[offset+1]=HEXDIG(d);
562
-			buff[offset+2]=':';
563
-			offset+=3;
564
-		}else{
565
-			buff[offset]=HEXDIG(d);
566
-			buff[offset+1]=':';
567
-			offset+=2;
568
-		}
569
-	}
570
-	/* last int16*/
571
-	hex4=((unsigned char)ip6[r*2]<<8)+(unsigned char)ip6[r*2+1];
572
-	a=hex4>>12;
573
-	b=(hex4>>8)&0xf;
574
-	c=(hex4>>4)&0xf;
575
-	d=hex4&0xf;
576
-	if (a){
577
-		buff[offset]=HEXDIG(a);
578
-		buff[offset+1]=HEXDIG(b);
579
-		buff[offset+2]=HEXDIG(c);
580
-		buff[offset+3]=HEXDIG(d);
581
-		offset+=4;
582
-	}else if(b){
583
-		buff[offset]=HEXDIG(b);
584
-		buff[offset+1]=HEXDIG(c);
585
-		buff[offset+2]=HEXDIG(d);
586
-		offset+=3;
587
-	}else if(c){
588
-		buff[offset]=HEXDIG(c);
589
-		buff[offset+1]=HEXDIG(d);
590
-		offset+=2;
591
-	}else{
592
-		buff[offset]=HEXDIG(d);
593
-		offset+=1;
594
-	}
595
-
596
-	return offset;
597
-}
503
+int ip6tosbuf(unsigned char* ip6, char* buff, int len);
598 504
 
599 505
 
600 506
 /* converts a raw ipv4 addr (4 bytes) to ascii */
601
-static inline int ip4tosbuf(unsigned char* ip4, char* buff, int len)
602
-{
603
-	int offset;
604
-	register unsigned char a,b,c;
605
-	int r;
606
-
607
-	offset=0;
608
-	if (unlikely(len<IP4_MAX_STR_SIZE))
609
-		return 0;
610
-	for(r=0;r<3;r++){
611
-		a=(unsigned char)ip4[r]/100;
612
-		c=(unsigned char)ip4[r]%10;
613
-		b=(unsigned char)ip4[r]%100/10;
614
-		if (a){
615
-			buff[offset]=a+'0';
616
-			buff[offset+1]=b+'0';
617
-			buff[offset+2]=c+'0';
618
-			buff[offset+3]='.';
619
-			offset+=4;
620
-		}else if (b){
621
-			buff[offset]=b+'0';
622
-			buff[offset+1]=c+'0';
623
-			buff[offset+2]='.';
624
-			offset+=3;
625
-		}else{
626
-			buff[offset]=c+'0';
627
-			buff[offset+1]='.';
628
-			offset+=2;
629
-		}
630
-	}
631
-	/* last number */
632
-	a=(unsigned char)ip4[r]/100;
633
-	c=(unsigned char)ip4[r]%10;
634
-	b=(unsigned char)ip4[r]%100/10;
635
-	if (a){
636
-		buff[offset]=a+'0';
637
-		buff[offset+1]=b+'0';
638
-		buff[offset+2]=c+'0';
639
-		offset+=3;
640
-	}else if (b){
641
-		buff[offset]=b+'0';
642
-		buff[offset+1]=c+'0';
643
-		offset+=2;
644
-	}else{
645
-		buff[offset]=c+'0';
646
-		offset+=1;
647
-	}
648
-
649
-	return offset;
650
-}
507
+int ip4tosbuf(unsigned char* ip4, char* buff, int len);
651 508
 
652 509
 
653 510
 /* fast ip_addr -> string converter;
... ...
@@ -657,170 +514,47 @@ static inline int ip4tosbuf(unsigned char* ip4, char* buff, int len)
657 514
  *  will return error (no detailed might fit checks are made, for example
658 515
  *   if len==7 the function will fail even for 1.2.3.4).
659 516
  */
660
-static inline int ip_addr2sbuf(struct ip_addr* ip, char* buff, int len)
661
-{
662
-	switch(ip->af){
663
-		case AF_INET6:
664
-			return ip6tosbuf(ip->u.addr, buff, len);
665
-			break;
666
-		case AF_INET:
667
-			return ip4tosbuf(ip->u.addr, buff, len);
668
-			break;
669
-		default:
670
-			LM_CRIT("unknown address family %d\n", ip->af);
671
-			return 0;
672
-	}
673
-}
517
+int ip_addr2sbuf(struct ip_addr* ip, char* buff, int len);
518
+
674 519
 
675 520
 /* same as ip_addr2sbuf, but with [  ] around IPv6 addresses */
676
-static inline int ip_addr2sbufz(struct ip_addr* ip, char* buff, int len)
677
-{
678
-	char *p;
679
-	int sz;
521
+int ip_addr2sbufz(struct ip_addr* ip, char* buff, int len);
680 522
 
681
-	p = buff;
682
-	switch(ip->af){
683
-		case AF_INET6:
684
-			*p++ = '[';
685
-			sz = ip6tosbuf(ip->u.addr, p, len-2);
686
-			p += sz;
687
-			*p++ = ']';
688
-			*p=0;
689
-			return sz + 2;
690
-			break;
691
-		case AF_INET:
692
-			return ip4tosbuf(ip->u.addr, buff, len);
693
-			break;
694
-		default:
695
-			LM_CRIT("unknown address family %d\n", ip->af);
696
-			return 0;
697
-	}
698
-}
699 523
 
700 524
 /* maximum size of a str returned by ip_addr2a (including \0) */
701 525
 #define IP_ADDR_MAX_STR_SIZE (IP6_MAX_STR_SIZE+1) /* ip62ascii +  \0*/
702 526
 #define IP_ADDR_MAX_STRZ_SIZE (IP6_MAX_STR_SIZE+3) /* ip62ascii + [ + ] + \0*/
527
+
703 528
 /* fast ip_addr -> string converter;
704 529
  * it uses an internal buffer
705 530
  */
706
-static inline char* ip_addr2a(struct ip_addr* ip)
707
-{
708
-	static char buff[IP_ADDR_MAX_STR_SIZE];
709
-	int len;
710
-
711
-	len=ip_addr2sbuf(ip, buff, sizeof(buff)-1);
712
-	buff[len]=0;
713
-
714
-	return buff;
715
-}
531
+char* ip_addr2a(struct ip_addr* ip);
716 532
 
717 533
 
718 534
 /* full address in text representation, including [] for ipv6 */
719
-static inline char* ip_addr2strz(struct ip_addr* ip)
720
-{
535
+char* ip_addr2strz(struct ip_addr* ip);
721 536
 
722
-	static char buff[IP_ADDR_MAX_STRZ_SIZE];
723
-	char *p;
724
-	int len;
725
-
726
-	p = buff;
727
-	if(ip->af==AF_INET6) {
728
-		*p++ = '[';
729
-	}
730
-	len=ip_addr2sbuf(ip, p, sizeof(buff)-3);
731
-	p += len;
732
-	if(ip->af==AF_INET6) {
733
-		*p++ = ']';
734
-	}
735
-	*p=0;
736
-
737
-	return buff;
738
-}
739 537
 
740 538
 #define SU2A_MAX_STR_SIZE  (IP6_MAX_STR_SIZE + 2 /* [] */+\
741 539
 		1 /* : */ + USHORT2SBUF_MAX_LEN + 1 /* \0 */)
742 540
 
541
+
743 542
 /* returns an asciiz string containing the ip and the port
744 543
  *  (<ip_addr>:port or [<ipv6_addr>]:port)
745 544
  */
746
-static inline char* su2a(union sockaddr_union* su, int su_len)
747
-{
748
-	static char buf[SU2A_MAX_STR_SIZE];
749
-	int offs;
750
-
751
-	if (unlikely(su->s.sa_family==AF_INET6)){
752
-		if (unlikely(su_len<sizeof(su->sin6)))
753
-			return "<addr. error>";
754
-		buf[0]='[';
755
-		offs=1+ip6tosbuf((unsigned char*)su->sin6.sin6_addr.s6_addr, &buf[1],
756
-				sizeof(buf)-4);
757
-		buf[offs]=']';
758
-		offs++;
759
-	}else
760
-		if (unlikely(su_len<sizeof(su->sin)))
761
-			return "<addr. error>";
762
-		else
763
-			offs=ip4tosbuf((unsigned char*)&su->sin.sin_addr, buf, sizeof(buf)-2);
764
-	buf[offs]=':';
765
-	offs+=1+ushort2sbuf(su_getport(su), &buf[offs+1], sizeof(buf)-(offs+1)-1);
766
-	buf[offs]=0;
767
-	return buf;
768
-}
545
+char* su2a(union sockaddr_union* su, int su_len);
769 546
 
770 547
 #define SUIP2A_MAX_STR_SIZE  (IP6_MAX_STR_SIZE + 2 /* [] */ + 1 /* \0 */)
548
+
771 549
 /* returns an asciiz string containing the ip
772 550
  *  (<ipv4_addr> or [<ipv6_addr>])
773 551
  */
774
-static inline char* suip2a(union sockaddr_union* su, int su_len)
775
-{
776
-	static char buf[SUIP2A_MAX_STR_SIZE];
777
-	int offs;
778
-
779
-	if (unlikely(su->s.sa_family==AF_INET6)){
780
-		if (unlikely(su_len<sizeof(su->sin6)))
781
-			return "<addr. error>";
782
-		buf[0]='[';
783
-		offs=1+ip6tosbuf((unsigned char*)su->sin6.sin6_addr.s6_addr, &buf[1],
784
-				IP6_MAX_STR_SIZE);
785
-		buf[offs]=']';
786
-		offs++;
787
-	}else
788
-		if (unlikely(su_len<sizeof(su->sin)))
789
-			return "<addr. error>";
790
-		else
791
-			offs=ip4tosbuf((unsigned char*)&su->sin.sin_addr, buf, IP4_MAX_STR_SIZE);
792
-	buf[offs]=0;
793
-	return buf;
794
-}
552
+char* suip2a(union sockaddr_union* su, int su_len);
795 553
 
796 554
 
797 555
 /* converts an ip_addr structure to a hostent, returns pointer to internal
798 556
  * statical structure */
799
-static inline struct hostent* ip_addr2he(str* name, struct ip_addr* ip)
800
-{
801
-	static struct hostent he;
802
-	static char hostname[256];
803
-	static char* p_aliases[1];
804
-	static char* p_addr[2];
805
-	static char address[16];
806
-	int len;
807
-
808
-	p_aliases[0]=0; /* no aliases*/
809
-	p_addr[1]=0; /* only one address*/
810
-	p_addr[0]=address;
811
-	len = (name->len<255)?name->len:255;
812
-	memcpy(hostname, name->s, len);
813
-	hostname[len] = '\0';
814
-	if (ip->len>16) return 0;
815
-	memcpy(address, ip->u.addr, ip->len);
816
-
817
-	he.h_addrtype=ip->af;
818
-	he.h_length=ip->len;
819
-	he.h_addr_list=p_addr;
820
-	he.h_aliases=p_aliases;
821
-	he.h_name=hostname;
822
-	return &he;
823
-}
557
+struct hostent* ip_addr2he(str* name, struct ip_addr* ip);
824 558
 
825 559
 
826 560
 /* init a dest_info structure */
Browse code

core: new parameter ipv6_hex_style

- can be set to "a" or "A" to specify if hex digits in local computed
ipv6 addresses are lowercase or uppercase. Default is "A" (same format
used so far).
- enables the ability to follow recommedations of RFC5952, section 4.3
- GH #2488

Daniel-Constantin Mierla authored on 30/09/2020 07:17:03
Showing 1 changed files
... ...
@@ -41,6 +41,8 @@
41 41
 
42 42
 #include "dprint.h"
43 43
 
44
+extern char *ksr_ipv6_hex_style;
45
+
44 46
 enum sip_protos { PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP,
45 47
 	PROTO_WS, PROTO_WSS, PROTO_OTHER };
46 48
 #define PROTO_LAST PROTO_OTHER
... ...
@@ -530,7 +532,7 @@ static inline int ip6tosbuf(unsigned char* ip6, char* buff, int len)
530 532
 	register unsigned short hex4;
531 533
 	int r;
532 534
 
533
-#define HEXDIG(x) (((x)>=10)?(x)-10+'A':(x)+'0')
535
+#define HEXDIG(x) (((x)>=10)?(x)-10+ksr_ipv6_hex_style[0]:(x)+'0')
534 536
 
535 537
 	offset=0;
536 538
 	if (unlikely(len<IP6_MAX_STR_SIZE))
Browse code

core: udp server - add sockaddr_storage to ensure enough size for sockaddr_union

- safety check that from addr len is matching expected size for listen
socket
- rename local variables to suggest better their purpose

Daniel-Constantin Mierla authored on 09/05/2020 09:00:20
Showing 1 changed files
... ...
@@ -71,6 +71,7 @@ union sockaddr_union{
71 71
 	struct sockaddr     s;
72 72
 	struct sockaddr_in  sin;
73 73
 	struct sockaddr_in6 sin6;
74
+	struct sockaddr_storage sas;
74 75
 };
75 76
 
76 77
 
Browse code

core: safer truncation of hostname using memcpy

Daniel-Constantin Mierla authored on 08/05/2020 19:28:01
Showing 1 changed files
... ...
@@ -800,11 +800,14 @@ static inline struct hostent* ip_addr2he(str* name, struct ip_addr* ip)
800 800
 	static char* p_aliases[1];
801 801
 	static char* p_addr[2];
802 802
 	static char address[16];
803
+	int len;
803 804
 
804 805
 	p_aliases[0]=0; /* no aliases*/
805 806
 	p_addr[1]=0; /* only one address*/
806 807
 	p_addr[0]=address;
807
-	strncpy(hostname, name->s, (name->len<256)?(name->len)+1:256);
808
+	len = (name->len<255)?name->len:255;
809
+	memcpy(hostname, name->s, len);
810
+	hostname[len] = '\0';
808 811
 	if (ip->len>16) return 0;
809 812
 	memcpy(address, ip->u.addr, ip->len);
810 813
 
Browse code

core: support to asign names to listen sockets

- full format:
listen=proto:address:port advertise address:port name string
- example:
listen=udp:10.0.0.10:5060 advertise 1.2.3.4:5060 name "sock1"
- the value for the name has to be enclosed in quotes
- name can be provided also when advertise is not needed

Daniel-Constantin Mierla authored on 02/04/2020 09:35:00
Showing 1 changed files
... ...
@@ -116,6 +116,7 @@ typedef struct socket_info {
116 116
 	struct addr_info* addr_info_lst; /* extra addresses (e.g. SCTP mh) */
117 117
 	int workers; /* number of worker processes for this socket */
118 118
 	int workers_tcpidx; /* index of workers in tcp children array */
119
+	str sockname; /* socket name given in config listen value */
119 120
 	struct advertise_info useinfo; /* details to be used in SIP msg */
120 121
 #ifdef USE_MCAST
121 122
 	str mcast; /* name of interface that should join multicast group*/
Browse code

Revert "core: add new function buf_print_ip(..) to print an IP address to an buffer"

This reverts commit 4acca8ad2f31f465b08f15d98cc4780236e2beb0.

Henning Westerholt authored on 23/03/2020 14:05:51
Showing 1 changed files
... ...
@@ -265,8 +265,6 @@ int mk_net_str(struct net* dst, str* s);
265 265
 
266 266
 void print_ip(char* prefix, struct ip_addr* ip, char* suffix);
267 267
 void stdout_print_ip(struct ip_addr* ip);
268
-void buf_print_ip(char *buf, struct ip_addr* ip, unsigned int len);
269
-
270 268
 void print_net(struct net* net);
271 269
 
272 270
 char* get_proto_name(unsigned int proto);
Browse code

core: add new function buf_print_ip(..) to print an IP address to an buffer

Henning Westerholt authored on 23/03/2020 11:04:54
Showing 1 changed files
... ...
@@ -265,6 +265,8 @@ int mk_net_str(struct net* dst, str* s);
265 265
 
266 266
 void print_ip(char* prefix, struct ip_addr* ip, char* suffix);
267 267
 void stdout_print_ip(struct ip_addr* ip);
268
+void buf_print_ip(char *buf, struct ip_addr* ip, unsigned int len);
269
+
268 270
 void print_net(struct net* net);
269 271
 
270 272
 char* get_proto_name(unsigned int proto);
Browse code

core: tcp - keep original connection info for haproxy tunnels

Daniel-Constantin Mierla authored on 28/10/2019 09:23:40
Showing 1 changed files
... ...
@@ -172,6 +172,15 @@ typedef struct dest_info {
172 172
 } dest_info_t;
173 173
 
174 174
 
175
+typedef struct ksr_coninfo {
176
+	ip_addr_t src_ip;
177
+	ip_addr_t dst_ip;
178
+	unsigned short src_port; /* host byte order */
179
+	unsigned short dst_port; /* host byte order */
180
+	int proto;
181
+	socket_info_t *csocket;
182
+} ksr_coninfo_t;
183
+
175 184
 typedef struct sr_net_info {
176 185
 	str data;
177 186
 	receive_info_t* rcv;
Browse code

core: pad some fields in contained structs for 4 byte alignment

- safety for sigbus on strict cpu architectures when accesing fields by
address

Daniel-Constantin Mierla authored on 28/10/2019 09:00:28
Showing 1 changed files
... ...
@@ -91,6 +91,7 @@ typedef struct addr_info {
91 91
 typedef struct advertise_info {
92 92
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
93 93
 	unsigned short port_no;  /* port number */
94
+	short port_pad; /* padding field */
94 95
 	str port_no_str; /* port number converted to string -- optimization*/
95 96
 	str address_str;        /*ip address converted to string -- optimization*/
96 97
 	struct ip_addr address; /* ip address */
... ...
@@ -109,6 +110,8 @@ typedef struct socket_info {
109 110
 	struct socket_info* prev;
110 111
 	unsigned short port_no;  /* port number */
111 112
 	char proto; /* tcp or udp*/
113
+	char proto_pad0; /* padding field */
114
+	short proto_pad1; /* padding field */
112 115
 	str sock_str; /* Socket proto, ip, and port as string */
113 116
 	struct addr_info* addr_info_lst; /* extra addresses (e.g. SCTP mh) */
114 117
 	int workers; /* number of worker processes for this socket */
... ...
@@ -143,7 +146,11 @@ typedef struct receive_info {
143 146
 										* the msg was received */
144 147
 	char proto;
145 148
 #ifdef USE_COMP
149
+	char proto_pad0;  /* padding field */
146 150
 	short comp; /* compression */
151
+#else
152
+	char proto_pad0;  /* padding field */
153
+	short proto_pad1; /* padding field */
147 154
 #endif
148 155
 	/* no need for dst_su yet */
149 156
 } receive_info_t;
... ...
@@ -153,10 +160,14 @@ typedef struct dest_info {
153 160
 	struct socket_info* send_sock;
154 161
 	union sockaddr_union to;
155 162
 	int id; /* tcp stores the connection id here */
156
-	char proto;
157 163
 	snd_flags_t send_flags;
164
+	char proto;
158 165
 #ifdef USE_COMP
166
+	char proto_pad0;  /* padding field */
159 167
 	short comp;
168
+#else
169
+	char proto_pad0;  /* padding field */
170
+	short proto_pad1; /* padding field */
160 171
 #endif
161 172
 } dest_info_t;
162 173
 
Browse code

core: init local structs to avoid use of garbage values

Daniel-Constantin Mierla authored on 01/10/2019 08:28:13
Showing 1 changed files
... ...
@@ -421,6 +421,7 @@ static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
421 421
 			break;
422 422
 		default:
423 423
 			LM_CRIT("unknown address family %d\n", su->s.sa_family);
424
+			memset(ip, 0, sizeof(ip_addr_t));
424 425
 	}
425 426
 }
426 427
 
Browse code

core: option to run event_route[core:msg-received] in incomming message

- done before any major SIP parsing
- on drop, processing of the message is stopped
- enable with received_route_mode=yes (or 1)
- for kemi, set the callback name via kemi.received_route_callback param

Daniel-Constantin Mierla authored on 01/03/2019 11:55:41
Showing 1 changed files
... ...
@@ -167,6 +167,7 @@ typedef struct sr_net_info {
167 167
 	dest_info_t* dst;
168 168
 } sr_net_info_t;
169 169
 
170
+sr_net_info_t *ksr_evrt_rcvnetinfo_get(void);
170 171
 
171 172
 #define SND_FLAGS_INIT(sflags) \
172 173
 	do{ \
Browse code

core: compacted typedefs for ip addr structs

Daniel-Constantin Mierla authored on 14/10/2017 08:41:32
Showing 1 changed files
... ...
@@ -49,7 +49,7 @@ enum sip_protos { PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP,
49 49
 enum comp_methods { COMP_NONE, COMP_SIGCOMP, COMP_SERGZ };
50 50
 #endif
51 51
 
52
-struct ip_addr{
52
+typedef struct ip_addr {
53 53
 	unsigned int af;	/* address family: AF_INET6 or AF_INET */
54 54
 	unsigned int len;	/* address len, 16 or 4 */
55 55
 
... ...
@@ -60,14 +60,12 @@ struct ip_addr{
60 60
 		unsigned short addr16[8];
61 61
 		unsigned char  addr[16];
62 62
 	}u;
63
-};
64
-
65
-typedef struct ip_addr ip_addr_t;
63
+} ip_addr_t;
66 64
 
67
-struct net{
65
+typedef struct net {
68 66
 	struct ip_addr ip;
69 67
 	struct ip_addr mask;
70
-};
68
+} sr_net_t;
71 69
 
72 70
 union sockaddr_union{
73 71
 	struct sockaddr     s;
... ...
@@ -75,13 +73,11 @@ union sockaddr_union{
75 73
 	struct sockaddr_in6 sin6;
76 74
 };
77 75
 
78
-typedef struct net sr_net_t;
79
-
80 76
 
81 77
 enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
82 78
 	SI_IS_ANY=8, SI_IS_MHOMED=16 };
83 79
 
84
-struct addr_info{
80
+typedef struct addr_info {
85 81
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
86 82
 	struct ip_addr address; /*ip address */
87 83
 	str address_str;        /*ip address converted to string -- optimization*/
... ...
@@ -89,22 +85,19 @@ struct addr_info{
89 85
 	union sockaddr_union su;
90 86
 	struct addr_info* next;
91 87
 	struct addr_info* prev;
92
-};
88
+} addr_info_t;
93 89
 
94
-typedef struct addr_info addr_info_t;
95 90
 
96
-struct advertise_info {
91
+typedef struct advertise_info {
97 92
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
98 93
 	unsigned short port_no;  /* port number */
99 94
 	str port_no_str; /* port number converted to string -- optimization*/
100 95
 	str address_str;        /*ip address converted to string -- optimization*/
101 96
 	struct ip_addr address; /* ip address */
102 97
 	str sock_str; /* Socket proto, ip, and port as string */
103
-};
104
-
105
-typedef struct advertise_info advertise_info_t;
98
+} advertise_info_t;
106 99
 
107
-struct socket_info {
100
+typedef struct socket_info {
108 101
 	int socket;
109 102
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
110 103
 	struct ip_addr address; /* ip address */
... ...
@@ -124,11 +117,21 @@ struct socket_info {
124 117
 #ifdef USE_MCAST
125 118
 	str mcast; /* name of interface that should join multicast group*/
126 119
 #endif /* USE_MCAST */
127
-};
120
+} socket_info_t;
121
+
122
+
123
+/* send flags */
124
+#define SND_F_FORCE_CON_REUSE	1 /* reuse an existing connection or fail */
125
+#define SND_F_CON_CLOSE			2 /* close the connection after sending */
126
+#define SND_F_FORCE_SOCKET		4 /* send socket in dst is forced */
127
+
128
+typedef struct snd_flags {
129
+	unsigned short f;          /* snd flags */
130
+	unsigned short blst_imask; /* blacklist ignore mask */
131
+} snd_flags_t;
128 132
 
129
-typedef struct socket_info socket_info_t;
130 133
 
131
-struct receive_info{
134
+typedef struct receive_info {
132 135
 	struct ip_addr src_ip;
133 136
 	struct ip_addr dst_ip;
134 137
 	unsigned short src_port; /* host byte order */
... ...
@@ -143,28 +146,27 @@ struct receive_info{
143 146
 	short comp; /* compression */
144 147
 #endif
145 148
 	/* no need for dst_su yet */
146
-};
149
+} receive_info_t;
150
+
151
+
152
+typedef struct dest_info {
153
+	struct socket_info* send_sock;
154
+	union sockaddr_union to;
155
+	int id; /* tcp stores the connection id here */
156
+	char proto;
157
+	snd_flags_t send_flags;
158
+#ifdef USE_COMP
159
+	short comp;
160
+#endif
161
+} dest_info_t;
147 162
 
148
-typedef struct receive_info receive_info_t;
149 163
 
150 164
 typedef struct sr_net_info {
151 165
 	str data;
152
-	struct dest_info* dst;
153
-	struct receive_info* rcv;
166
+	receive_info_t* rcv;
167
+	dest_info_t* dst;
154 168
 } sr_net_info_t;
155 169
 
156
-/* send flags */
157
-#define SND_F_FORCE_CON_REUSE	1 /* reuse an existing connection or fail */
158
-#define SND_F_CON_CLOSE			2 /* close the connection after sending */
159
-#define SND_F_FORCE_SOCKET		4 /* send socket in dst is forced */
160