--- jumpgate.c.orig 2002-11-30 05:00:12.000000000 +0800 +++ jumpgate.c 2009-10-20 14:29:14.000000000 +0800 @@ -25,10 +25,11 @@ rec.latency_flag = rec.log_flag = rec.rport = lport = 0; rec.hostname = NULL; + rec.local_addr = htonl(INADDR_ANY); bind_addr = htonl(INADDR_ANY); opterr = 0; - while((c = getopt(argc, argv, "hsvil:r:a:b:f:c:L:")) != -1) + while((c = getopt(argc, argv, "hsvil:r:a:b:f:c:L:B:")) != -1) { switch(c) { @@ -112,6 +113,20 @@ } break; + case 'B': + if((rec.local_addr = inet_addr(optarg)) == INADDR_NONE) + { + if((h_ent = gethostbyname(optarg)) != NULL) + { + memcpy(&rec.local_addr, h_ent->h_addr, sizeof(in_addr_t)); + } + else + { + error_exit(0, "error: error resolving hostname for outbound binding"); + } + } + break; + case 'f': rec.log_flag = 1; rec.logfile = optarg; @@ -258,7 +273,7 @@ { fprintf(stderr, "usage: %s [-hsvi] [-b ] [-l ]\n", name); fprintf(stderr, " [-r ] [-a ] [-f ]\n"); - fprintf(stderr, " [-c ] [-L ]\n"); + fprintf(stderr, " [-c ] [-L ] [-B ]\n"); } void @@ -267,7 +282,7 @@ fprintf(stderr, "\njumpgate v%s by Patroklos G. Argyroudis \n\n", VERSION); fprintf(stderr, "usage: %s [-hsvi] [-b ] [-l ]\n", name); fprintf(stderr, " [-r ] [-a ] [-f ]\n"); - fprintf(stderr, " [-c ] [-L ]\n\n"); + fprintf(stderr, " [-c ] [-L ] [-B ]\n\n"); fprintf(stderr, "-h this help message\n"); fprintf(stderr, "-s log jumpgate use via syslog(3)\n"); fprintf(stderr, "-v display version number and exit\n"); @@ -278,7 +293,8 @@ fprintf(stderr, "-a specify the host to forward the connection\n"); fprintf(stderr, "-f specify the filename to log the connection\n"); fprintf(stderr, "-c specify the maximum number of the connections backlog\n"); - fprintf(stderr, "-L specify the latency in microseconds\n\n"); + fprintf(stderr, "-L specify the latency in microseconds\n"); + fprintf(stderr, "-B specify the local outgoing IP Address to bind\n\n"); } /* taken from "UNIX Network Programming" by W. Richard Stevens */ @@ -434,8 +450,13 @@ int n, rfd; char buf[BUFSIZE]; fd_set fdsr, fdse; - struct sockaddr_in raddr; + struct sockaddr_in raddr, laddr; + bzero(&laddr, sizeof(laddr)); + laddr.sin_family = AF_INET; + laddr.sin_addr.s_addr = jmp->local_addr; + laddr.sin_port = 0; + if(jmp->log_flag == 1) { if((fp = fopen(jmp->logfile, "a+")) == NULL) @@ -448,7 +469,11 @@ { error_log(1, 1, LOG_ERR, "socket error"); } - + + if (bind(rfd, (struct sockaddr *) &laddr, sizeof(laddr)) < 0) { + error_exit(1, "bind outbound address error"); + } + raddr.sin_family = AF_INET; raddr.sin_port = htons(jmp->rport); memcpy(&raddr.sin_addr, &jmp->hostaddr, sizeof(in_addr_t)); --- jumpgate.h.orig 2002-11-30 05:00:12.000000000 +0800 +++ jumpgate.h 2009-10-20 14:25:17.000000000 +0800 @@ -43,6 +43,7 @@ useconds_t latency; /* latency in microseconds */ char *hostname; /* remote host name */ in_addr_t hostaddr; /* remote host address */ + in_addr_t local_addr; /* local outgoing address */ int log_flag; int latency_flag; char *logfile; /* name of the logfile */