Add multicastsocket

This commit is contained in:
PolynomialDivision 2017-07-13 00:06:17 +02:00
parent 0f144e5055
commit 5a5788edf8
5 changed files with 88 additions and 9 deletions

View file

@ -1,5 +1,5 @@
config 'settings' 'dawn'
option broadcast_ip '10.0.0.255'
option broadcast_ip '224.0.0.1'
option broadcast_port '1025'
option sort_order 'cfsb'
option hostapd_dir '/var/run/hostapd'

View file

@ -1,6 +1,6 @@
#ifndef __DAWN_MULTICASTSTSOCKET_H
#define __DAWN_MULTICASTSSOCKET_H
int setup_multicast_socket();
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr);
#endif

View file

@ -1,11 +1,7 @@
#ifndef __DAWN_NETWORKSOCKET_H
#define __DAWN_NETWORKSOCKET_H
int init_socket_runopts(char *_ip, char *_port, int broadcast_socket);
int send_string(char *msg);
void close_socket();

View file

@ -0,0 +1,71 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "multicastsocket.h"
static struct ip_mreq command; /* static ?! */
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr)
{
int loop = 1;
int sock;
memset (addr, 0, sizeof (*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl (INADDR_ANY);
addr->sin_port = htons (_multicast_port);
if ( ( sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror ("socket()");
exit (EXIT_FAILURE);
}
/* Mehr Prozessen erlauben, denselben Port zu nutzen */
loop = 1;
if (setsockopt ( sock,
SOL_SOCKET,
SO_REUSEADDR,
&loop, sizeof (loop)) < 0) {
perror ("setsockopt:SO_REUSEADDR");
exit (EXIT_FAILURE);
}
if(bind( sock,
(struct sockaddr *)addr,
sizeof(*addr)) < 0) {
perror ("bind");
exit (EXIT_FAILURE);
}
/* Broadcast auf dieser Maschine zulassen */
loop = 1;
if (setsockopt ( sock,
IPPROTO_IP,
IP_MULTICAST_LOOP,
&loop, sizeof (loop)) < 0) {
perror ("setsockopt:IP_MULTICAST_LOOP");
exit (EXIT_FAILURE);
}
/* Join the broadcast group: */
command.imr_multiaddr.s_addr = inet_addr (_multicast_ip);
command.imr_interface.s_addr = htonl (INADDR_ANY);
if (command.imr_multiaddr.s_addr == -1) {
perror ("224.0.0.1 ist keine Multicast-Adresse\n");
exit (EXIT_FAILURE);
}
if (setsockopt ( sock,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
&command, sizeof (command)) < 0) {
perror ("setsockopt:IP_ADD_MEMBERSHIP");
}
return sock;
}

View file

@ -38,7 +38,8 @@ int init_socket_runopts(char *_ip, char *_port, int broadcast_socket) {
sock = setup_broadcast_socket(ip, port, &addr);
} else
{
//sock = setup_multicast_socket(ip, port);
printf("Settingup multicastsocket!\n");
sock = setup_multicast_socket(ip, port, &addr);
}
pthread_t sniffer_thread;
@ -81,11 +82,22 @@ void *receive_msg(void *args) {
int send_string(char *msg) {
int msglen = strlen(msg);
if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&addr,
printf("Sending string!\n");
if (sendto( sock,
msg,
msglen,
0,
(struct sockaddr *) &addr,
sizeof (addr)) < 0) {
perror ("sendto()");
exit (EXIT_FAILURE);
}
/*if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&addr,
sizeof(addr)) != msglen) {
fprintf(stderr, "Failed to send message.\n");
return -1;
}
}*/
return 0;
}