News for hackers. Stuff that matters.
Re:The trouble with struct sockaddr's fake flexible array by jandrese on Friday November 29, 2024 @10:41AM (#42274580)
It wouldn’t help the kernel case here, but I’ve long thought that the socket TCP and UDP APIs were bad in the general use case. Having to build and shuffle around the sockaddr structure is too low level for the majority use case. I think most programmers would prefer something like:

    int inet_connect(AF_TYPE, destination, port, options, &error);
Also:

    int inet_listen(AF_TYPE, port, options, &error);
So for example:

    int sock;
    inet_err err = NULL;
    sock = inet_connect(SOCK_STREAM, “google.com”, 443, NULL, &err);
    if ( sock < 0 )
    {
        printf(“Failed to connect: %s\n”, err->message);
        return -1;
    }
Basically one level more abstraction in the API. The old API could stick around for people who are doing something weird, but this would cover most use cases I think. Added bonus: programs using this API are trivial to add support for IPv6, it is just a recompile.
Re:The trouble with struct sockaddr's fake flexible array by asveikau on Friday November 29, 2024 @10:44AM (#42274604)
> sock = inet_connect(SOCK_STREAM, “google.com”, 443, NULL, &err);

You just added a DNS lookup to connect(2).

Also, you linked socket creation to all of this. This would make it hard to do an asynchronous version. Today you can create a socket and do a non-blocking connect(2) (after doing your asynchronous DNS lookup)

Lastly, I think if you tasked someone with creating that API, they would probably use the old APIs to build it ...

Re:The trouble with struct sockaddr's fake flexible array by ape4 on Friday November 29, 2024 @11:13AM (#42274814)
That second parameter could accept a text IP-address eg "192.168.100.100". Like the address bar of a browser.
Re:The trouble with struct sockaddr's fake flexible array by braiamp on Friday November 29, 2024 @11:52AM (#42275109)
So, the dns lookup is still there, it's just optional. You would have to pass a flag to tell it not to try to do the lookup or you would have to do something fancy trying to know if the passed parameter is an IP.
Re:The trouble with struct sockaddr's fake flexible array by asveikau on Friday November 29, 2024 @11:54AM (#42275126)
This is already how all the DNS lookup APIs work.
Re:The trouble with struct sockaddr's fake flexible array by tptacek on Friday November 29, 2024 @03:36PM (#42276891)
The problem is with asking the kernel to perform dns lookups.
Re:The trouble with struct sockaddr's fake flexible array by GauntletWizard on Friday November 29, 2024 @05:57PM (#42277871)
If SystemD had it's way, it would. The local system resolver cache is the default on most systems. DNS is well understood. Why not put it in the Kernel?

If you're doing that, though, you'd better dang well give me secure key storage in the Kernel, too, or full TLS capability. Allow my program to n load it's RSA/ECC key at startup, then drop privileges to access that file, and have a kernel module handle the cryptography.

Re:The trouble with struct sockaddr's fake flexible array by tptacek on Friday November 29, 2024 @07:02PM (#42278260)
It extremely doesn't make sense to have a DNS resolver resident in the kernel: DNS resolution is policy-intensive, involves various upgrades to cryptographic transports, and often requires debugging, all weak points of the kernel. Against all that, there's no performance advantage, and doing it admits a new networking API that almost no applications will use --- most new code being written in higher-level languages that already abstract over system calls.
Re:The trouble with struct sockaddr's fake flexible array by GauntletWizard on Friday November 29, 2024 @10:58PM (#42279277)
There's a huge performance advantage, if you ask SystemD - Multiple resolutions can be cached. This is incredible bullshit, of course, but that is one of many lies Pottering has gotten away with.
Re:The trouble with struct sockaddr's fake flexible array by toast0 on Saturday November 30, 2024 @07:22PM (#42285097)
Caching resolutions can be a nice thing, and having in one place can be useful so that the throwing away the cache can be done consistently for all apps when useful (you likely should clear the cache when switching networks, or when the user asks you to), but none of that means it should be a kernel service.

If there's enough volume of requests that a kernel level cache meaningfully improves real world scenarios, something is broken in those scenarios. :p (but I think you agree with me)

Re:The trouble with struct sockaddr's fake flexible array by jandrese on Sunday December 01, 2024 @08:54PM (#42292356)
How often do you not want to support host lookup when doing a TCP or UDP connection?

Certainly you could implement these functions using the existing BSD socket libraries, but that misses out on having them used everywhere so you get "free" IPv6 transitions and whatnot.

Re:The trouble with struct sockaddr's fake flexible array by Cloudef on Friday November 29, 2024 @10:50AM (#42274649)
I think plan9 API is the sanest one https://9fans.github.io/plan9port/man/man3/dial.html
Re:The trouble with struct sockaddr's fake flexible array by jeroenhd on Friday November 29, 2024 @03:12PM (#42276725)
The socket API was designed to be a low-level API. If you're writing applications, you probably want to use libcurl/WinInet or some other abstraction over all of the low-level stuff. Using raw connect() calls is like writing your own XML parser when myriad of existing solutions are free and ready to use.

Your example would fit perfectly in a libinet library, but I doubt the C standard library is going to include improvements upon this. If you want good internet support, pick a library or, better yet, another language because plain C is terrible for managing arbitrary network inputs anyway.

There is also some stuff your code doesn't handle (do you retry when a connection fails but an SRV record lists multiple options? who is supposed to free the inet_err struct, because your example leaks memory? how do you handle attempts to connect over IPv6 on hosts still stuck on IPv4 only?) that I would expect a low-level C interface to be more explicit about.

Re:The trouble with struct sockaddr's fake flexible array by viraptor on Friday November 29, 2024 @08:09PM (#42278595)
There's lots of higher level libraries that can do this nicely. But there has to exist the low level socket API which actually allows all the weird options to exist. Like protocols without ports.