first udp (with multicast try)

This commit is contained in:
Martin/Geno 2018-08-05 07:58:16 +02:00
parent 4b57580f8e
commit 9e4aa98c04
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
4 changed files with 107 additions and 0 deletions

View File

@ -12,6 +12,14 @@
#include <libubox/avl-cmp.h>
#include <libubox/uloop.h>
#include <libubox/blobmsg.h>
#ifndef MINI
#include <libubox/usock.h>
#include <libubox/ustream.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#endif
#include <getopt.h>
#include "hostapd/ieee802_11_defs.h" // ETH_ALEN + hwaddr_aton
#include "hostapd/common.h"

View File

@ -4,6 +4,10 @@
#include "wifi_clients.h"
#include "ubus.h"
#ifndef MINI
#include "socket.h"
#endif
struct option longopts[] = {
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
@ -150,6 +154,15 @@ int main(int argc, char *argv[])
log_error("exit with error on ubus init\n");
return ret;
}
#ifndef MINI
ret = socket_init("br-lan");
if (ret)
{
socket_close();
log_error("exit with error on socket init\n");
return ret;
}
#endif
uloop_run();
uloop_done();

View File

@ -0,0 +1,73 @@
#include "include.h"
#include "log.h"
#include "socket.h"
static struct uloop_fd server;
static void server_cb(struct uloop_fd *fd, unsigned int events) {
struct sockaddr_in6 addr;
char buf[REQUEST_MAXLEN];
char control[256];
struct iovec iv = {
.iov_base = buf,
.iov_len = sizeof(buf) - 1
};
struct msghdr mh = {
.msg_name = &addr,
.msg_namelen = sizeof(addr),
.msg_iov = &iv,
.msg_iovlen = 1,
.msg_control = control,
.msg_controllen = sizeof(control)
};
if (recvmsg(server.fd, &mh, MSG_WAITALL) < 0) {
log_info("socket: invalid package\n");
return;
}
char addr_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(addr.sin6_addr), addr_str, INET6_ADDRSTRLEN);
log_info("socket:recv [%s]:%d - %s\n", addr_str, ntohs(addr.sin6_port), buf);
if (sendto(server.fd, addr_str, sizeof(addr_str) - 1, 0, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
log_error("socket: could not send answer\n");
}
log_info("socket: send answer\n");
}
int socket_init(char *ifname) {
server.cb = server_cb;
server.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_IPV6ONLY | USOCK_NUMERIC, SOCKET_ADDR, SOCKET_PORT);
if (server.fd < 0) {
return 1;
}
struct in6_addr mgroup_addr;
if (!inet_pton(AF_INET6, SOCKET_MADDR, &mgroup_addr)) {
log_error("socket: invalid multicast group\n");
return -1;
}
struct ipv6_mreq mreq;
mreq.ipv6mr_multiaddr = mgroup_addr;
mreq.ipv6mr_interface = if_nametoindex(ifname);
if (setsockopt(server.fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) {
log_error("socket: unable to join multicast group\n");
return -1;
}
uloop_fd_add(&server, ULOOP_READ);
return 0;
}
void socket_close() {
}

View File

@ -0,0 +1,13 @@
#ifndef __WIFICTLD_SOCKET_H
#define __WIFICTLD_SOCKET_H
#define SOCKET_ADDR "::"
#define SOCKET_MADDR "ff02::31f1"
#define SOCKET_PORT "1000"
#define REQUEST_MAXLEN 256
int socket_init();
void socket_close();
#endif