Commits

David du Colombier committed 4a8b3bb

devip: fix addrlen in connect() and bind()

On some systems, the third argument of connect() and bind()
is expected to be the length of the address family instead
of the length of the sockaddr structure.

R=rsc
http://codereview.appspot.com/6492074

Comments (0)

Files changed (2)

 	return AF_INET6;
 }
 
+static int
+addrlen(struct sockaddr_storage *ss)
+{
+	switch(ss->ss_family){
+	case AF_INET:
+		return sizeof(struct sockaddr_in);
+	case AF_INET6:
+		return sizeof(struct sockaddr_in6);
+	}
+	return 0;
+}
+
 void
 osipinit(void)
 {
 		break;
 	}
 
-	if(connect(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(connect(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }
 
 				break;
 			}
 
-			if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) >= 0)	
+			if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)	
 				return;
 		}
 		oserror();
 		break;
 	}
 
-	if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }
 
 	return AF_INET6;
 }
 
+static int
+addrlen(struct sockaddr_storage *ss)
+{
+	switch(ss->ss_family){
+	case AF_INET:
+		return sizeof(struct sockaddr_in);
+	case AF_INET6:
+		return sizeof(struct sockaddr_in6);
+	}
+	return 0;
+}
+
 void
 osipinit(void)
 {
 		break;
 	}
 
-	if(connect(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(connect(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }
 
 				break;
 			}
 
-			if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) >= 0)	
+			if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)	
 				return;
 		}
 		oserror();
 		break;
 	}
 
-	if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }