Re: [twsocket] Multicast question

2011-05-27 Thread emanuele bizzarri


Il 26/05/2011 15:28, Éric Fleming Bonilha ha scritto:
 Hi Emanuele,
 
 Thanks for the examples
 
 I actually have a working application with multicast, but I was
 wondering if it was correct, because I tested it with a switch that
 manages IGMP and no multicast groups were being created.
 
 I saw that you set TTL to 32, why? The default 1 would not work? I know
 that 1 would work just on local network (My case)

I used multicast on wan. Our customer wanted this setting.

 
 What I´m confused is with the group creation, how my ICS based
 application with create a multicast group that is visible for switches
 (By using IGMP) and switches can manage this multicast distribution?
 Without that, multicast would be just like broadcast.
 

Using wireshark, when a multicast socket enter in listening state, you
should see igmp packets on the net.
These packets should tell to the switch how to manage multicast
distribution.

 I saw on TWSocket source that when I start listening it actually sets
 the socket to ADD_MEMBERSHIP tp the group, this may be OK for receiving
 data, but I´m confused about sending multicast data, because the sender
 is the one that was supposed to create the multicast group right? But to
 send multicast data on network I need to use Connect method instead of
 Listen from the TWSocket, in this case, on Connect method I didn´t find
 a mention to ADD_MEMBERSHIP or creating a multicast group.

In my implementation, I start listening also for the sender, using
TUDP.SetTx and TUDP.StartRx methods. I use TUDP.Connect method, only for
send udp unicast packets.

I'm not a multicast expert. I found this kind of solution that worked in
my scenario.

Using my implementation, what kind of packet distribution do you get?
Broadcast?

Bye

 
 Thanks
 Eric
 
 
 -Mensagem Original- From: emanuele bizzarri
 Sent: Thursday, May 26, 2011 3:41 AM
 To: twsocket@elists.org
 Subject: Re: [twsocket] Multicast question
 
 Hi,
 this is an example of TUDP class that can be used in multicast (receive
 and transmit).
 This unit depends by other units, but you can easily extract what you need.
 Note that I set multicast ttl.
 I hope it will be usefull for you,
 Bye
 Emanuele
 
 
 unit ewPlatform_UDP;
 
 interface
 
 uses
  ewPlatform_Lists,
  ewPlatform_Logger,
  ewPlatform_Memory,
  ewPlatform_Packets,
  ewPlatform_Strings,
  OverByteIcsWsocket,
  WinSock,
  Windows,
  SysUtils,
  Classes;
 
 type
  TOnUDPError=procedure(aError:integer)of object;
  TOnUDPDataSent=procedure(aInteger:integer)of object;
 
 TOnUDPDataAvailable=procedure(aSender:TObject;aData:pointer;aSize:integer;aPeerIP:string;aPeerPort:integer)
 
 of object;
  TUDP=class
  protected
fWS:TWSocket;
fSent:boolean;
fPeerSrc:TSockAddr;
fPeerSrcLen:integer;
fRxData:pointer;
fRxSize:integer;
fWhiteList:TStringList;
fBlackList:TStringList;
fTxList:TStringList;
procedure DataAvailable(aSender:TObject;aError:word);
procedure DataSent(aSender:TObject;aErrCode:word);
procedure DnsLookupDone(aSender:TObject;aErrCode:word);
procedure ChangeState(aSender:TObject;aOldState,aNewState:TSocketState);
procedure Send;
function GetStarted:boolean;
  public
pLocalIP:string;
pLocalPort:integer;
pPeerIP:string;
pPeerPort:integer;
pMultiThreaded:boolean;
pMulticast:boolean;
pUseWhiteList:boolean;
pUseBlackList:boolean;
pMulticastTTL:integer;
 
pGetPeerByData:boolean;
pOnDataSent:TOnUDPDataSent;
pOnDataAvailable:TOnUDPDataAvailable;
pOnError:TOnUDPError;
pOnChangeState:TChangeState;
 
