aboutsummaryrefslogtreecommitdiff
path: root/src/net.c
diff options
context:
space:
mode:
authorsethdelliott <devnull@localhost>2010-07-26 21:30:34 +0000
committersethdelliott <devnull@localhost>2010-07-26 21:30:34 +0000
commita1344ede16f93a4b361b86658a09ab223f44adf8 (patch)
tree41b5474596a047966a7893e3c58e0c62ecd8ff72 /src/net.c
parentbb0677cdfb77d327e9b4f0348f12571c727951f6 (diff)
downloadiperf3-a1344ede16f93a4b361b86658a09ab223f44adf8.tar.gz
Added support for binding (-B) to a specific interface
Diffstat (limited to 'src/net.c')
-rw-r--r--src/net.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/net.c b/src/net.c
index 14183e2..898dca6 100644
--- a/src/net.c
+++ b/src/net.c
@@ -18,21 +18,32 @@
*/
/* make connection to server */
-// XXX: Change client to server?
int
-netdial(int proto, char *client, int port)
+netdial(int proto, char *local, char *server, int port)
{
int s;
struct hostent *hent;
- struct sockaddr_in sa;
+ struct sockaddr_in sa, lo;
- /* XXX: This is not working for non-fully qualified host names use getaddrinfo() instead? */
- if ((hent = gethostbyname(client)) == 0) {
+ s = socket(AF_INET, proto, 0);
+ if (s < 0) {
return (-1);
}
- s = socket(AF_INET, proto, 0);
- if (s < 0) {
+ if (local) {
+ if ((hent = gethostbyname(local)) == 0)
+ return (-1);
+
+ memset(&lo, 0, sizeof(lo));
+ memmove(&lo.sin_addr, hent->h_addr_list[0], 4);
+ lo.sin_family = AF_INET;
+
+ if (bind(s, (struct sockaddr *) &lo, sizeof(lo)) < 0)
+ return (-1);
+ }
+
+ /* XXX: This is not working for non-fully qualified host names use getaddrinfo() instead? */
+ if ((hent = gethostbyname(server)) == 0) {
return (-1);
}
@@ -55,6 +66,7 @@ netannounce(int proto, char *local, int port)
{
int s;
struct sockaddr_in sa;
+ struct hostent *hent;
/* XXX: implement binding to a local address rather than * */
memset((void *) &sa, 0, sizeof sa);
@@ -66,6 +78,14 @@ netannounce(int proto, char *local, int port)
int opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
+ if (local) {
+ if ((hent = gethostbyname(local)) == NULL)
+ return (-1);
+ memcpy(&sa.sin_addr, hent->h_addr_list[0], 4);
+ } else {
+ sa.sin_addr.s_addr = htonl(INADDR_ANY);
+ }
+
sa.sin_port = htons(port);
sa.sin_family = AF_INET;