27 Eylül 2017 Çarşamba

UDP ve Multicast

UDP ve Multicast
Multicast (çok yöne yayın) verinin gruptaki tüm unsurlara ulaştırma tekniği. UDP socketi açarak D sınıfı adres aralığını yani 224.0.0.1 ila 239.255.255.255 arasındaki bir adresi kullanmak gerekiyor.

Şimdiye kadar şu adresi kullandım
231.11.1.5:6005

IPv6
Açıklaması şöyle. IPv6, multicast olmadan bir çok hizmeti çalıştıramaz.
In IPv6, there’s no longer any broadcast – sending one packet to a large number of unspecified hosts. There’s only multicast, unicast and anycast. In IPv6 all nodes are required to support multicast. Without multicast, many services that you need will simply not work.
Link Local Adres
Açıklaması şöyle
There are certain blocks of multicast address space reserved for certain things. For example, the 224.0.0.0/24 block is reserved for local network control (Link-Local), and multicast addresses in this block should not be forwarded to another LAN, even with multicast routing enabled.

Multicast Çeşitleri
Multicast public internette routerlar kopyalama yapmadığı için çalışmaz! Multicast iki çeşit olabilir.

1. DM (Dense Mode) : Multicast her router'a gönderiliz.
2. SM (Sparse Mode) : Randevu noktası satın alınır.

Layer 2 switch'ler multicast paketleri tüm portlarına yayarlar. Örnekte Machine A0 bir multicast paket gönderirse Switch A paketi Switch B'ye de gönderir. Switch B de paketi Machine B0 ve Machine B1'e gönderir.
(Machine A0) --- [Switch A] ============ [Switch B] --- (Machine B0)

                                             |

                                             |
                                        (Machine B1)
Multicast kullanırken SO_REUSEADDR seçeneğinden biraz daha farklı olan SO_REUSEPORT seçeneği karşımıza çıkıyor. Bu seçenekte iki tane multicast soketi aynı portu dinleyebiliyorlar.

Tunnel
Tunnel için açıklama şöyle
A tunnel is a mechanism used to ship a foreign protocol across a network that normally wouldn't support it. Tunneling protocols allow you to use, for example, IP to send another protocol in the "data" portion of the IP datagram. Most tunneling protocols operate at layer 4, which means they are implemented as a protocol that replaces something like TCP or UDP.
Açıklaması şöyle
Tunneling is a method used to transfer a payload of one protocol using an internetwork transportation medium of another protocol
...
Both Layer 2 and Layer 3 use tunneling. Typical Layer 2 tunneling protocols are PPTP (Point-to-Point Tunneling Protocol) and L2TP (Layer Two Tunneling Protocol). Layer 3 usually uses IPSec tunnel mode as a tunneling protocol.
GRE (Generic Routing Encapsulation) Tunnel
GRE Cisco tarafından geliştirilmiştir. IP omurgada "diğer" tipte paketkeri taşır. Internette multicast için GRE Tunnel kullanılır.Paketleri şifrelemez. Açıklaması şöyle
A tunnel wraps packets inside other packets, so you have different packet headers for the outer packets. This is true even if the tunnel is not encrypted, e.g. GRE.
Açıklaması şöyle
GRE is a simple IP packet encapsulation protocol. a GRE tunnel is used when packets need to be sent from one network to another, without being parsed or treated like IP packets by any intervening routers. a GRE tunnel interface comes up as soon as it is configured and it stays up as long as there is a valid tunnel source address or interface which is up.
GRE şöyledir.
65001(Asia)                65001(North America)   
  \                               /                 
   +-- 65000 =---gre---= 65000 --+
       (Asia)        (North America)
L2TP
Açıklaması şöyle. VPN için kullanılır.
L2TP is a tunneling protocol (used to support VPNs) that allows multiplexing of multiple PPP sessions between two IP-connected endpoints, and a control protocol for dynamically establishing and maintaining the emulation of these PPP sessions. This is very different than GRE.
ESP Tunnel
Paketleri şifreler. Açıklaması şöyle
When Encapsulating Security Payload (ESP) is used in the tunnel mode, a new IP header is computed and added.

C
Göndermek için
Multicast göndermek için bir gruba dahil olmak şart değildir. Ancak dinlemek için şarttır.
Loopback
Kendi multicast paketlerimizi alıp almayacağımızı ayarlarız. Örnek:
char loopch=0;//Almamak için 0, almak için 1 ata
setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP,&loopch, sizeof(loopch));

 
IP_MULTICAST_IF
Genellikle multicast ethernet kartı sistem yöneticisi tarafından ayarlanır ancak program içinden seçmek için IP_MULTICAST_IF kullanılır. Seçilen kartın multicast kabiliyeti yoksa hata verir. Örnek:

struct in_addr        localInterface;
localInterface.s_addr = inet_addr("9.5.1.1");
setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF,&localInterface,sizeof(localInterface));

Almak için
IP_ADD_MEMBERSHIP
Almak için, alıcının multicast grubuna abone olması gerekir.

1. Önce ip_mreq sınıfı doldurulur.imr_multiaddr alanına IP adresini vermek mecburidir. Bu değer 224.0.0.0 - 239.255.255.255 arasında bir adres olmalıdır.

2. Sonra imr_interface alanına ethernet kartının IP adresini verebiliriz ya da INADDR_ANY genel adresini kullanarak tüm kartlardan gelen multicast paketlerini okuyabiliriz. İkinci seçenek daha kolay.

Örnek 1
Ethernet kartının adresini veren bir örnek şöyle.
// Join multicast group.
ip_mreq mreq = {};
mreq.imr_multiaddr.s_addr = inet_addr(mcast_ip.c_str());
mreq.imr_interface.s_addr = inet_addr(mcast_interface_ip.c_str());
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{...error...}
Örnek 2
Multicast adresi yanlış verilirse - örneğin 127.0.0.1 - hata alırız.
const char* host = "127.0.0.1"; 
ip_mreq mcast; 
mcast.imr_multiaddr.s_addr = inet_addr(host);
mcast.imr_interface.s_addr = htonl(INADDR_ANY);
int res = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast, sizeof(mcast));
Sonuç olarak - 1 alırız.
Örnek 3
Şöyle yaparız.
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"

mreq.imr_multiaddr.s_addr=inet_addr(HELLO_GROUP);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
  perror("setsockopt");
  ...
}

IP_DROP_MEMBERSHIP
Abone olmak için kullanılan ip_mreq yapısını bu sefer aboneliği iptal etmek için kullanırız.
struct ip_mreq mreq;
setsockopt (socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));


C#
Sadece multicast paket göndermek için şunu yapmak yeterli.
UdpClient.Send(byte[] dgram, int bytes, IPEndPoint endPoint)
endpoint olarak
endPoint = new IPEndPoint(IPAddress.Broadcast, <port number>)
kullanamak gerekir.

Eğer multicast göndermek ve almak istiyorsak şöyle yapmak gerekir
m_udpClientReceiver = new UdpClient();
m_receivingEndPoint = new IPEndPoint(IPAddress.Any, m_port);
m_udpClientReceiver.ExclusiveAddressUse = false;
m_udpClientReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, 
                                           SocketOptionName.ReuseAddress, true);
m_udpClientReceiver.ExclusiveAddressUse = false;
m_udpClientReceiver.Client.Bind(m_receivingEndPoint);
m_udpClientReceiver.JoinMulticastGroup(m_multicastAddress, 255);
Java
MulticastSocket kullanılır.

Hiç yorum yok:

Yorum Gönder