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

✨ std: System.hostname #2649

Conversation

miketheman
Copy link
Contributor

Addition of a method to retrieve the current hostname.

Hat tip to @jhass for showing the example on the mailing list.

I was unable to figure out a method for testing a hostname longer than 255 chars without creating the sethostname method, and didn't want to implement two things at once before I know if this is the correct approach.

The notation in src/lib_c/<platform>/c/sys/socket.cr is a little inconsistent across platforms - some use xN, others use fd - so I followed my gut and use descriptive notation for all platforms.

@miketheman
Copy link
Contributor Author

Reading the specs, I also think at some point soon it may make sense to split the specs up into subdirs so as to keep them focused on segments of the Socket namespace, but that's a future decision.

# Maximum of 253 characters are allowed, with 2 bytes reserved for storage.
# In practice, many platforms will disallow anything longer than 63 characters.
# ```
# Socket.gethostname # => my.machine.name.local
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Socket.gethostname # => "host.example.org"

It returns a string and using an mDNS name for your stuff will probably break things, so not a good example IMO ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, fixing example.

@miketheman miketheman force-pushed the miketheman/socket_gethostname branch from f690534 to e2c2b57 Compare May 25, 2016 21:32
@lbguilherme
Copy link
Contributor

I believe it would be better for the name to be Socket.hostname, following naming conventions of not using "get" for getters.

@miketheman
Copy link
Contributor Author

@lbguilherme That might make sense, how would that relate to a scenario in which someone is setting a hostname?
Socket.sethostname is very clear as to what to expect the outcome should be.

Unless we think that Socket.hostname returns current hostname and Socket.hostname("foobar.com") is an acceptable approach?

The latter seems more confusing to me.

@jhass
Copy link
Member

jhass commented May 26, 2016

Perhaps Socket is the wrong home for it after all. Shall we introduce a System module or so for this kind of stuff? /cc @crystal-lang/crystallers Anybody wants to see what existent stuff could be moved there? I can imagine there's some on Process already.

Another name I can think of is Socket.current_hostname. sethostname(2) would be mapped to Socket.current_hostname=(new_name), if we ever decide to do it.

@asterite
Copy link
Member

In any case, this functionality is blocking and maybe it should be non-blocking (use libevent, or some other mechanism), so this will have to wait untli @waj can approve this (he's on vacations for a few weeks now)

@jhass
Copy link
Member

jhass commented May 26, 2016

@asterite It is a blocking system call, but it is not network dependent. It simply reads system configuration. It should usually be the same as the nodename property of the uname(2) call from my understanding.

@asterite
Copy link
Member

@jhass Oh, my mistake, I confused this with something like getaddrinfo.

I agree that Socket.hostname is probably not the best place to put this. I always check how other languages provide this. Go has it in the os package. C# with Dns.GetHostName. In Java it's java.net.InetAddress.getLocalHost().getHostName() (of course).

Go also has Getpid, Getuid and others in os package, and we (and Ruby) has those in Process.

So maybe Process.hostname and Process.hostname=? That was actually my initial though when I read the man page for gethostname:

The gethostname() function returns the standard host name for the current processor

Well, Process is certainly different than Processor, and hostname doesn't change between processes, but since it's a class method I think it's not that confusing. We could add a System or OS class/module, but Process is already there. Thoughts?

@jhass
Copy link
Member

jhass commented May 26, 2016

POSIX words it as

The gethostname() function shall return the standard host name for the current machine

I don't like it on Process, it should keep at stuff specific to a process, not to a machine. I would prefer Machine or System, os always felt like an odd hodgepodge in Python, so it might attract too different stuff.

@asterite
Copy link
Member

asterite commented May 26, 2016

System sounds good, but I'd like to find more things to put in it. If we'll end up with just System.hostname then I still prefer to use Process or even Socket for this (python too uses socket). Or maybe we can just use System and later use it if we ever need more system-related functionality.

@miketheman
Copy link
Contributor Author

I see this as my first foray into LibC here, so wanted to get the
discussion going.
I foresee a large variety of system-related functions that we should be
able to call, irrespective of their underlying POSIX namespaces.
I'm in favor of a System. class, and can port it over there.
I originally chose Socket to be similar to Ruby.

On Thu, May 26, 2016, 09:54 Ary Borenszweig notifications@github.com
wrote:

System sounds good, but I'd like to find more things to put in it. If
we'll end up with just System.hostname then I still prefer to use Process
or even Socket for this (python too uses socket
https://docs.python.org/3/library/socket.html#socket.gethostname). Or
maybe we can just use System and later use it if we even need more
system-related functionality.


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#2649 (comment)

@asterite
Copy link
Member

OK, let's use System 😸

I was planning on releasing 0.17.4 today, which contains a few goodies and many fixes. I'd like to include this too because it's backwards compatible and generally useful.

We have to create a system.cr file and require it in prelude.cr. system.cr must require "c/sys/socket".

We have these options:

  1. You update the code and make a PR targetting the release/0.17 branch (though I guess it can target master and then I can merge it in release/0.17, @jhass ?)
  2. I merge this PR into release/0.17 and then move the method to the System module
  3. I commit directly into release/0.17, thanking you (I'll of course mention you in the Changelog, as usual)

@miketheman miketheman force-pushed the miketheman/socket_gethostname branch from e2c2b57 to 5c6800c Compare May 26, 2016 14:20
@@ -0,0 +1,20 @@
require "c/sys/socket"

class System
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module ;)

New class for System-related functions.

First: method to retrieve the current hostname.
@miketheman miketheman force-pushed the miketheman/socket_gethostname branch from 5c6800c to aee43c2 Compare May 26, 2016 14:23
@miketheman
Copy link
Contributor Author

@asterite I've updated the PR, let's hope CI doesn't bite me. :)

  • I wasn't certain if there was an "order" to prelude.cr - so I appended to the end. Does order matter in this file? If so, we should document it inline. Otherwise, alpha-sort makes me happiest.
  • The specs passed without the require "c/sys/socket" - os that getting loaded in some other manner?

I'm happy to send a PR to any branch you like.

@asterite
Copy link
Member

@miketheman Thanks!

Order used to matter, but now it matters less and less. It only matters if macros are used (macros need to be defined before they are used), but a first compiler pass gathers all classes and methods, so in this case it's OK to put it in any order.

@asterite
Copy link
Member

It seems libevent requires c/netdb, which in turns requires c/sys/socket, so that's why it works (libevent is always required). In any case, it's better to have a specific require in case we remove libevent or we change its dependencies.

@miketheman
Copy link
Contributor Author

@asterite awesome - I think I'll add a note to prelude.cr in another PR about ordering.

For this one, all tests pass, the world didn't explode, is there any further steps?

@jhass jhass closed this in 29a0ac1 May 26, 2016
@jhass
Copy link
Member

jhass commented May 26, 2016

No, thank you!

@miketheman
Copy link
Contributor Author

Woohoo, thanks!

@miketheman miketheman deleted the miketheman/socket_gethostname branch May 26, 2016 15:11
@miketheman miketheman changed the title ✨ std: Socket.gethostname ✨ std: System.hostname May 26, 2016
@miketheman miketheman mentioned this pull request May 26, 2016
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

Successfully merging this pull request may close these issues.

None yet

5 participants