소스 검색

core: socket info - do not use interface index as internal array index

- there are cases when the interface number is high, even if there are
  only a few real network interfaces, which can result in inability to
  listen on them

(cherry picked from commit 7acfe7723b4772ca45ce31ae98bf3d0b6948f50a)
Daniel-Constantin Mierla 11 달 전
부모
커밋
1d216f8764
1개의 변경된 파일25개의 추가작업 그리고 13개의 파일을 삭제
  1. 25 13
      src/core/socket_info.c

+ 25 - 13
src/core/socket_info.c

@@ -1471,6 +1471,7 @@ static int build_iface_list(void)
 	char name[MAX_IF_LEN];
 	int is_link_local = 0;
 	int num = 0;
+	int ifidx = 0;
 
 	if(ifaces == NULL) {
 		if((ifaces = (struct idxlist *)pkg_malloc(
@@ -1598,31 +1599,42 @@ static int build_iface_list(void)
 				}
 			}
 
-			if(index >= MAX_IFACE_NO) {
-				LM_ERR("Invalid interface index returned: %d (n: %d) - skip\n",
-						index, num);
-				pkg_free(entry);
-				continue;
+			for(ifidx = 0; ifidx < MAX_IFACE_NO; ifidx++) {
+				if(ifaces[ifidx].index == index) {
+					/* interface with same index already found */
+					break;
+				}
+			}
+			if(ifidx == MAX_IFACE_NO) {
+				if(num == MAX_IFACE_NO) {
+					LM_ERR("too many interfaces: %d (n: %d) - skipping\n",
+							index, num);
+					pkg_free(entry);
+					goto done;
+				}
+				ifidx = num;
+				num++;
 			}
-			num++;
 
-			if(strlen(ifaces[index].name) == 0 && strlen(name) > 0) {
-				memcpy(ifaces[index].name, name, MAX_IF_LEN - 1);
-				ifaces[index].name[MAX_IF_LEN - 1] = '\0';
+			if(strlen(ifaces[ifidx].name) == 0 && strlen(name) > 0) {
+				memcpy(ifaces[ifidx].name, name, MAX_IF_LEN - 1);
+				ifaces[ifidx].name[MAX_IF_LEN - 1] = '\0';
 			}
 
-			ifaces[index].index = index;
+			ifaces[ifidx].index = index;
 
-			if(ifaces[index].addresses == 0)
-				ifaces[index].addresses = entry;
+			if(ifaces[ifidx].addresses == 0)
+				ifaces[ifidx].addresses = entry;
 			else {
-				for(tmp = ifaces[index].addresses; tmp->next;
+				for(tmp = ifaces[ifidx].addresses; tmp->next;
 						tmp = tmp->next) /*empty*/
 					;
 				tmp->next = entry;
 			}
 		}
 	}
+
+done:
 	if(nl_sock > 0)
 		close(nl_sock);
 	/* the socket should be closed so we can bind again */