home *** CD-ROM | disk | FTP | other *** search
- RCS_ID_C= "$Id: fingerd.c,v 1.4 1993/10/18 15:45:30 ppessi Exp $";
- /*
- * fingerd.c --- an example of TCP daemon for AmiTCP/IP
- *
- * This command sends either a AmiTCP/IP banner or the specified file
- * into the TCP socket server_socket
- *
- * Author: ppessi <Pekka.Pessi@hut.fi>
- *
- * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
- * Helsinki University of Technology, Finland.
- *
- * Created : Mon May 24 23:44:43 1993 ppessi
- * Last modified: Mon Oct 18 17:34:41 1993 ppessi
- *
- * $Log: fingerd.c,v $
- * Revision 1.4 1993/10/18 15:45:30 ppessi
- * Added real version tags.
- *
- * Revision 1.3 1993/10/15 01:24:00 ppessi
- * A new version supporting real fingering. Uses Apipe:.
- *
- * Revision 1.2 1993/08/10 20:46:23 jraja
- * Added version string.
- *
- * Revision 1.1 1993/06/04 11:49:53 jraja
- * Initial revision
- *
- */
-
- #include "fingerd_rev.h"
- const char version[] = VERSTAG;
-
- char copyright[] =
- "Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
- "Helsinki University of Technology, Finland.\n";
-
- #ifdef AMIGA
- #if __SASC
- #include <proto/socket.h>
- #include <proto/dos.h>
- #include <clib/exec_protos.h>
- #include <pragmas/exec_sysbase_pragmas.h>
- #elif __GNUC__
- #include <inline/socket.h>
- #include <inline/exec.h>
- #else
- #include <clib/socket_protos.h>
- #endif
- #endif /* AMIGA */
-
- #include <errno.h>
- #include <netdb.h>
-
- #include <sys/param.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <netinet/in.h>
-
- #include <signal.h>
-
- #include <dos/dos.h>
- #include <exec/execbase.h>
- #include <dos/var.h>
-
- #include <stdlib.h>
- #include <inetdlib.h>
-
- #include "pathnames.h"
-
- #define isspace(x) (x == ' ')
-
- extern struct ExecBase *SysBase;
-
- BPTR Stdin = NULL;
- BPTR Stdout = NULL;
- BPTR Stderr = NULL;
-
- void
- _STIdosStdio(void)
- {
- struct Process *p = (struct Process *)SysBase->ThisTask;
-
- Stdin = p->pr_CIS;
- Stdout = p->pr_COS;
- Stderr = p->pr_CES ? p->pr_CES : Stdout;
- }
-
- #define BANNER \
- "\r\n AmiTCP/IP 2.1 Release System.\r\n\r\n"
-
- int
- main(int argc, char **argv)
- {
- long len;
- int s = server_socket;
- BPTR pfh;
-
- # define CMDHEAD "apipe:" _PATH_FINGER ""
- # define CMDLEN (sizeof(CMDHEAD)-1)
- # define LINELEN (1024)
- # define CMDLINELEN (1024+CMDLEN)
-
- char line[LINELEN], cmdline[CMDLINELEN] = CMDHEAD;
- char *fname = cmdline;
-
- #ifdef LOGGING
- #include <netinet/in.h>
- struct sockaddr_in sin;
- int sval;
- #endif
-
- if (s == -1) {
- #ifdef STANDALONE
- struct sockaddr_in sin;
-
- s = serveraccept("finger", &sin);
- if (s != -1) {
- FPrintf(Stderr, "Accepted a connection from %s, port %ld\n",
- inet_ntoa(sin.sin_addr), sin.sin_port);
- } else
- #endif
- return 1;
- }
-
- #ifdef LOGGING /* unused for now */
- sval = sizeof(sin);
- if (getpeername(0, &sin, &sval) < 0)
- fatal("getpeername");
- #endif
-
- {
- enum { WSPACE, TOKEN, QUOTED } state = WSPACE;
- char c;
- int received, peek, len = 0;
-
- do {
- received = recv(s, line + len, 1, 0);
- if (received < 0) {
- PrintNetFault(Errno(), "recv");
- return 1;
- }
- } while (received && line[len] != '\n' && len++ < LINELEN - 2);
-
- if (len == 1 && argc > 1)
- fname = argv[1];
-
- if (line[len - 1] == '\r')
- len--;
- line[len] = '\0';
-
- /*
- * Parse command line:
- * Quote arguments,
- * convert /w to -l
- */
- peek = 0; received = len = CMDLEN;
- while ((c = line[peek++]) && len < CMDLINELEN - 4) {
- switch(state) {
- case WSPACE:
- if (c == '/'
- && (line[peek] == 'W' || line[peek] == 'w')
- && (line[peek + 1] == '\0' || isspace(line[peek + 1]))) {
- cmdline[len++] = ' ';
- cmdline[len++] = '-';
- cmdline[len++] = 'l';
- peek += 1;
- received = len;
- } else if (c == '"') {
- cmdline[len++] = '"';
- state = QUOTED;
- } else if (!isspace(c)) {
- cmdline[len++] = ' ';
- cmdline[len++] = '"';
- cmdline[len++] = c;
- state = TOKEN;
- }
- break;
- case TOKEN:
- if (c == '"') {
- state = QUOTED;
- } else if (isspace(c)) {
- cmdline[len++] = '"';
- state = WSPACE;
- received = len;
- } else {
- cmdline[len++] = c;
- }
- break;
- case QUOTED:
- if (c == '"') {
- state = TOKEN;
- } else {
- cmdline[len++] = c;
- }
- break;
- }
- }
- if (c || state == QUOTED) {
- /* remove last argument */
- len = received;
- } else if (state == TOKEN) {
- cmdline[len++] = '"';
- }
- cmdline[len] = '\0'; /* put eos */
- }
-
- pfh = Open(fname, MODE_OLDFILE);
-
- /* Try to open banner if there is no APipe or Finger */
- if (pfh ||
- argc >= 2 && (pfh = Open(argv[1], MODE_OLDFILE))) {
- while (FGets(pfh, line, sizeof(line))) {
- len = strlen(line);
- /* Change eol's to the network standard */
- if (line[len - 1] == '\n') {
- line[len - 1] = '\r';
- line[len++] = '\n';
- }
- if (send(s, line, len, 0) < 0) break;
- }
- Close(pfh);
- } else {
- if (send(s, BANNER, strlen(BANNER), 0) < 0) {
- PrintNetFault(Errno(), "send");
- return 1;
- }
- }
-
- return 0;
- }
-