code: drawterm

Download patch

ref: 2f4516ba69e49b93de2f844c5e772112ff58a31e
parent: 23a48c7cfe2e38265eeaa1a8b8adfc57c887be76
author: David du Colombier <0intro@gmail.com>
date: Mon Sep 10 15:51:39 EDT 2012

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

--- a/kern/devip-posix.c
+++ b/kern/devip-posix.c
@@ -26,6 +26,18 @@
 	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)
 {
@@ -84,7 +96,7 @@
 		break;
 	}
 
-	if(connect(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(connect(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }
 
@@ -172,7 +184,7 @@
 				break;
 			}
 
-			if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) >= 0)	
+			if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)	
 				return;
 		}
 		oserror();
@@ -190,7 +202,7 @@
 		break;
 	}
 
-	if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }
 
--- a/kern/devip-win32.c
+++ b/kern/devip-win32.c
@@ -25,6 +25,18 @@
 	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)
 {
@@ -88,7 +100,7 @@
 		break;
 	}
 
-	if(connect(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(connect(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }
 
@@ -176,7 +188,7 @@
 				break;
 			}
 
-			if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) >= 0)	
+			if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)	
 				return;
 		}
 		oserror();
@@ -194,7 +206,7 @@
 		break;
 	}
 
-	if(bind(fd, (struct sockaddr*)&ss, sizeof(ss)) < 0)
+	if(bind(fd, (struct sockaddr*)&ss, addrlen(&ss)) < 0)
 		oserror();
 }