constructor Create;
destructor Destroy;override;
procedure StartRx;
procedure SetTx;
procedure Disconnect;
procedure Connect;
function Add2WhiteList(aIPPort:string):boolean;
function Add2BlackList(aIPPort:string):boolean;
procedure DeleteFromWhiteList(aIPPort:string);
procedure DeleteFromBlackList(aIPPort:string);
procedure ClearWhiteList;
procedure ClearBlackList;
function NewData(aData:pointer;aSize:integer):integer;
 
property pStarted:boolean read GetStarted;
 
end;
 
 implementation
 
 //--
 
 //TUDP
 //--
 
 constructor TUDP.Create;
 begin
  inherited Create;
  TLogger.Log('UDP socket creation
 threadID='+inttostr(GetCurrentThreadID),lsVerbose,true);
  fWS:=TWSocket.Create(nil);
  fWS.OnDataAvailable:=DataAvailable;
  fWS.OnDataSent:=DataSent;
  fWS.OnDnsLookupDone:=DnsLookUpDone;
  fWS.OnChangeState:=ChangeState;
  fWS.Proto:='udp';
 
  pGetPeerByData:=false;
  fTxList:=TStringList.Create;
  fSent:=false;
 
  pLocalIP:='0.0.0.0';
  pLocalPort:=0;
  pPeerIP:='';
  pPeerPort:=0;
  pMultiThreaded:=false;
  pMulticast:=false;
  pMulticastTTL:=32;
 
  fWhiteList:=TStringList.Create;
  fBlackList:=TStringList.Create;
  pUseWhiteList:=false;
  pUseBlackList:=false

Re: [twsocket] Multicast question

2011-05-27 Thread Éric Fleming Bonilha

Hi Emanuele

Thanks again for the code, now I understand better the concept of IGMP 
groups



I used multicast on wan. Our customer wanted this setting.
I see, I will leave an option for my software for the user to configure the 
TTL, I´m using default 1 (LAN only)



Using wireshark, when a multicast socket enter in listening state, you
should see igmp packets on the net.
These packets should tell to the switch how to manage multicast
distribution.


On my implementation, I was using Connect instead of Listen for the sender, 
so, the sender was not joining the IGMP group, but as I read, the sender is 
not required to join an IGMP group just the receivers must join the group in 
order to receive multicast data



Using my implementation, what kind of packet distribution do you get?
Broadcast?


I actually get Multicast distribution but since I don´t have a managed 
switch over here (that can manage IGMP groups) it works like broadcast, all 
machines on the network receive the multicast data (If I setup wireshark on 
them I can see) not only the machines who joined the group, because this is 
managed by the switch by using IGMP Snooping



Now I have a clearer idea of IGMP, but I would like to know if the sender is 
advised to join the IGMP group, or I can have the sender without joining the 
group


Thanks
Eric 


--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Multicast question

2011-05-26 Thread emanuele bizzarri
Hi,
this is an example of TUDP class that can be used in multicast (receive
and transmit).
This unit depends by other units, but you can easily extract what you need.
Note that I set multicast ttl.
I hope it will be usefull for you,
Bye
Emanuele


unit ewPlatform_UDP;

interface

uses
  ewPlatform_Lists,
  ewPlatform_Logger,
  ewPlatform_Memory,
  ewPlatform_Packets,
  ewPlatform_Strings,
  OverByteIcsWsocket,
  WinSock,
  Windows,
  SysUtils,
  Classes;

type
  TOnUDPError=procedure(aError:integer)of object;
  TOnUDPDataSent=procedure(aInteger:integer)of object;

TOnUDPDataAvailable=procedure(aSender:TObject;aData:pointer;aSize:integer;aPeerIP:string;aPeerPort:integer)
of object;
  TUDP=class
  protected
fWS:TWSocket;
fSent:boolean;
fPeerSrc:TSockAddr;
fPeerSrcLen:integer;
fRxData:pointer;
fRxSize:integer;
fWhiteList:TStringList;
fBlackList:TStringList;
fTxList:TStringList;
procedure DataAvailable(aSender:TObject;aError:word);
procedure DataSent(aSender:TObject;aErrCode:word);
procedure DnsLookupDone(aSender:TObject;aErrCode:word);
procedure ChangeState(aSender:TObject;aOldState,aNewState:TSocketState);
procedure Send;
function GetStarted:boolean;
  public
pLocalIP:string;
pLocalPort:integer;
pPeerIP:string;
pPeerPort:integer;
pMultiThreaded:boolean;
pMulticast:boolean;
pUseWhiteList:boolean;
pUseBlackList:boolean;
pMulticastTTL:integer;

pGetPeerByData:boolean;
pOnDataSent:TOnUDPDataSent;
pOnDataAvailable:TOnUDPDataAvailable;
pOnError:TOnUDPError;
pOnChangeState:TChangeState;

constructor Create;
destructor Destroy;override;
procedure StartRx;
procedure SetTx;
procedure Disconnect;
procedure Connect;
function Add2WhiteList(aIPPort:string):boolean;
function Add2BlackList(aIPPort:string):boolean;
procedure DeleteFromWhiteList(aIPPort:string);
procedure DeleteFromBlackList(aIPPort:string);
procedure ClearWhiteList;
procedure ClearBlackList;
function NewData(aData:pointer;aSize:integer):integer;

property pStarted:boolean read GetStarted;

end;

implementation

//--
//TUDP
//--
constructor TUDP.Create;
begin
  inherited Create;
  TLogger.Log('UDP socket creation
threadID='+inttostr(GetCurrentThreadID),lsVerbose,true);
  fWS:=TWSocket.Create(nil);
  fWS.OnDataAvailable:=DataAvailable;
  fWS.OnDataSent:=DataSent;
  fWS.OnDnsLookupDone:=DnsLookUpDone;
  fWS.OnChangeState:=ChangeState;
  fWS.Proto:='udp';

  pGetPeerByData:=false;
  fTxList:=TStringList.Create;
  fSent:=false;

  pLocalIP:='0.0.0.0';
  pLocalPort:=0;
  pPeerIP:='';
  pPeerPort:=0;
  pMultiThreaded:=false;
  pMulticast:=false;
  pMulticastTTL:=32;

  fWhiteList:=TStringList.Create;
  fBlackList:=TStringList.Create;
  pUseWhiteList:=false;
  pUseBlackList:=false;

  fRxSize:=256*1024;
  getmem(fRxData,fRxSize);

  fPeerSrcLen:=sizeof(fPeerSrc);
end;

destructor TUDP.Destroy;
begin
  Disconnect;
  FreeObjAndNil(fWS);
  FreeMemAndNil(fRxData);
  FreeObjAndNil(fWhiteList);
  FreeObjAndNil(fBlackList);
  FreeList(fTxList);
  FreeObjAndNil(fTxList);
  inherited Destroy;
end;

procedure TUDP.StartRx;
begin
  Disconnect;
  pLocalIP:=trim(pLocalIP);
  if (pLocalIP'')and(pLocalPort0) then
  begin
fWS.Proto:='udp';
fWS.MultiThreaded:=pMultiThreaded;
fWS.Addr:=pLocalIP;
fWS.Port:=inttostr(pLocalPort);
if fWS.Addr='' then
  fWS.Addr:='0.0.0.0';
if pMulticast then
begin
  fWS.MultiCast:=true;
  fWS.ReuseAddr:=true;
  fWS.MultiCastAddrStr:=pPeerIP;
end;
fWS.Listen;
fWS.SocketRcvBufSize:=fRXSize;
if pMulticast then

setsockopt(fWS.HSocket,IPPROTO_IP,IP_MULTICAST_TTL,@pMulticastTTL,sizeof(pMulticastTTL));
  end
  else
TLogger.Log('UDP socket start rx error LocalIP='+pLocalIP+'
LocalPort='+inttostr(pLocalPort),lsError,true);
end;

function TUDP.GetStarted:boolean;
begin
  result:=false;
  if (assigned(fWS)) then
result:=(fWS.State=wsListening)or(fWS.State=wsConnected);
end;

procedure TUDP.SetTx;
var
  lRes:boolean;
begin
  pPeerIP:=trim(pPeerIP);
  lRes:=(pPeerIp'')and(pPeerPort0);
  TLogger.Log('UDP socket set tx PeerIP='+pPeerIP+'
PeerPort='+inttostr(pPeerPort)+'...
'+Boolean2Str(lRes,'ok','ko'),Boolean2Severity(lRes,lsVerbose,lsError),true);
  fPeerSrc.sin_family:=AF_INET;
  fPeerSrc.sin_addr.s_addr:=WSocket_inet_addr(AnsiString(pPeerIP));
  fPeerSrc.sin_port:=htons(word(pPeerPort));
  fSent:=false;
  FreeList(fTxList);
end;

procedure TUDP.Disconnect;
begin
  fWS.Shutdown(SD_BOTH);
  fWS.Close;
  FreeList(fTxList);
  fSent:=false;
end;

procedure TUDP.Connect;
begin
  Disconnect;
  fWS.Proto:='udp';
  fWS.MultiThreaded:=pMultiThreaded;
  fWS.LocalAddr:=pLocalIP;
  fWS.Addr:=trim(pPeerIP);
  fWS.Port:=inttostr(pPeerPort);
  SetTx;
  if (fWS.Addr'')and(pPeerPort0) then
   

Re: [twsocket] Multicast question

2011-05-26 Thread Éric Fleming Bonilha

Hi Emanuele,

Thanks for the examples

I actually have a working application with multicast, but I was wondering if 
it was correct, because I tested it with a switch that manages IGMP and no 
multicast groups were being created.


I saw that you set TTL to 32, why? The default 1 would not work? I know that 
1 would work just on local network (My case)


What I´m confused is with the group creation, how my ICS based application 
with create a multicast group that is visible for switches (By using IGMP) 
and switches can manage this multicast distribution? Without that, multicast 
would be just like broadcast.


I saw on TWSocket source that when I start listening it actually sets the 
socket to ADD_MEMBERSHIP tp the group, this may be OK for receiving data, 
but I´m confused about sending multicast data, because the sender is the one 
that was supposed to create the multicast group right? But to send multicast 
data on network I need to use Connect method instead of Listen from the 
TWSocket, in this case, on Connect method I didn´t find a mention to 
ADD_MEMBERSHIP or creating a multicast group.


Thanks
Eric


-Mensagem Original- 
From: emanuele bizzarri

Sent: Thursday, May 26, 2011 3:41 AM
To: twsocket@elists.org
Subject: Re: [twsocket] Multicast question

Hi,
this is an example of TUDP class that can be used in multicast (receive
and transmit).
This unit depends by other units, but you can easily extract what you need.
Note that I set multicast ttl.
I hope it will be usefull for you,
Bye
Emanuele


unit ewPlatform_UDP;

interface

uses
 ewPlatform_Lists,
 ewPlatform_Logger,
 ewPlatform_Memory,
 ewPlatform_Packets,
 ewPlatform_Strings,
 OverByteIcsWsocket,
 WinSock,
 Windows,
 SysUtils,
 Classes;

type
 TOnUDPError=procedure(aError:integer)of object;
 TOnUDPDataSent=procedure(aInteger:integer)of object;

TOnUDPDataAvailable=procedure(aSender:TObject;aData:pointer;aSize:integer;aPeerIP:string;aPeerPort:integer)
of object;
 TUDP=class
 protected
   fWS:TWSocket;
   fSent:boolean;
   fPeerSrc:TSockAddr;
   fPeerSrcLen:integer;
   fRxData:pointer;
   fRxSize:integer;
   fWhiteList:TStringList;
   fBlackList:TStringList;
   fTxList:TStringList;
   procedure DataAvailable(aSender:TObject;aError:word);
   procedure DataSent(aSender:TObject;aErrCode:word);
   procedure DnsLookupDone(aSender:TObject;aErrCode:word);
   procedure ChangeState(aSender:TObject;aOldState,aNewState:TSocketState);
   procedure Send;
   function GetStarted:boolean;
 public
   pLocalIP:string;
   pLocalPort:integer;
   pPeerIP:string;
   pPeerPort:integer;
   pMultiThreaded:boolean;
   pMulticast:boolean;
   pUseWhiteList:boolean;
   pUseBlackList:boolean;
   pMulticastTTL:integer;

   pGetPeerByData:boolean;
   pOnDataSent:TOnUDPDataSent;
   pOnDataAvailable:TOnUDPDataAvailable;
   pOnError:TOnUDPError;
   pOnChangeState:TChangeState;

   constructor Create;
   destructor Destroy;override;
   procedure StartRx;
   procedure SetTx;
   procedure Disconnect;
   procedure Connect;
   function Add2WhiteList(aIPPort:string):boolean;
   function Add2BlackList(aIPPort:string):boolean;
   procedure DeleteFromWhiteList(aIPPort:string);
   procedure DeleteFromBlackList(aIPPort:string);
   procedure ClearWhiteList;
   procedure ClearBlackList;
   function NewData(aData:pointer;aSize:integer):integer;

   property pStarted:boolean read GetStarted;

   end;

implementation

//--
//TUDP
//--
constructor TUDP.Create;
begin
 inherited Create;
 TLogger.Log('UDP socket creation
threadID='+inttostr(GetCurrentThreadID),lsVerbose,true);
 fWS:=TWSocket.Create(nil);
 fWS.OnDataAvailable:=DataAvailable;
 fWS.OnDataSent:=DataSent;
 fWS.OnDnsLookupDone:=DnsLookUpDone;
 fWS.OnChangeState:=ChangeState;
 fWS.Proto:='udp';

 pGetPeerByData:=false;
 fTxList:=TStringList.Create;
 fSent:=false;

 pLocalIP:='0.0.0.0';
 pLocalPort:=0;
 pPeerIP:='';
 pPeerPort:=0;
 pMultiThreaded:=false;
 pMulticast:=false;
 pMulticastTTL:=32;

 fWhiteList:=TStringList.Create;
 fBlackList:=TStringList.Create;
 pUseWhiteList:=false;
 pUseBlackList:=false;

 fRxSize:=256*1024;
 getmem(fRxData,fRxSize);

 fPeerSrcLen:=sizeof(fPeerSrc);
end;

destructor TUDP.Destroy;
begin
 Disconnect;
 FreeObjAndNil(fWS);
 FreeMemAndNil(fRxData);
 FreeObjAndNil(fWhiteList);
 FreeObjAndNil(fBlackList);
 FreeList(fTxList);
 FreeObjAndNil(fTxList);
 inherited Destroy;
end;

procedure TUDP.StartRx;
begin
 Disconnect;
 pLocalIP:=trim(pLocalIP);
 if (pLocalIP'')and(pLocalPort0) then
 begin
   fWS.Proto:='udp';
   fWS.MultiThreaded:=pMultiThreaded;
   fWS.Addr:=pLocalIP;
   fWS.Port:=inttostr(pLocalPort);
   if fWS.Addr='' then
 fWS.Addr:='0.0.0.0';
   if pMulticast then
   begin
 fWS.MultiCast:=true;
 fWS.ReuseAddr:=true;
 fWS.MultiCastAddrStr:=pPeerIP;
   end;
   fWS.Listen;
   fWS.SocketRcvBufSize:=fRXSize;
   if pMulticast

[twsocket] Multicast question

2011-05-25 Thread Éric Fleming Bonilha
Hi

I´m wondering how to use ICS properly to send and receive Multicast data. Where 
can I find example for multicast and ICS?

I´m in doubt about the IGMP protocol, do I need to implement anything on 
application layer or does IGMP protocol is implemented on underlying stack 
automatically?

How my application will create a multicast group on network and/or join a group?

Thank you very much!
Eric
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be