Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Brl.Socket] AF_UNSPEC_ usage in docs is wrong #276

Open
GWRon opened this issue May 20, 2023 · 0 comments
Open

[Brl.Socket] AF_UNSPEC_ usage in docs is wrong #276

GWRon opened this issue May 20, 2023 · 0 comments

Comments

@GWRon
Copy link
Contributor

GWRon commented May 20, 2023

William on the discord server has issues with creating sockets following the documentation:

Before we create a socket, we define a TAddrInfo object to provide hints for the kind of socket we want to create. This is the modern way to handle sockets, and it simplifies a lot of the coding we'll need to do when dealing with them.

Local hints:TAddrInfo = New TAddrInfo(AF_INET_, SOCK_STREAM_)

The first argument AF_INET_ requests that the socket use the IPv4 protocol. For IPv6 you can use AF_INET6_, and if you don't care which protocol to use, you can use AF_UNSPEC_, which will allow you to use either.

Source: https://blitzmax.org/docs/en/tutorials/network_programming/#creating-a-socket

His testcode is this one:

Local hints:TAddrInfo = New TAddrInfo(AF_UNSPEC_, SOCK_STREAM_) ' socket object
Local socket:TSocket = TSocket.Create(hints) ' actual socket
If Not socket Then
    Print "Unable to create socket."
    End
End If

I extended it to get the params in use - and then pass these values directly into socket_().

SuperStrict
Framework Brl.Socket
Import Brl.StandardIO


'Local hints:TAddrInfo = New TAddrInfo(AF_INET6_, SOCK_STREAM_)
Local hints:TAddrInfo = New TAddrInfo(AF_INET_, SOCK_STREAM_)
'Local hints:TAddrInfo = New TAddrInfo(AF_UNSPEC_, SOCK_STREAM_)
print "hints: family = " + hints.family()
print "hints: sockType = " + hints.sockType()
print "hints: canonName = " + hints.canonName()
print "hints: HostName = " + hints.HostName()
print "hints: HostIp = " + hints.HostIp()
print "hints: Protocol = " + hints.Protocol()
print "-----"
Local socket:TSocket = TSocket.Create(hints)

'direct call: family=0 (unspec), family=2 (ipv4) family=10 (ipv6)
'             socket_(family, sockType, protocol)
Local socketID:Int = socket_( 0,1,0)
print socketID


If Not socket Then
    Print "Unable to create socket."
    End
End If

Print "Socket Created"

When passing 0 for the family-param, the socketID is -1.

Now I checked what the socket() docs say:
Linux: https://man7.org/linux/man-pages/man2/socket.2.html
There is no mention of some "use whatever is possible". (sys/socket.h defines it but yeah, no "autonegotiation" or so)

On Windows they strongly disencourage the usage of unspec:
https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket#remarks
They also tell to use the IPv6 one:

Applications are encouraged to use AF_INET6 for the af parameter and create a dual-mode socket that can be used with both IPv4 and IPv6.

on Solaris they say the IPv6 one would use IPv4 or IPv6 (so also not the unspec one):
https://docs.oracle.com/cd/E19120-01/open.solaris/817-4415/6mjum5som/index.html

Tried to print the "errno"-value but pub.stdc defines a errno_() which seems to be incorrect - raising an extra issue for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant