Annotation of 42BSD/usr.lib/sendmail/aux/mconnect.c, revision 1.1.1.1

1.1       root        1: # include <stdio.h>
                      2: # include <signal.h>
                      3: # include <ctype.h>
                      4: # include <sgtty.h>
                      5: # include <sys/types.h>
                      6: # include <sys/socket.h>
                      7: # include <netinet/in.h>
                      8: # include <netdb.h>
                      9: 
                     10: static char    SccsId[] =      "@(#)mconnect.c 4.1             7/25/83";
                     11: 
                     12: struct sockaddr_in     SendmailAddress;
                     13: struct sgttyb          TtyBuf;
                     14: 
                     15: main(argc, argv)
                     16:        int argc;
                     17:        char **argv;
                     18: {
                     19:        register int s;
                     20:        char *host;
                     21:        register int i;
                     22:        int pid;
                     23:        struct servent *sp;
                     24:        int raw = 0;
                     25:        char buf[1000];
                     26:        register char *p;
                     27:        extern char *index();
                     28:        register FILE *f;
                     29:        extern u_long inet_addr();
                     30:        extern finis();
                     31: 
                     32:        (void) gtty(0, &TtyBuf);
                     33:        (void) signal(SIGINT, finis);
                     34:        s = socket(AF_INET, SOCK_STREAM, 0, 0);
                     35:        if (s < 0)
                     36:        {
                     37:                perror("socket");
                     38:                exit(-1);
                     39:        }
                     40: 
                     41:        sp = getservbyname("smtp", "tcp");
                     42:        if (sp != NULL)
                     43:                SendmailAddress.sin_port = sp->s_port;
                     44: 
                     45:        while (--argc > 0)
                     46:        {
                     47:                register char *p = *++argv;
                     48: 
                     49:                if (*p == '-')
                     50:                {
                     51:                        switch (*++p)
                     52:                        {
                     53:                          case 'h':             /* host */
                     54:                                break;
                     55: 
                     56:                          case 'p':             /* port */
                     57:                                SendmailAddress.sin_port = htons(atoi(*++argv));
                     58:                                argc--;
                     59:                                break;
                     60: 
                     61:                          case 'r':             /* raw connection */
                     62:                                raw = 1;
                     63:                                TtyBuf.sg_flags &= ~CRMOD;
                     64:                                stty(0, &TtyBuf);
                     65:                                TtyBuf.sg_flags |= CRMOD;
                     66:                                break;
                     67:                        }
                     68:                }
                     69:                else if (host == NULL)
                     70:                        host = p;
                     71:        }
                     72:        if (host == NULL)
                     73:                host = "localhost";
                     74: 
                     75:        if (isdigit(*host))
                     76:                SendmailAddress.sin_addr.s_addr = inet_addr(host);
                     77:        else
                     78:        {
                     79:                register struct hostent *hp = gethostbyname(host);
                     80: 
                     81:                if (hp == NULL)
                     82:                {
                     83:                        fprintf(stderr, "mconnect: unknown host %s\r\n", host);
                     84:                        finis();
                     85:                }
                     86:                bcopy(hp->h_addr, &SendmailAddress.sin_addr, hp->h_length);
                     87:        }
                     88:        SendmailAddress.sin_family = AF_INET;
                     89:        printf("connecting to host %s (0x%x), port 0x%x\r\n", host,
                     90:               SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
                     91:        if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
                     92:        {
                     93:                perror("connect");
                     94:                exit(-1);
                     95:        }
                     96: 
                     97:        /* good connection, fork both sides */
                     98:        printf("connection open\n");
                     99:        pid = fork();
                    100:        if (pid < 0)
                    101:        {
                    102:                perror("fork");
                    103:                exit(-1);
                    104:        }
                    105:        if (pid == 0)
                    106:        {
                    107:                /* child -- standard input to sendmail */
                    108:                int c;
                    109: 
                    110:                f = fdopen(s, "w");
                    111:                while ((c = fgetc(stdin)) >= 0)
                    112:                {
                    113:                        if (!raw && c == '\n')
                    114:                                fputc('\r', f);
                    115:                        fputc(c, f);
                    116:                        if (c == '\n')
                    117:                                fflush(f);
                    118:                }
                    119:        }
                    120:        else
                    121:        {
                    122:                /* parent -- sendmail to standard output */
                    123:                f = fdopen(s, "r");
                    124:                while (fgets(buf, sizeof buf, f) != NULL)
                    125:                {
                    126:                        fputs(buf, stdout);
                    127:                        fflush(stdout);
                    128:                }
                    129:        }
                    130:        finis();
                    131: }
                    132: 
                    133: finis()
                    134: {
                    135:        stty(0, &TtyBuf);
                    136:        exit(0);
                    137: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.