DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 20th May 2013
unkmail unkmail is offline
New User
 
Join Date: May 2013
Posts: 6
Default OpenBSD multicast

How to send multicast datagram via non-default network interface?
I try this:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define IFACE_ADDR      "192.168.2.1"
#define MCAST_ADDR      "239.1.2.3"
#define MCAST_PORT      1234

int main(void)
{
        struct sockaddr_in      ifaddr, mcaddr;
        int                     sock, on;
        char                    msg[] = "test test test";

        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
                err(1, "socket");

        on = 1;
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) == -1)
                err(1, "setsockopt(SO_REUSEADDR)");

        ifaddr.sin_addr.s_addr = inet_addr(IFACE_ADDR);
        if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
            &ifaddr.sin_addr.s_addr, sizeof(ifaddr.sin_addr.s_addr)) == -1)
                err(1, "setsockopt(IP_MULTICAST_IF)");

        memset(&mcaddr, 0, sizeof mcaddr);
        mcaddr.sin_family = AF_INET;
        mcaddr.sin_addr.s_addr = inet_addr(MCAST_ADDR);
        mcaddr.sin_port = (unsigned short)htons(MCAST_PORT);

        if (sendto(sock, msg, sizeof msg, 0,
            (struct sockaddr *)&mcaddr, sizeof mcaddr) == -1)
                err(1, "sendto");
        close(sock);
        return (0);
}
but allways get "sendto: No route to host".
Reply With Quote
  #2   (View Single Post)  
Old 20th May 2013
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

How about adding the missing route?

Code:
$ sudo route delete 224/4
delete net 224/4

$ sudo route add 224/4 192.168.2.1
add net 224/4: gateway 192.168.2.1

$ netstat -rn -f inet

Routing tables

Internet:
Destination        Gateway            Flags   Refs      Use   Mtu  Prio Iface
default            192.168.222.10     UGS        7    10755     -     8 re0  
127/8              127.0.0.1          UGRS       0        0 33152     8 lo0  
127.0.0.1          127.0.0.1          UH         2        0 33152     4 lo0  
127.0.0.10         127.0.0.10         UH         0        0 33152     4 lo1  
192.168.2.1        192.168.2.1        UH         1        0 33152     4 lo2  
192.168.222/24     link#1             UC         2        0     -     4 re0  
192.168.222.10     00:10:5a:14:52:a5  UHLc       1     1163     -     4 re0  
192.168.222.20     127.0.0.1          UG         0        0 33152    56 lo0  
192.168.222.200    link#1             UHLc       1        3     -     4 re0  
224/4              192.168.2.1        UGS        0        0 33152     8 lo2
Please note that because of having only one NIC I had to create an lo2 interface with your example address:

Code:
$ sudo ifconfig lo2 create 

$ sudo ifconfig lo2 192.168.2.1

$ ifconfig lo2
lo2: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33152
        priority: 0
        groups: lo
        inet6 fe80::1%lo2 prefixlen 64 scopeid 0x6
        inet 192.168.2.1 netmask 0xffffff00
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #3   (View Single Post)  
Old 20th May 2013
unkmail unkmail is offline
New User
 
Join Date: May 2013
Posts: 6
Default

Quote:
Originally Posted by J65nko View Post
How about adding the missing route?
Thanks for yours reply!
Sure, yours solution shuld works. But actualy it makes lo2 "primary network interface" for multicast traffic.
My goal send few datagrams from an interface without modifying system wide defaults.

man 4 ip:
Quote:
For hosts with multiple interfaces, each multicast transmission is sent
from the primary network interface. The IP_MULTICAST_IF option overrides
the default for subsequent transmissions from a given socket:

struct in_addr addr;
setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));

where addr is the local IP address of the desired interface or INADDR_ANY
to specify the default interface. An interface's local IP address and
multicast capability can be obtained via the SIOCGIFCONF and SIOCGIFFLAGS
ioctl(2)'s. Normal applications should not need to use this option.
sounds like this should allow what I want... but don't works. maybe I misunderstood something?

ps: sorry for my english.

Last edited by unkmail; 20th May 2013 at 12:51 PM. Reason: spelling
Reply With Quote
  #4   (View Single Post)  
Old 20th May 2013
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

Instead of messing around with the complete 224/4 block, you could add a more specific route:

Code:
$ sudo route add 239.1.2.3 192.168.2.1
add host 239.1.2.3: gateway 192.168.2.1
If this is not acceptable, you probably will have more success in solving this issue by asking on the OpenBSD misc mailing list. I only used sockets in Perl, and those where not for multicast
See http://www.openbsd.org/mail.html
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #5   (View Single Post)  
Old 20th May 2013
unkmail unkmail is offline
New User
 
Join Date: May 2013
Posts: 6
Default

Thanks! This was my stupid bug in pf.conf:
block return on $wifi_if
pass in quick on $wifi_if inet proto udp from any to any port 1234
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Playing IPTV Multicast stream tomageeni OpenBSD General 2 18th July 2010 09:28 PM


All times are GMT. The time now is 02:03 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick