From owner-freebsd-hackers Sat Jul 10 22:43:28 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from obie.softweyr.com (unknown [204.68.178.33]) by hub.freebsd.org (Postfix) with ESMTP id 7187114CC1; Sat, 10 Jul 1999 22:43:20 -0700 (PDT) (envelope-from wes@softweyr.com) Received: from softweyr.com (homer.softweyr.com [204.68.178.39]) by obie.softweyr.com (8.8.8/8.8.8) with ESMTP id XAA29700; Sat, 10 Jul 1999 23:42:55 -0600 (MDT) (envelope-from wes@softweyr.com) Message-ID: <37882EDD.1003BD9E@softweyr.com> Date: Sat, 10 Jul 1999 23:42:53 -0600 From: Wes Peters Organization: Softweyr LLC X-Mailer: Mozilla 4.5 [en] (X11; U; FreeBSD 3.1-RELEASE i386) X-Accept-Language: en MIME-Version: 1.0 To: Warner Losh Cc: Sheldon Hearn , "Brian F. Feldman" , hackers@FreeBSD.ORG Subject: Re: a BSD identd References: <56059.931624857@axl.noc.iafrica.com> <199907102148.PAA33143@harmony.village.org> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Warner Losh wrote: > > Yes. I'd love to replace my perl script: > > #!/usr/local/bin/perl > ($a, $b) = split(/[,\n\r ]+/,<>); > print "$a , $b : USERID : UNIX : Warm-Fuzzy\r\n"; > > with something a little less heavy-weight. OK, here's roughly the same thing as a stand-alone server in C (for the absolute fastest lies in the west. ;^) It's been a while since I've done any programming above the guts of the IP level, so feel free to critique the bejabbers out of this. I don't think I'm leaking any memory or file descriptors, or otherwise doing something stupid, but I spent about 10 minutes on this code. Purists will note that the parsing of the client request is REALLY SIMPLE. ;^) /* * Copyright (c) 1999 Softweyr LLC. All rights reserved. * * identd: a simple server for RFC1413 Identification Service that refuses * to give away critical system information. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notices, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above * copyright notices, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. * * $Id$ */ #include #include #include #include #include #include #define IDENT_PORT 113 char *pname; const int STARS_SHINE = 1; int main(int argc, char *argv[]) { int serv_sock, client_sock, cli_len; size_t iolen; struct sockaddr_in cli_addr, serv_addr; char iobuf[BUFSIZ], *ioptr; pname = argv[0]; /* * Create a TCP socket and bind it to the ident port. */ serv_sock = socket(AF_INET, SOCK_STREAM, 0); if (serv_sock < 0) { perror("opening server socket"); return -1; } bzero((char *) &serv_addr, sizeof (serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(IDENT_PORT); if (bind(serv_sock, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { perror("binding server socket to ident port"); return -1; } listen(serv_sock, 5); while (STARS_SHINE) { /* * Gather host connections and answer them immediately. */ cli_len = sizeof (cli_addr); bzero((char *) &cli_addr, sizeof (cli_addr)); client_sock = accept(serv_sock, (struct sockaddr *) &cli_addr, &cli_len); if (client_sock < 0) { perror("accepting connection on server socket"); break; } if ((iolen = read(client_sock, iobuf, BUFSIZ)) < 0) { perror("reading from client"); close(client_sock); break; } iobuf[iolen] = '\0'; ioptr = strchr(iobuf, '\r'); if (ioptr == NULL) { perror("invalid client request"); close(client_sock); break; } iolen = ioptr - iobuf; strncpy(ioptr, " : USERID : UNIX : Warm-Fuzzy\r\n", BUFSIZ - iolen); if (write(client_sock, iobuf, strlen(iobuf)) < strlen(iobuf)) { perror("writing response to client"); } close(client_sock); } /* * Not normally reached, but those stars may go out some day... */ close(serv_sock); return 0; } -- "Where am I, and what am I doing in this handbasket?" Wes Peters Softweyr LLC http://softweyr.com/ wes@softweyr.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message