9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
Subject: [9fans] dial M for Minimalism
Date: Thu, 10 Dec 1998 10:19:48 +0000	[thread overview]
Message-ID: <19981210101948.LuiGpp7CHDnzJZYCa2mIf_LQUxyYR1UsSrFUkB9FhoM@z> (raw)



forsyth@caldo.demon.co.uk wrote:

> as a doctor, i am often asked ``why doesn't Plan 9 (or Inferno) use
> the `standard' socket calls?''
>
> the source of tcpblast.c was recently posted to this list.
> it makes a tcp/ip call to a given machine.
> the bits that do so are shown below (i have completed
> the code that looks up the service name):
>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <netdb.h>
>
>         ...
>
> struct sockaddr_in sock_in;
> struct servent *sp;
> struct hostent *host;
>
>         ...
>  memset(&sock_in, 0, sizeof (sock_in));
>  sock_in.sin_family = AF_INET;
>  f = socket(AF_INET, SOCK_STREAM, 0);
>  if (f < 0) {
>   perror("tcpblast: socket");
>   exit(3);
>  }
>  if (bind(f, (struct sockaddr*) &sock_in, sizeof (sock_in)) < 0) {
>   perror("tcpblast: bind");
>   exit(1);
>  }
>
>  host = gethostbyname(argv[1]);
>  if (host) {
>   sock_in.sin_family = host->h_addrtype;
>   memmove(&sock_in.sin_addr, host->h_addr, host->h_length);
>  } else {
>   sock_in.sin_family = AF_INET;
>   sock_in.sin_addr.s_addr = inet_addr(argv[1]);
>   if (sock_in.sin_addr.s_addr == -1) {
>    fprintf(stderr, "tcpblast: %s unknown host\n", argv[1]);
>    exit(1);
>   }
>  }
>  sp = getservbyname("discard", "tcp");
>  if (sp)
>    sock_in.sin_port = sp->s_port;
>  else
>    sock_in.sin_port = htons(9);
>
>  if (connect(f, (struct sockaddr*) &sock_in, sizeof(sock_in)) <0)
>  {
>   perror("tcpblast connect:");
>   exit(1);
>  }
>
> by contrast, in my variant for Plan 9, i wrote:
>
> #include <u.h>
> #include <libc.h>
>
>         ...
>
>         fd = dial(netmkaddr(argv[0], "tcp", "discard"), nil, nil, nil);
>         if(fd < 0){
>                 fprint(2, "tcpbuzz: can't dial %s: %r\n", argv[0]);
>                 exits("dial");
>         }
>
> the nil, nil, nil looks and sounds like a Eurovision Song Contest
> score, which is perhaps a blemish.  (each nil represents a default
> that more specialised applications can set to access such things as
> port number assignment and the connection's control and status files.
> Inferno's dial interface is simpler still, partly because of the use
> of Limbo tuples.)  apart from that, the incantation is straightforward
> and easy to write.  because the interface and underlying
> infrastructure provides a good degree of abstraction, it also works
> without change for all suitable network types and protocols; it isn't
> limited to TCP/IP or IP networks.  the connection service and name
> space together sort out the details; that the network files live in
> the per-process name space also enables the same code to dial through
> an imported gateway.





             reply	other threads:[~1998-12-10 10:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-10 10:19  [this message]
  -- strict thread matches above, loose matches on Subject: below --
1998-11-26 12:30 Nigel
1998-11-26 11:40 forsyth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19981210101948.LuiGpp7CHDnzJZYCa2mIf_LQUxyYR1UsSrFUkB9FhoM@z \
    --to=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).