summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2013-03-12 10:13:10 +0100
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2013-03-12 10:22:58 +0100
commit77a83472135eef19d9953898d106e6a5ab015912 (patch)
tree387dd9260226cbfb204c09ee9b66ec51085af1c2
parented959b801b98cb7dcceda100bceee923ce5acbc7 (diff)
downloadecon-77a83472135eef19d9953898d106e6a5ab015912.tar.gz
econ-77a83472135eef19d9953898d106e6a5ab015912.tar.bz2
econ-77a83472135eef19d9953898d106e6a5ab015912.zip
util: Set SO_BROADCAST for udp eonnect sockets by default
So that we can use it for econfind, and do not have the repeat all the code.
-rw-r--r--econfind.c37
-rw-r--r--util.c7
2 files changed, 8 insertions, 36 deletions
diff --git a/econfind.c b/econfind.c
index ae31a26..88bbbd3 100644
--- a/econfind.c
+++ b/econfind.c
@@ -17,8 +17,6 @@
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
@@ -29,39 +27,6 @@
#include "econproto.h"
#include "econpacket.h"
-static int
-connect_broadcast(const char *addr, int port)
-{
- int fd, ret;
- struct sockaddr_in s;
- int broadcast_enable = 1;
-
- memset(&s, 0, sizeof s);
- s.sin_family = AF_INET;
- s.sin_port = htons(port);
- s.sin_addr.s_addr = inet_addr(addr);
-
- fd = socket(AF_INET, SOCK_DGRAM, 0);
- if (fd == -1)
- return -1;
- ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
- &broadcast_enable, sizeof(broadcast_enable));
- if (ret < 0) {
- fprintf(stderr, "Failed to setsockopt broadcast: %s\n",
- strerror(errno)),
- close(fd);
- return -1;
- }
-
- if (connect(fd, (struct sockaddr *) &s, sizeof s) < 0) {
- fprintf(stderr, "Failed to connect: %s\n", strerror(errno));
- close(fd);
- return -1;
- }
-
- return fd;
-}
-
int
main(int argc, char *argv[])
{
@@ -73,7 +38,7 @@ main(int argc, char *argv[])
if (argc < 2)
exit(EXIT_FAILURE);
- cfd = connect_broadcast(argv[1], ECON_PORTNUMBER);
+ cfd = connect_to_host(SOCK_DGRAM, argv[1], STR(ECON_PORTNUMBER));
if (cfd < 0)
exit(EXIT_FAILURE);
diff --git a/util.c b/util.c
index 867a8e0..4df5080 100644
--- a/util.c
+++ b/util.c
@@ -199,6 +199,13 @@ connect_to_host(int socktype, const char *host, const char *port)
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd == -1)
continue;
+ if (socktype == SOCK_DGRAM) {
+ int broadcast_enable = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
+ &broadcast_enable, sizeof(broadcast_enable)) < 0)
+ fprintf(stderr, "Failed to setsockopt broadcast: %s\n",
+ strerror(errno));
+ }
if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
break;