aboutsummaryrefslogtreecommitdiff
path: root/src/net.c
diff options
context:
space:
mode:
authorsethdelliott <sethdelliott@gmail.com>2010-06-18 21:08:50 +0000
committersethdelliott <sethdelliott@gmail.com>2010-06-18 21:08:50 +0000
commit982c704a8a6a5e9a4f9dc8a76f0dd05e8cdb4411 (patch)
treea1ea73e5d8038528b316a6716737e53bc918a33a /src/net.c
parentff385f98e97fa0d73745a58ec9c1ff0276d47a5e (diff)
downloadiperf3-982c704a8a6a5e9a4f9dc8a76f0dd05e8cdb4411.tar.gz
NOTE: This is not a working revision. I'm in the process of redesigning how the client and server communicate. It will be fixed soon
Diffstat (limited to 'src/net.c')
-rw-r--r--src/net.c72
1 files changed, 34 insertions, 38 deletions
diff --git a/src/net.c b/src/net.c
index 75da7ac..254637d 100644
--- a/src/net.c
+++ b/src/net.c
@@ -21,40 +21,39 @@
int
netdial(int proto, char *client, int port)
{
- int s;
+ int s;
struct hostent *hent;
struct sockaddr_in sa;
socklen_t sn;
- /* XXX: This is not working for non-fully qualified host names
- use getaddrinfo() instead?
- */
- if ((hent = gethostbyname(client)) == 0)
- {
- perror("gethostbyname");
- return (-1);
+ /* XXX: This is not working for non-fully qualified host names use getaddrinfo() instead? */
+ if ((hent = gethostbyname(client)) == 0) {
+ perror("gethostbyname");
+ return (-1);
}
+
s = socket(AF_INET, proto, 0);
- if (s < 0)
- {
- perror("socket");
- return (-1);
+ if (s < 0) {
+ perror("socket");
+ return (-1);
}
+
memset(&sa, 0, sizeof sa);
memmove(&sa.sin_addr, hent->h_addr, 4);
sa.sin_port = htons(port);
sa.sin_family = AF_INET;
- if (connect(s, (struct sockaddr *) & sa, sizeof sa) < 0 && errno != EINPROGRESS)
- {
- perror("netdial: connect error");
- return (-1);
+ if (connect(s, (struct sockaddr *) & sa, sizeof sa) < 0 && errno != EINPROGRESS) {
+ perror("netdial: connect error");
+ return (-1);
}
+
sn = sizeof sa;
- if (getpeername(s, (struct sockaddr *) & sa, &sn) >= 0)
- {
- return (s);
+
+ if (getpeername(s, (struct sockaddr *) & sa, &sn) >= 0) {
+ return (s);
}
+
perror("getpeername error");
return (-1);
}
@@ -64,33 +63,31 @@ netdial(int proto, char *client, int port)
int
netannounce(int proto, char *local, int port)
{
- int s;
+ int s;
struct sockaddr_in sa;
/* XXX: implement binding to a local address rather than * */
- //printf("in netannounce: port = %d \n", port);
memset((void *) &sa, 0, sizeof sa);
s = socket(AF_INET, proto, 0);
- if (s < 0)
- {
- perror("socket");
- return (-1);
+ if (s < 0) {
+ perror("socket");
+ return (-1);
}
- int opt = 1;
+ int opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
sa.sin_port = htons(port);
sa.sin_family = AF_INET;
- if (bind(s, (struct sockaddr *) & sa, sizeof(struct sockaddr_in)) < 0)
- {
- close(s);
- perror("bind");
- return (-1);
+ if (bind(s, (struct sockaddr *) & sa, sizeof(struct sockaddr_in)) < 0) {
+ close(s);
+ perror("bind");
+ return (-1);
}
+
if (proto == SOCK_STREAM)
- listen(s, 5);
+ listen(s, 5);
return s;
}
@@ -106,12 +103,11 @@ Nread(int fd, char *buf, int count, int prot)
struct sockaddr from;
socklen_t len = sizeof(from);
register int cnt;
- if (prot == SOCK_DGRAM)
- {
- cnt = recvfrom(fd, buf, count, 0, &from, &len);
- } else
- {
- cnt = mread(fd, buf, count);
+
+ if (prot == SOCK_DGRAM) {
+ cnt = recvfrom(fd, buf, count, 0, &from, &len);
+ } else {
+ cnt = mread(fd, buf, count);
}
return (cnt);
}