Browse code

core: parse_phostport split for k compatibility

parse_phostport in sr and ser returned a list of pkg_malloc'ed
addresses belonging to the same multi-homed "group". For example
sctp:(1.2.3.4, 5.6.7.8):5080 is a valid address and it means
that this sctp listening socket must use multi-homing on the 2
IPs. However several kamailio modules use parse_phostport and
expect the old integer returning version. In this case it was
easier to split the function in the core into parse_phostport()
(old behaviour) and parse_phostport_mh() (returns list of MH
addresses) and make the core command line parser use the MH
supporting version.

Andrei Pelinescu-Onciul authored on 20/07/2009 11:00:37
Showing 2 changed files
... ...
@@ -963,7 +963,21 @@ error:
963 963
  * returns  fills proto, port, host and returns list of addresses on success
964 964
  * (pkg malloc'ed) and 0 on failure
965 965
  */
966
-struct name_lst* parse_phostport(char* s, char** host, int* hlen,
966
+/** get protocol host and port from a string representation.
967
+ * parses [proto:]host[:port]  or
968
+ *  [proto:](host_1, host_2, ... host_n)[:port]
969
+ * where proto= udp|tcp|tls|sctp
970
+ * @param s  - string (like above)
971
+ * @param host - will be filled with the host part
972
+ *               Note: for multi-homing it wil contain all the addresses
973
+ *               (e.g.: "sctp:(1.2.3.4, 5.6.7.8)" => host="(1.2.3.4, 5.6.7.8)")
974
+ * @param hlen - will be filled with the length of the host part.
975
+ * @param port - will be filled with the port if present or 0 if it's not.
976
+ * @param proto - will be filled with the protocol if present or PROTO_NONE
977
+ *                if it's not.
978
+ * @return  fills proto, port, host and returns 0 on success and -1 on failure.
979
+ */
980
+int parse_phostport(char* s, char** host, int* hlen,
967 981
 								 int* port, int* proto)
968 982
 {
969 983
 	char* first; /* first ':' occurrence */
... ...
@@ -997,7 +1011,7 @@ struct name_lst* parse_phostport(char* s, char** host, int* hlen,
997 997
 				break;
998 998
 		}
999 999
 	}
1000
-	if (p==s) return 0;
1000
+	if (p==s) return -1;
1001 1001
 	if (*(p-1)==':') goto error_colons;
1002 1002
 
1003 1003
 	if (first==0){ /* no ':' => only host */
... ...
@@ -1030,22 +1044,48 @@ struct name_lst* parse_phostport(char* s, char** host, int* hlen,
1030 1030
 		*hlen=(int)(first-*host);
1031 1031
 	}
1032 1032
 end:
1033
-	return parse_name_lst(*host, *hlen);
1033
+	return 0;
1034 1034
 error_brackets:
1035 1035
 	LOG(L_ERR, "ERROR: parse_phostport: too many brackets in %s\n", s);
1036
-	return 0;
1036
+	return -1;
1037 1037
 error_colons:
1038 1038
 	LOG(L_ERR, "ERROR: parse_phostport: too many colons in %s\n", s);
1039
-	return 0;
1039
+	return -1;
1040 1040
 error_proto:
1041 1041
 	LOG(L_ERR, "ERROR: parse_phostport: bad protocol in %s\n", s);
1042
-	return 0;
1042
+	return -1;
1043 1043
 error_port:
1044 1044
 	LOG(L_ERR, "ERROR: parse_phostport: bad port number in %s\n", s);
1045
+	return -1;
1046
+}
1047
+
1048
+
1049
+
1050
+/** get protocol host, port and MH addresses list from a string representation.
1051
+ * parses [proto:]host[:port]  or
1052
+ *  [proto:](host_1, host_2, ... host_n)[:port]
1053
+ * where proto= udp|tcp|tls|sctp
1054
+ * @param s  - string (like above)
1055
+ * @param host - will be filled with the host part
1056
+ *               Note: for multi-homing it wil contain all the addresses
1057
+ *               (e.g.: "sctp:(1.2.3.4, 5.6.7.8)" => host="(1.2.3.4, 5.6.7.8)")
1058
+ * @param hlen - will be filled with the length of the host part.
1059
+ * @param port - will be filled with the port if present or 0 if it's not.
1060
+ * @param proto - will be filled with the protocol if present or PROTO_NONE
1061
+ *                if it's not.
1062
+ * @return  fills proto, port, host and returns list of addresses on success
1063
+ * (pkg malloc'ed) and 0 on failure
1064
+ */
1065
+static struct name_lst* parse_phostport_mh(char* s, char** host, int* hlen,
1066
+								 int* port, int* proto)
1067
+{
1068
+	if (parse_phostport(s, host, hlen, port, proto)==0)
1069
+		return parse_name_lst(*host, *hlen);
1045 1070
 	return 0;
1046 1071
 }
1047 1072
 
1048 1073
 
1074
+
1049 1075
 /** Update \c cfg_file variable to contain full pathname. The function updates
1050 1076
  * the value of \c cfg_file global variable to contain full absolute pathname
1051 1077
  * to the main configuration file of SER. The function uses CFG_FILE macro to
... ...
@@ -1712,7 +1752,7 @@ try_again:
1712 1712
 					}
1713 1713
 					break;
1714 1714
 			case 'l':
1715
-					if ((n_lst=parse_phostport(optarg, &tmp, &tmp_len,
1715
+					if ((n_lst=parse_phostport_mh(optarg, &tmp, &tmp_len,
1716 1716
 											&port, &proto))==0){
1717 1717
 						fprintf(stderr, "bad -l address specifier: %s\n",
1718 1718
 										optarg);
... ...
@@ -97,7 +97,7 @@ struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
97 97
 
98 98
 struct socket_info** get_sock_info_list(unsigned short proto);
99 99
 
100
-struct name_lst* parse_phostport(char* s, char** host, int* hlen,
100
+int parse_phostport(char* s, char** host, int* hlen,
101 101
 								 int* port, int* proto);
102 102
 
103 103
 /* helper function: