Problems with IPv6 and UDP

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Problems with IPv6 and UDP

Post by infratec »

Again IPv6 network problems:

Target was to implement a STUN client.
In Linux Mint x64 and PB 6.04 x64 it was working.

Not in WIndows 10 x64 with PB 6.04 x86 and x64

So I wrote a test program:

Code: Select all

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Please enable Thread safe option"
CompilerEndIf


EnableExplicit



Procedure Server(*Dummy)
  
  Protected Server.i, *Buffer, Event.i, Client.i, Len.i, Received$, Exit.i
  
  
  Server = CreateNetworkServer(#PB_Any, 3478, #PB_Network_UDP|#PB_Network_IPv6, "localhost")
  If Server
    
    Debug "Server started"
    
    *Buffer = AllocateMemory(1024, #PB_Memory_NoClear)
    If *Buffer
      
      Repeat
        Event = NetworkServerEvent(Server)
        Select Event
          Case #PB_NetworkEvent_None
            Delay(10)
          Case #PB_NetworkEvent_Data
            Debug "Server event"
            Client = EventClient()
            Len = ReceiveNetworkData(Client, *Buffer, MemorySize(*Buffer))
            If Len > 0
              Received$ = PeekS(*Buffer, Len, #PB_UTF8|#PB_ByteLength)
              Debug "Server received: " + Received$
              Select Received$
                Case "ping"
                  If SendNetworkString(Client, "pong") > 0
                    Debug "Server sent pong"
                  Else
                    Debug "Server send failed"
                  EndIf
                  
                Case "exit"
                  If SendNetworkString(Client, "bye") > 0
                    Debug "Server sent bye"
                  Else
                    Debug "Server send failed"
                  EndIf
                  Exit = #True
              EndSelect
            EndIf
        EndSelect
      Until Exit
      FreeMemory(*Buffer)
    EndIf
    
    CloseNetworkServer(Server)
  EndIf
  
EndProcedure




Define Thread.i, Connection.i, Event.i, State.i, Received$, *Buffer, Len.i, Timeout.q


Thread = CreateThread(@Server(), #Null)
If Thread
  
  Delay(1000)
  
  Connection = OpenNetworkConnection("localhost", 3478, #PB_Network_UDP|#PB_Network_IPv6)
  If Connection
    Debug "Connection open"
    
    *Buffer = AllocateMemory(1024, #PB_Memory_NoClear)
    If *Buffer
      
      Timeout = ElapsedMilliseconds() + 1000 * 5  ; run max 5 seconds
      
      Repeat
        
        Select State
          Case 0 To 2
            If SendNetworkString(Connection, "ping") > 0
              Debug "Client sent ping"
            Else
              Debug "Client send failed"
            EndIf
            State + 1
            
          Case 3
            If SendNetworkString(Connection, "bye") > 0
              Debug "Client sent ping"
            Else
              Debug "Client send failed"
            EndIf
            State + 1
            
        EndSelect
        
        Received$ = ""
        Repeat
          Event = NetworkClientEvent(Connection)
          Select Event
            Case #PB_NetworkEvent_None
              Debug "No client event"
              Delay(500)
            Case #PB_NetworkEvent_Data
              Debug "Client data event"
              Len = ReceiveNetworkData(Connection, *Buffer, MemorySize(*Buffer))
              If Len > 0
                Received$ = PeekS(*Buffer, Len, #PB_UTF8|#PB_ByteLength)
                Debug "Client received: " + Received$
              EndIf
              
          EndSelect
        Until Received$ <> "" Or Timeout - ElapsedMilliseconds() < 0
        
      Until Received$ = "bye" Or Timeout - ElapsedMilliseconds() < 0
      
      FreeMemory(*Buffer)
    EndIf
    
    CloseNetworkConnection(Connection)
  Else
    Debug "No connection to server"
  EndIf
  
Else
  Debug "Thread crfeate failed"
EndIf

It fails on both OS but with different results.:
In WIndows the server receives the ping and answer with pong, but the client gets no event.
In Linux the client send the ping, but the server get no event.
At the moment I can not test it on macOS.

Can someone with IPv6 can confirm this, or I am too stupid to code something like this?

Networking with IPv6 (espacially UDP) and PB drives me crazy.
User avatar
idle
Always Here
Always Here
Posts: 6040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Massive problems with IPv6 and UDP

Post by idle »

I couldn't get it working with ipv6 not sure what's going on

for hole punching have you looked at libjuice
https://github.com/paullouisageneau/libjuice

I came across libdatachannel the other day which presents a native c api for WebRTC and it uses libjuice to do hole punching.
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Massive problems with IPv6 and UDP

Post by infratec »

It works in macOS Ventura 13.5.1 with PB 6.04 x64 C backend.

I get 3 ping pongs.
User avatar
idle
Always Here
Always Here
Posts: 6040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Massive problems with IPv6 and UDP

Post by idle »

Ipv4 worked on win 11 but ipv6 failed.
There is an oddity on windows, ipv6 is always active on localhost even if it's disabled on the network adapter so maybe that has something to do with it.
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Massive problems with IPv6 and UDP

Post by infratec »

The original 'bug' occurs on a real interface with a real IPv6 address (2a00:...)
I only wrote the example for localhost, to make it more easy to test.
User avatar
idle
Always Here
Always Here
Posts: 6040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Massive problems with IPv6 and UDP

Post by idle »

infratec wrote: Thu Dec 21, 2023 8:26 pm The original 'bug' occurs on a real interface with a real IPv6 address (2a00:...)
I only wrote the example for localhost, to make it more easy to test.
could you use libjuice?
https://github.com/paullouisageneau/lib ... ectivity.c
https://github.com/paullouisageneau/lib ... t/server.c
and there's also violet but looks to be linux only build
https://github.com/paullouisageneau/violet

I still can't get it to work, wonder what the socket options are?
Post Reply