Date: Sat, 10 Jul 1999 23:42:53 -0600 From: Wes Peters <wes@softweyr.com> To: Warner Losh <imp@village.org> Cc: Sheldon Hearn <sheldonh@uunet.co.za>, "Brian F. Feldman" <green@FreeBSD.ORG>, hackers@FreeBSD.ORG Subject: Re: a BSD identd Message-ID: <37882EDD.1003BD9E@softweyr.com> References: <56059.931624857@axl.noc.iafrica.com> <199907102148.PAA33143@harmony.village.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?37882EDD.1003BD9E>