--- l2_ch_socket.c 2001/09/07 10:23:31 1.19
+++ l2_ch_socket.c 2001/09/07 13:03:07 1.20
@@ -139,8 +139,8 @@
int i;
l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
struct hostent *pHostentry;
- struct sockaddr_in IP4Sockaddr;
- struct sockaddr_in6 IP6Sockaddr;
+ struct sockaddr_in IP4Localsock, IP4Sockaddr;
+ struct sockaddr_in6 IP6Localsock, IP6Sockaddr;
struct in6_addr *IP6Addr;
/* make sure a target is configured */
@@ -149,6 +149,10 @@
/* open channel socket */
if (cfg->iFamily == AF_INET) { /* IPv4 */
+ /* initialize address structures */
+ memset(&IP4Localsock, 0, sizeof(IP4Localsock));
+ memset(&IP4Sockaddr, 0, sizeof(IP4Sockaddr));
+
/* resolve host numerically */
if (inet_pton(AF_INET, cfg->pszHost, &IP4Sockaddr.sin_addr.s_addr) == 1) {
IP4Sockaddr.sin_family = AF_INET;
@@ -168,23 +172,48 @@
else /* host string was not parsable for some reason */
return L2_ERR_SYS;
- if (cfg->iProto == IPPROTO_TCP) { /* With TCP we must connect */
+ if (cfg->iProto == IPPROTO_TCP) { /* with TCP we must connect */
if ((cfg->iSocket = socket(PF_INET, SOCK_STREAM, cfg->iProto)) == -1)
return L2_ERR_SYS;
+ /* if we have a socket, then bind ourselves to any port number */
+ IP4Localsock.sin_family = AF_INET;
+ IP4Localsock.sin_addr.s_addr = htonl(INADDR_ANY);
+ IP4Localsock.sin_port = htons(0);
+
+ if (bind(cfg->iSocket, (struct sockaddr *)&IP4Localsock,\
+ sizeof(IP4Localsock))) {
+ return L2_ERR_SYS;
+ }
+
if (connect(cfg->iSocket, (struct sockaddr *)&IP4Sockaddr,
sizeof(IP4Sockaddr))) {
close(cfg->iSocket);
return L2_ERR_SYS;
}
}
- else if (cfg->iProto == IPPROTO_UDP) /* With UDP we do not connect */
+ else if (cfg->iProto == IPPROTO_UDP) { /* with UDP we do not connect */
if ((cfg->iSocket = socket(PF_INET, SOCK_DGRAM, cfg->iProto)) == -1)
return L2_ERR_SYS;
+ /* if we have a socket, then bind ourselves to any port number */
+ IP4Localsock.sin_family = AF_INET;
+ IP4Localsock.sin_addr.s_addr = htonl(INADDR_ANY);
+ IP4Localsock.sin_port = htons(0);
+
+ if (bind(cfg->iSocket, (struct sockaddr *)&IP4Localsock,\
+ sizeof(IP4Localsock))) {
+ return L2_ERR_SYS;
+ }
+ }
+
return L2_OK;
}
else if (cfg->iFamily == AF_INET6) { /* IPv6 */
+ /* initialize address structures */
+ memset(&IP6Localsock, 0, sizeof(IP6Localsock));
+ memset(&IP6Sockaddr, 0, sizeof(IP6Sockaddr));
+
/* resolve host numerically */
if (inet_pton(AF_INET6, cfg->pszHost, &IP6Sockaddr.sin6_addr.s6_addr) == 1) {
IP6Sockaddr.sin6_family = AF_INET6;
@@ -199,25 +228,46 @@
sizeof(IP6Sockaddr.sin6_addr.__u6_addr));
}
else
- return L2_ERR_USE; /* what?? we specify IPv6 when it is not? */
+ return L2_ERR_USE; /* what?? we specify IPv6 when it is not? */
}
- else /* host string was not parsable for some reason */
+ else /* host string was not parsable for some reason */
return L2_ERR_SYS;
- if (cfg->iProto == IPPROTO_TCP) { /* With TCP we must connect */
+ if (cfg->iProto == IPPROTO_TCP) { /* with TCP we must connect */
if ((cfg->iSocket = socket(PF_INET6, SOCK_STREAM, cfg->iProto)) == -1)
return L2_ERR_SYS;
+ /* if we have a socket, then bind ourselves to any port number */
+ IP6Localsock.sin6_family = AF_INET6;
+ IP6Localsock.sin6_addr = in6addr_any;
+ IP6Localsock.sin6_port = htons(0);
+
+ if (bind(cfg->iSocket, (struct sockaddr *)&IP6Localsock,\
+ sizeof(IP6Localsock))) {
+ return L2_ERR_SYS;
+ }
+
if (connect(cfg->iSocket, (struct sockaddr *)&IP6Sockaddr,\
sizeof(IP6Sockaddr))) {
close(cfg->iSocket);
return L2_ERR_SYS;
}
}
- else if (cfg->iProto == IPPROTO_UDP) /* With UDP we do not connect */
+ else if (cfg->iProto == IPPROTO_UDP) { /* with UDP we do not connect */
if ((cfg->iSocket = socket(PF_INET, SOCK_DGRAM, cfg->iProto)) == -1)
return L2_ERR_SYS;
+ /* if we have a socket, then bind ourselves to any port number */
+ IP6Localsock.sin6_family = AF_INET6;
+ IP6Localsock.sin6_addr = in6addr_any;
+ IP6Localsock.sin6_port = htons(0);
+
+ if (bind(cfg->iSocket, (struct sockaddr *)&IP6Localsock,\
+ sizeof(IP6Localsock))) {
+ return L2_ERR_SYS;
+ }
+ }
+
return L2_OK;
}
|