2014-07-11 22:12:11 +02:00
|
|
|
/*
|
|
|
|
This file is part of actube.
|
|
|
|
|
|
|
|
actube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pthread.h>
|
2016-02-18 09:25:45 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
2016-03-03 20:46:20 +01:00
|
|
|
#include "cw/log.h"
|
|
|
|
#include "cw/sock.h"
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include "socklist.h"
|
|
|
|
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
struct socklistelem *socklist = 0;
|
2014-07-11 22:12:11 +02:00
|
|
|
int socklist_len;
|
|
|
|
static pthread_mutex_t socklist_mutex;
|
2016-02-17 20:26:59 +01:00
|
|
|
static int socklist_wtpcount = 0;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
int socklist_init()
|
|
|
|
{
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (pthread_mutex_init(&socklist_mutex, NULL))
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
socklist = malloc(sizeof(struct socklistelem) * SOCKLIST_SIZE);
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(socklist, 0, sizeof(struct socklistelem) * SOCKLIST_SIZE);
|
|
|
|
if (!socklist) {
|
|
|
|
cw_log(LOG_ERR, "Fatal error while initializing socklist: %s",
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void socklist_lock()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&socklist_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void socklist_unlock()
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&socklist_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void socklist_destroy()
|
|
|
|
{
|
|
|
|
int i;
|
2016-02-17 20:26:59 +01:00
|
|
|
for (i = 0; i < socklist_len; i++) {
|
2014-07-11 22:12:11 +02:00
|
|
|
close(socklist[i].sockfd);
|
|
|
|
}
|
|
|
|
free(socklist);
|
2016-02-17 20:26:59 +01:00
|
|
|
socklist = 0;
|
2014-07-11 22:12:11 +02:00
|
|
|
pthread_mutex_destroy(&socklist_mutex);
|
|
|
|
}
|
|
|
|
|
2016-02-15 19:16:02 +01:00
|
|
|
/**
|
|
|
|
* Find a good reply socket (only for IPv4)
|
|
|
|
* @param sa source address
|
|
|
|
* @return socket or -1 if no socket was found
|
|
|
|
*/
|
2016-02-18 09:25:45 +01:00
|
|
|
int socklist_find_reply_socket(struct sockaddr *sa, int port)
|
2016-02-15 19:16:02 +01:00
|
|
|
{
|
2016-02-17 20:26:59 +01:00
|
|
|
int bestindex = -1;
|
2016-02-15 19:16:02 +01:00
|
|
|
int i;
|
2018-03-31 14:13:07 +02:00
|
|
|
uint32_t addr,mask,saddr;
|
|
|
|
|
2016-02-15 19:16:02 +01:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
for (i = 0; i < socklist_len; i++) {
|
|
|
|
/* we want only an unicast socket for reply */
|
2016-02-15 19:16:02 +01:00
|
|
|
if (socklist[i].type != SOCKLIST_UNICAST_SOCKET)
|
|
|
|
continue;
|
2016-02-17 20:26:59 +01:00
|
|
|
|
|
|
|
/* and we want only sockets with same sa_family */
|
|
|
|
if (sa->sa_family != socklist[i].addr.sa_family)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
/* the first fd would be always the best, if we don't
|
2016-02-15 19:16:02 +01:00
|
|
|
* find later a better one */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (bestindex == -1) {
|
|
|
|
bestindex = i;
|
2016-02-15 19:16:02 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
/* we want first the same port */
|
2016-02-18 09:25:45 +01:00
|
|
|
if (sock_getport(&socklist[i].addr) != port) {
|
2016-02-17 20:26:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we havn't already found a socket with same port
|
|
|
|
* this is now our best socket*/
|
2016-02-18 09:25:45 +01:00
|
|
|
if (sock_getport(&socklist[bestindex].addr) != port) {
|
|
|
|
bestindex = i;
|
2016-02-17 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the next checks only aply to IPv4 */
|
|
|
|
if (socklist[i].addr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
2016-02-17 11:17:30 +01:00
|
|
|
/* get our source address and netmask */
|
2018-03-31 14:13:07 +02:00
|
|
|
addr =
|
2016-02-17 20:26:59 +01:00
|
|
|
((struct sockaddr_in *) &socklist[i].addr)->sin_addr.s_addr;
|
2018-03-31 14:13:07 +02:00
|
|
|
mask =
|
2016-02-17 20:26:59 +01:00
|
|
|
((struct sockaddr_in *) &socklist[i].netmask)->sin_addr.s_addr;
|
2016-02-15 19:16:02 +01:00
|
|
|
|
2016-02-17 11:17:30 +01:00
|
|
|
/* get source of requested address */
|
2018-03-31 14:13:07 +02:00
|
|
|
saddr = ((struct sockaddr_in *) sa)->sin_addr.s_addr;
|
2016-02-15 19:16:02 +01:00
|
|
|
|
2016-02-18 09:25:45 +01:00
|
|
|
/* if the request comes from the same subnet where our
|
|
|
|
* socket is cconnected, this is our new best socked.
|
|
|
|
* So we can serve AP's w*here no deault route is configured */
|
2016-02-17 20:26:59 +01:00
|
|
|
if ((addr & mask) == (saddr & mask)) {
|
|
|
|
bestindex = i;
|
2016-02-15 19:16:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (bestindex != -1)
|
|
|
|
return socklist[bestindex].sockfd;
|
|
|
|
return -1;
|
2016-02-15 19:16:02 +01:00
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
static int find_reply_socket(struct sockaddr *sa, int bc)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-31 14:13:07 +02:00
|
|
|
unsigned int snlen;
|
|
|
|
struct sockaddr_storage bcaddr;
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
int bestsockfd = -1;
|
|
|
|
int i;
|
2016-02-17 20:26:59 +01:00
|
|
|
for (i = 0; i < socklist_len; i++) {
|
2014-07-11 22:12:11 +02:00
|
|
|
struct sockaddr_storage sn;
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(&sn, 0, sizeof(sn));
|
2018-03-31 14:13:07 +02:00
|
|
|
snlen = sizeof(struct sockaddr_storage);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (getsockname(socklist[i].sockfd, (struct sockaddr *) &sn, &snlen) < 0) {
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (sa->sa_family != sn.ss_family)
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (sn.ss_family == AF_INET) {
|
|
|
|
int p1 = ntohs(((struct sockaddr_in *) sa)->sin_port);
|
|
|
|
int p2 = ntohs(((struct sockaddr_in *) &sn)->sin_port);
|
2014-07-26 20:23:46 +02:00
|
|
|
if (p1 != p2)
|
|
|
|
continue;
|
2016-02-17 20:26:59 +01:00
|
|
|
|
2014-07-26 20:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
if (bestsockfd == -1)
|
|
|
|
bestsockfd = socklist[i].sockfd;
|
|
|
|
|
|
|
|
if (!bc)
|
|
|
|
return bestsockfd;
|
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (!sock_getbroadcastaddr
|
|
|
|
((struct sockaddr *) &sn, (struct sockaddr *) &bcaddr))
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (sock_cmpaddr((struct sockaddr *) &bcaddr, sa, 0))
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bestsockfd = socklist[i].sockfd;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
|
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
return bestsockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void socklist_add_connection(int index)
|
|
|
|
{
|
|
|
|
socklist_lock();
|
|
|
|
socklist[index].wtpcount++;
|
|
|
|
socklist_wtpcount++;
|
|
|
|
socklist_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void socklist_del_connection(int index)
|
|
|
|
{
|
|
|
|
socklist_lock();
|
|
|
|
socklist[index].wtpcount--;
|
|
|
|
socklist_wtpcount--;
|
|
|
|
socklist_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
int socklist_add_multicast(const char *addr, const char *port, int ac_proto)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
struct addrinfo hints;
|
2016-02-17 20:26:59 +01:00
|
|
|
struct addrinfo *res, *res0;
|
2018-03-31 14:13:07 +02:00
|
|
|
int rc;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
2016-02-17 20:26:59 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
rc = getaddrinfo(addr, port, &hints, &res0);
|
2016-02-17 20:26:59 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
cw_log(LOG_ERR, "Can't bind multicast address '%s': %s", addr,
|
|
|
|
gai_strerror(rc));
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
for (res = res0; res; res = res->ai_next) {
|
2014-07-11 22:12:11 +02:00
|
|
|
struct sockaddr *sa = res->ai_addr;
|
2018-03-31 14:13:07 +02:00
|
|
|
void *opt;
|
|
|
|
int optlen;
|
|
|
|
int rfd;
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
int sockfd = socket(res->ai_addr->sa_family, SOCK_DGRAM, 0);
|
|
|
|
/* create socket */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (sockfd == -1) {
|
|
|
|
cw_log(LOG_ERR, "Can't create multicast socket: %",
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* bind address */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (bind(sockfd, sa, sock_addrlen(sa)) < 0) {
|
2014-07-11 22:12:11 +02:00
|
|
|
close(sockfd);
|
2016-03-06 12:44:48 +01:00
|
|
|
cw_log(LOG_ERR, "Can't bind multicast %s: %s", addr,
|
2016-02-17 20:26:59 +01:00
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use setsockopt() to request that the kernel joins a multicast group */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (res->ai_addr->sa_family == AF_INET) {
|
2018-03-31 14:13:07 +02:00
|
|
|
struct sockaddr_in *sain;
|
|
|
|
char sinin[100];
|
2014-07-11 22:12:11 +02:00
|
|
|
struct ip_mreq mreq;
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(&mreq, 0, sizeof(mreq));
|
2018-03-31 14:13:07 +02:00
|
|
|
sain = (struct sockaddr_in *) res->ai_addr;
|
2016-02-17 20:26:59 +01:00
|
|
|
mreq.imr_multiaddr.s_addr = sain->sin_addr.s_addr;
|
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
2014-07-11 22:12:11 +02:00
|
|
|
opt = &mreq;
|
2016-02-17 20:26:59 +01:00
|
|
|
optlen = sizeof(mreq);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
|
2016-03-06 11:23:05 +01:00
|
|
|
sock_addrtostr((struct sockaddr *) sain, sinin, 100,1);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, opt, optlen)
|
|
|
|
< 0) {
|
2014-07-11 22:12:11 +02:00
|
|
|
close(sockfd);
|
2016-02-17 20:26:59 +01:00
|
|
|
cw_log(LOG_ERR, "Can't add multicast membership %s: %s",
|
|
|
|
addr, strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-02-17 20:26:59 +01:00
|
|
|
if (res->ai_addr->sa_family == AF_INET6) {
|
2014-07-11 22:12:11 +02:00
|
|
|
struct ipv6_mreq mreq;
|
2018-03-31 14:13:07 +02:00
|
|
|
struct sockaddr_in6 *sain6;
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(&mreq, 0, sizeof(mreq));
|
2018-03-31 14:13:07 +02:00
|
|
|
sain6 = (struct sockaddr_in6 *) res->ai_addr;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
memcpy(&mreq.ipv6mr_multiaddr.s6_addr, &sain6->sin6_addr.s6_addr,
|
|
|
|
sizeof(sain6->sin6_addr.s6_addr));
|
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
mreq.ipv6mr_interface = 0; /* htonl(INADDR_ANY);*/
|
2014-07-11 22:12:11 +02:00
|
|
|
opt = &mreq;
|
2016-02-17 20:26:59 +01:00
|
|
|
optlen = sizeof(mreq);
|
|
|
|
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, opt, optlen)
|
|
|
|
< 0) {
|
2014-07-11 22:12:11 +02:00
|
|
|
close(sockfd);
|
2016-02-17 20:26:59 +01:00
|
|
|
cw_log(LOG_ERR, "Can't join multicast group %s: %s", addr,
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
rfd = find_reply_socket(sa, 0);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
socklist[socklist_len].sockfd = sockfd;
|
2018-03-26 15:11:57 +02:00
|
|
|
/*// socklist[socklist_len].reply_sockfd = rfd;*/
|
2016-02-17 20:26:59 +01:00
|
|
|
socklist[socklist_len].type = SOCKLIST_BCASTMCAST_SOCKET;
|
|
|
|
socklist[socklist_len].family = sa->sa_family;
|
|
|
|
socklist[socklist_len].ac_proto = ac_proto;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
socklist_len++;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
cw_log(LOG_INFO, "Bound to multicast group: %s (fd=%i,r:%i)", addr,
|
|
|
|
sockfd, rfd);
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-18 09:25:45 +01:00
|
|
|
static int socklist_check_size()
|
|
|
|
{
|
|
|
|
if (socklist_len>0 && (socklist_len % SOCKLIST_SIZE)==0){
|
|
|
|
struct socklistelem *newsocklist;
|
|
|
|
newsocklist = realloc(socklist, sizeof(struct socklistelem)*(socklist_len+SOCKLIST_SIZE));
|
|
|
|
if (!newsocklist) {
|
|
|
|
cw_log(LOG_ERR,"Can't increase socklist size, realoc failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
socklist = newsocklist;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
int socklist_add_unicast(const char *addr, const char *port, int ac_proto)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-05 12:23:16 +01:00
|
|
|
char sock_buf[SOCK_ADDR_BUFSIZE];
|
2018-03-31 14:13:07 +02:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res, *res0;
|
|
|
|
int rc;
|
2016-03-06 12:44:48 +01:00
|
|
|
|
2016-02-18 09:25:45 +01:00
|
|
|
if (!socklist_check_size())
|
|
|
|
return 0;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
2016-02-17 20:26:59 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
rc = getaddrinfo(addr, port, &hints, &res0);
|
2016-02-17 20:26:59 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
cw_log(LOG_ERR, "Can't bind unicast address '%s': %s", addr,
|
|
|
|
gai_strerror(rc));
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
for (res = res0; res; res = res->ai_next) {
|
2016-02-15 19:16:02 +01:00
|
|
|
char ifname[64];
|
|
|
|
struct sockaddr netmask;
|
|
|
|
struct sockaddr broadcast;
|
2018-03-31 14:13:07 +02:00
|
|
|
struct sockaddr *sa;
|
|
|
|
int sockfd;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
ifname[0] = 0;
|
|
|
|
rc = sock_getifinfo(res->ai_addr, ifname, &broadcast, &netmask);
|
2016-02-15 19:16:02 +01:00
|
|
|
if (!rc) {
|
2016-02-17 20:26:59 +01:00
|
|
|
cw_log(LOG_ERR, "No interface found for %s, can't bind.", addr);
|
2016-02-15 19:16:02 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:04:00 +01:00
|
|
|
/* Bind the control port */
|
2018-03-31 14:13:07 +02:00
|
|
|
sa = res->ai_addr;
|
|
|
|
sockfd = socket(res->ai_addr->sa_family, SOCK_DGRAM, 0);
|
2014-07-11 22:12:11 +02:00
|
|
|
/* create socket */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (sockfd == -1) {
|
|
|
|
cw_log(LOG_ERR, "Can't create unicast socket: %s",
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bind address */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (bind(sockfd, sa, sock_addrlen(sa)) < 0) {
|
2014-07-11 22:12:11 +02:00
|
|
|
close(sockfd);
|
2016-03-06 12:44:48 +01:00
|
|
|
cw_log(LOG_ERR, "Can't bind unicast socket %s: %s", addr,
|
2016-02-17 20:26:59 +01:00
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
socklist[socklist_len].sockfd = sockfd;
|
2018-03-26 15:11:57 +02:00
|
|
|
/*// socklist[socklist_len].reply_sockfd = sockfd;*/
|
2016-02-17 20:26:59 +01:00
|
|
|
socklist[socklist_len].family = sa->sa_family;
|
|
|
|
socklist[socklist_len].type = SOCKLIST_UNICAST_SOCKET;
|
|
|
|
socklist[socklist_len].ac_proto = ac_proto;
|
|
|
|
|
|
|
|
|
2016-03-20 15:04:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
if (res->ai_addr->sa_family == AF_INET) {
|
|
|
|
memcpy(&socklist[socklist_len].netmask, &netmask,
|
|
|
|
sock_addrlen(&netmask));
|
|
|
|
memcpy(&socklist[socklist_len].addr, res->ai_addr,
|
|
|
|
sock_addrlen(res->ai_addr));
|
|
|
|
cw_log(LOG_INFO,
|
|
|
|
"Bound to: %s:%s (%i) on interface %s, netmask %s", addr,
|
2018-03-05 12:23:16 +01:00
|
|
|
port, sockfd, ifname, sock_addr2str(&netmask,sock_buf));
|
2016-02-17 20:26:59 +01:00
|
|
|
} else {
|
2016-03-06 12:44:48 +01:00
|
|
|
cw_log(LOG_INFO, "Bound to: [%s]:%s (%i) on interface %s", addr,
|
2016-02-17 20:26:59 +01:00
|
|
|
port, sockfd, ifname);
|
2016-02-15 19:16:02 +01:00
|
|
|
}
|
2016-03-20 15:04:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Bind the data port */
|
|
|
|
sa = res->ai_addr;
|
|
|
|
|
|
|
|
/* XXX data port is currently hard coded */
|
|
|
|
sock_setport(sa,5247);
|
|
|
|
|
|
|
|
sockfd = socket(res->ai_addr->sa_family, SOCK_DGRAM, 0);
|
|
|
|
/* create socket */
|
|
|
|
if (sockfd == -1) {
|
2016-03-20 15:48:44 +01:00
|
|
|
cw_log(LOG_ERR, "Can't create unicast data socket: %s",
|
2016-03-20 15:04:00 +01:00
|
|
|
strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bind address */
|
|
|
|
if (bind(sockfd, sa, sock_addrlen(sa)) < 0) {
|
|
|
|
close(sockfd);
|
2016-03-20 15:48:44 +01:00
|
|
|
cw_log(LOG_ERR, "Can't bind unicast data socket %s: %s", addr,
|
2016-03-20 15:04:00 +01:00
|
|
|
strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:48:44 +01:00
|
|
|
cw_log(LOG_INFO, "Bound to data port: [%s]:%s (%i) on interface %s", addr,
|
|
|
|
"5247", sockfd, ifname);
|
2016-03-20 15:04:00 +01:00
|
|
|
|
|
|
|
socklist[socklist_len].data_sockfd = sockfd;
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
socklist_len++;
|
2016-02-15 19:16:02 +01:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
freeaddrinfo(res0);
|
2014-07-11 22:12:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
int socklist_add_broadcast(const char *addr, const char *port, int ac_proto)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-31 14:13:07 +02:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res, *res0;
|
|
|
|
int rc;
|
|
|
|
int sockfd;
|
|
|
|
|
2016-02-18 09:25:45 +01:00
|
|
|
if (!socklist_check_size())
|
|
|
|
return 0;
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
2016-02-17 20:26:59 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
rc = getaddrinfo(addr, port, &hints, &res0);
|
2016-02-17 20:26:59 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
cw_log(LOG_ERR, "Can't bind broadcast address '%s': %s", addr,
|
|
|
|
gai_strerror(rc));
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
for (res = res0; res; res = res->ai_next) {
|
|
|
|
int rfd;
|
2014-07-11 22:12:11 +02:00
|
|
|
struct sockaddr *sa = res->ai_addr;
|
|
|
|
sockfd = socket(res->ai_addr->sa_family, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
/* create socket */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (sockfd == -1) {
|
|
|
|
cw_log(LOG_ERR, "Can't create broadcast socket: %",
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef IP_BINDANY
|
2016-02-17 20:26:59 +01:00
|
|
|
struct sockaddr_in *sain = (struct sockaddr_in *) sa;
|
|
|
|
if (sain->sin_addr.s_addr == INADDR_BROADCAST) {
|
|
|
|
int opt = 1;
|
|
|
|
if (setsockopt(sockfd, IPPROTO_IP, IP_BINDANY, &opt, sizeof(opt))) {
|
|
|
|
cw_log(LOG_ERR, "Can't set sockopt IP_BIND_ANY: %s",
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
2016-02-17 20:26:59 +01:00
|
|
|
#endif
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
/* bind address */
|
2016-02-17 20:26:59 +01:00
|
|
|
if (bind(sockfd, sa, sock_addrlen(sa)) < 0) {
|
2014-07-11 22:12:11 +02:00
|
|
|
close(sockfd);
|
2016-02-17 20:26:59 +01:00
|
|
|
sockfd = -1;
|
|
|
|
cw_log(LOG_ERR, "Can't bind broadcast %s: %s", addr,
|
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-31 14:13:07 +02:00
|
|
|
rfd = find_reply_socket(sa, 1);
|
2016-02-17 20:26:59 +01:00
|
|
|
|
|
|
|
socklist[socklist_len].sockfd = sockfd;
|
2018-03-26 15:11:57 +02:00
|
|
|
/*// socklist[socklist_len].reply_sockfd = rfd;*/
|
2016-02-17 20:26:59 +01:00
|
|
|
socklist[socklist_len].type = SOCKLIST_BCASTMCAST_SOCKET;
|
|
|
|
socklist[socklist_len].family = sa->sa_family;
|
|
|
|
socklist[socklist_len].ac_proto = ac_proto;
|
|
|
|
|
|
|
|
memcpy(&socklist[socklist_len].addr, res->ai_addr,
|
2016-02-18 09:25:45 +01:00
|
|
|
sock_addrlen(res->ai_addr));
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-26 15:11:57 +02:00
|
|
|
/*// printf ("AC INIT PROTO : %d, i %i\n",ac_proto,socklist_len);
|
2016-02-17 20:26:59 +01:00
|
|
|
// printf ("sock proto %d\n",socklist[socklist_len].ac_proto);
|
2018-03-26 15:11:57 +02:00
|
|
|
*/ socklist_len++;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
cw_log(LOG_INFO, "Bound to broadcast: %s:%s (%i,R:%i,I:%d)", addr, port,
|
|
|
|
sockfd, rfd, socklist_len - 1);
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 20:26:59 +01:00
|
|
|
freeaddrinfo(res0);
|
|
|
|
return 1;
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|