Author Topic: Refuse connections from Public IPs  (Read 636 times)

kafffee

  • Posts: 262
Refuse connections from Public IPs
« on: 16 May '24 - 14:02 »
Hello there :-)

I am starting a WMA stream like this:

Code: [Select]
Dim ClientConnectProc = New CLIENTCONNECTPROC(AddressOf MyClientConnectNotify)

success = BassWma.BASS_WMA_EncodeOpenNetwork(MainModule.mixer, 2, BASSWMAEncode.BASS_WMA_ENCODE_SOURCE, 128000, LayerViewModel.SettingsViewModel.Portnumber, LayerViewModel.SettingsViewModel.MaxListeners)
BassWma.BASS_WMA_EncodeSetNotify(success, ClientConnectProc, IntPtr.Zero)

Code: [Select]
Private Sub MyClientConnectNotify(handle As Integer, connect As Boolean, ip As String, user As IntPtr)

    Dim RefuseConnection As Boolean

    If (LayerViewModel.SettingsViewModel.OnlyGrantLAN) And (Not CheckIfIPisLocal(ip)) Then
        RefuseConnection = True
    End If

    If connect Then
        Listeners = Listeners + 1
        ListenerPeak = ListenerPeak + 1
    Else
        Listeners = Listeners - 1
    End If
End Sub

Is there a way to refuse incoming connections for IP addresses other than local?

Ian @ un4seen

  • Administrator
  • Posts: 26028
Re: Refuse connections from Public IPs
« Reply #1 on: 16 May '24 - 18:06 »
There are 3 private address ranges (IPv4), as described here:

   https://en.wikipedia.org/wiki/IP_address#Private_addresses

So you could check the "ip" parameter for each of them. I'm afraid I can't advise on how that would be done in VB.Net, but it could look something like this in C:

Code: [Select]
bool isprivate = false;
if (!strncmp(ip, "10.", 3)) isprivate = true;
else if (!strncmp(ip, "192.168.", 8)) isprivate = true;
else if (!strncmp(ip, "174.", 4)) {
int n2 = atoi(ip + 4);
if (n2 >= 16 && n2 <= 31) isprivate = true;
}

You may also want to check for addresses starting with "127." to allow connections from the same system.

kafffee

  • Posts: 262
Re: Refuse connections from Public IPs
« Reply #2 on: 16 May '24 - 18:41 »
Hey Ian,

good to hear from you. I know about the address ranges, I have already implemented a function that tells public from private IPs (CheckIfIPIsLocal).

So this would be covered.

The question was more how to actually refuse the connection when the function tells me that the IP is public. I know how to do this with non-WMA streams when using ServerInit method, but not when using EncodeSetNotify. You know how I mean, how.can I tell BassWma to refuse the connection...?

Ian @ un4seen

  • Administrator
  • Posts: 26028
Re: Refuse connections from Public IPs
« Reply #3 on: 17 May '24 - 11:40 »
Oops, I see what you mean now. Unfortunately, there is no way to refuse connections with a BASSWMA server. Perhaps you could use a BASSenc server and one of its supported formats instead, or does it need to be WMA?

kafffee

  • Posts: 262
Re: Refuse connections from Public IPs
« Reply #4 on: 17 May '24 - 11:56 »
Oh I already do. Just wanted to provide the option to also stream WMA.

The problem ist that you cant just stream random music to the public, at least here in Germany, you have to pay the so-called GEMA, which will go (in parts?) to each one of the artists.

What is it like where you come from? Is there anything similar?

I guess I'll just give the user a hint or completely disable the WMA option...