first udp (with multicast try)
This commit is contained in:
		
							parent
							
								
									4b57580f8e
								
							
						
					
					
						commit
						9e4aa98c04
					
				|  | @ -12,6 +12,14 @@ | ||||||
| #include <libubox/avl-cmp.h> | #include <libubox/avl-cmp.h> | ||||||
| #include <libubox/uloop.h> | #include <libubox/uloop.h> | ||||||
| #include <libubox/blobmsg.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 <getopt.h> | ||||||
| #include "hostapd/ieee802_11_defs.h" // ETH_ALEN + hwaddr_aton | #include "hostapd/ieee802_11_defs.h" // ETH_ALEN + hwaddr_aton | ||||||
| #include "hostapd/common.h" | #include "hostapd/common.h" | ||||||
|  |  | ||||||
|  | @ -4,6 +4,10 @@ | ||||||
| #include "wifi_clients.h" | #include "wifi_clients.h" | ||||||
| #include "ubus.h" | #include "ubus.h" | ||||||
| 
 | 
 | ||||||
|  | #ifndef MINI | ||||||
|  | #include "socket.h" | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
| struct option longopts[] = { | struct option longopts[] = { | ||||||
| 	{"verbose", no_argument, 0, 'v'}, | 	{"verbose", no_argument, 0, 'v'}, | ||||||
| 	{"help", no_argument, 0, 'h'}, | 	{"help", no_argument, 0, 'h'}, | ||||||
|  | @ -150,6 +154,15 @@ int main(int argc, char *argv[]) | ||||||
| 		log_error("exit with error on ubus init\n"); | 		log_error("exit with error on ubus init\n"); | ||||||
| 		return ret; | 		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_run(); | ||||||
| 	uloop_done(); | 	uloop_done(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -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() { | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -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 | ||||||
		Loading…
	
		Reference in New Issue