--- qmail-remote.c.org	2007-01-30 03:30:20.000000000 +0100
+++ qmail-remote.c	2007-01-30 03:59:12.000000000 +0100
@@ -39,6 +39,7 @@
 static stralloc sauninit = {0};
 
 stralloc helohost = {0};
+stralloc outgoingip = {0};
 stralloc routes = {0};
 struct constmap maproutes;
 stralloc host = {0};
@@ -47,6 +48,7 @@
 saa reciplist = {0};
 
 struct ip_address partner;
+struct ip_address outip;
 
 #ifdef TLS
 # include <sys/stat.h>
@@ -67,6 +69,7 @@
 ch = sa->s[i]; if (ch < 33) ch = '?'; if (ch > 126) ch = '?';
 if (substdio_put(subfdoutsmall,&ch,1) == -1) _exit(0); } }
 
+void temp_noip() { out("Zinvalid ipaddr in control/outgoingip (#4.3.0)\n"); zerodie(); }
 void temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); }
 void temp_oserr() { out("Z\
 System resources temporarily unavailable. (#4.3.0)\n"); zerodie(); }
@@ -651,6 +654,7 @@
 
 void getcontrols()
 {
+  int r;
   if (control_init() == -1) temp_control();
   if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control();
   if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1)
@@ -665,6 +669,12 @@
     case 1:
       if (!constmap_init(&maproutes,routes.s,routes.len,1)) temp_nomem(); break;
   }
+  r = control_readline(&outgoingip,"control/outgoingip");
+  if (-1 == r) { if (errno == error_nomem) temp_nomem(); temp_control(); }
+  if (0 == r && !stralloc_copy(&outgoingip, "0.0.0.0")) temp_nomem();
+  if (0 == strcmp(outgoingip.s, "0.0.0.0"))
+  { outip.d[0]=outip.d[1]=outip.d[2]=outip.d[3]=(unsigned long) 0; }
+  else if (!ip_scan(outgoingip.s, &outip)) temp_noip();
 }
 
 void main(argc,argv)
@@ -755,7 +765,7 @@
     smtpfd = socket(AF_INET,SOCK_STREAM,0);
     if (smtpfd == -1) temp_oserr();
  
-    if (timeoutconn(smtpfd,&ip.ix[i].ip,(unsigned int) port,timeoutconnect) == 0) {
+    if (timeoutconn(smtpfd,&ip.ix[i].ip,&outip,(unsigned int) port,timeoutconnect) == 0) {  
       tcpto_err(&ip.ix[i].ip,0);
       partner = ip.ix[i].ip;
 #ifdef TLS
--- remoteinfo.c.org	2007-01-30 03:59:56.000000000 +0100
+++ remoteinfo.c	2007-01-30 04:07:21.000000000 +0100
@@ -49,7 +49,7 @@
   byte_copy(&sin.sin_addr,4,ipl);
   sin.sin_port = 0;
   if (bind(s,(struct sockaddr *) &sin,sizeof(sin)) == -1) { close(s); return 0; }
-  if (timeoutconn(s,ipr,113,timeout) == -1) { close(s); return 0; }
+  if (timeoutconn(s,ipr,ipl,113,timeout) == -1) { close(s); return 0; }
   fcntl(s,F_SETFL,fcntl(s,F_GETFL,0) & ~O_NDELAY);
  
   len = 0;
--- timeoutconn.c.org	2007-01-30 04:07:47.000000000 +0100
+++ timeoutconn.c	2007-01-30 04:09:33.000000000 +0100
@@ -10,9 +10,10 @@
 #include "byte.h"
 #include "timeoutconn.h"
 
-int timeoutconn(s,ip,port,timeout)
+int timeoutconn(s,ip,outip,port,timeout)
 int s;
 struct ip_address *ip;
+struct ip_address *outip;
 unsigned int port;
 int timeout;
 {
@@ -21,7 +22,14 @@
   char *x;
   fd_set wfds;
   struct timeval tv;
- 
+
+  /* bind() an outgoing ipaddr */
+  byte_zero(&sin,sizeof(sin));
+  byte_copy(&sin.sin_addr.s_addr,4,outip);
+  sin.sin_family = AF_INET;
+
+  if (-1 == bind(s,(struct sockaddr *) &sin,sizeof(sin))) return -1;
+
   byte_zero(&sin,sizeof(sin));
   byte_copy(&sin.sin_addr,4,ip);
   x = (char *) &sin.sin_port;
