Browse code

core: fix missing close() in get_out_socket()

- errorpath for EISCONN missed close()-ing the sockets
- added unlikely() on all the error checks

Andrei Pelinescu-Onciul authored on 02/02/2010 09:09:15
Showing 1 changed files
... ...
@@ -129,14 +129,14 @@ struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
129 129
 	struct socket_info* si;
130 130
 	struct ip_addr ip;
131 131
 
132
-	if (proto!=PROTO_UDP) {
132
+	if (unlikely(proto!=PROTO_UDP)) {
133 133
 		LOG(L_CRIT, "BUG: get_out_socket can only be called for UDP\n");
134 134
 		return 0;
135 135
 	}
136 136
 retry:
137 137
 	switch(to->s.sa_family){
138 138
 	case AF_INET : {
139
-		if(sock_inet < 0){
139
+		if(unlikely(sock_inet < 0)){
140 140
 			sock_inet = socket(AF_INET, SOCK_DGRAM, 0);
141 141
 			if (sock_inet==-1) {
142 142
 				LM_ERR("socket() failed: %s\n", strerror(errno));
... ...
@@ -148,7 +148,7 @@ retry:
148 148
 	}
149 149
 #ifdef USE_IPV6
150 150
 	case AF_INET6 : {
151
-		if(sock_inet6 < 0){
151
+		if(unlikely(sock_inet6 < 0)){
152 152
 			sock_inet6 = socket(AF_INET6, SOCK_DGRAM, 0);
153 153
 			if (sock_inet6==-1) {
154 154
 				LM_ERR("socket() failed: %s\n", strerror(errno));
... ...
@@ -164,13 +164,19 @@ retry:
164 164
 		return 0;
165 165
 	}
166 166
 	}
167
-	if (connect(*temp_sock, &to->s, sockaddru_len(*to))==-1) {
168
-		if (errno==EISCONN && !mhomed_sock_cache_disabled){
167
+	if (unlikely(connect(*temp_sock, &to->s, sockaddru_len(*to))==-1)) {
168
+		if (unlikely(errno==EISCONN && !mhomed_sock_cache_disabled)){
169 169
 			/*  no multiple connects support on the same socket */
170 170
 			mhomed_sock_cache_disabled=1;
171
-			sock_inet=-1;
171
+			if (sock_inet>=0){
172
+				close(sock_inet);
173
+				sock_inet=-1;
174
+			}
172 175
 #ifdef USE_IPV6
173
-			sock_inet6=-1;
176
+			if (sock_inet>=0){
177
+				close(sock_inet6);
178
+				sock_inet6=-1;
179
+			}
174 180
 #endif /* USE_IPV6 */
175 181
 			goto retry;
176 182
 		}
... ...
@@ -179,7 +185,7 @@ retry:
179 185
 		goto error;
180 186
 	}
181 187
 	len=sizeof(from);
182
-	if (getsockname(*temp_sock, &from.s, &len)==-1) {
188
+	if (unlikely(getsockname(*temp_sock, &from.s, &len)==-1)) {
183 189
 		LOG(L_ERR, "ERROR: get_out_socket: getsockname failed: %s\n",
184 190
 				strerror(errno));
185 191
 		goto error;
... ...
@@ -195,7 +201,7 @@ retry:
195 201
 	return si;
196 202
 error:
197 203
 	LOG(L_ERR, "ERROR: get_out_socket: no socket found\n");
198
-	if (unlikely(*temp_sock >=0 && mhomed_sock_cache_disabled)){
204
+	if (unlikely(mhomed_sock_cache_disabled && *temp_sock >=0)){
199 205
 		close(*temp_sock);
200 206
 		*temp_sock=-1;
201 207
 	}