Back to index

courier  0.68.2
tcpremoteinfo.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2001 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include      "config.h"
00008 #endif
00009 #include      "tcpremoteinfo.h"
00010 #include      "soxwrap/sconnect.h"
00011 
00012 #if HAVE_UNISTD_H
00013 #include      <unistd.h>
00014 #endif
00015 #if HAVE_FCNTL_H
00016 #include      <fcntl.h>
00017 #endif
00018 #include      <stdio.h>
00019 #include      <errno.h>
00020 #include      <string.h>
00021 
00022 #include      "soxwrap/soxwrap.h"
00023 
00024 
00025 const char *tcpremoteinfo(const RFC1035_ADDR *laddr, int lport,
00026        const RFC1035_ADDR *raddr, int rport, const char **ostype)
00027 {
00028 int    fd;
00029 time_t current_time, max_time;
00030 fd_set fds;
00031 struct timeval       tv;
00032 static char buf[512];
00033 char   *bufptr;
00034 int    bufleft, n;
00035 char   *p;
00036 char   *q;
00037 RFC1035_NETADDR      sin;
00038 const struct sockaddr *addr;
00039 int    addrlen;
00040 
00041        fd=rfc1035_mksocket(SOCK_STREAM, 0, &n);
00042        if (fd < 0)   return (0);
00043 
00044        if (rfc1035_mkaddress(n, &sin, laddr, 0, &addr, &addrlen) < 0)
00045        {
00046               close(fd);
00047               return (0);
00048        }
00049 
00050        if (sox_bind(fd, addr, addrlen) < 0)
00051        {
00052               sox_close(fd);
00053               return (0);
00054        }
00055 
00056        time (&current_time);
00057        max_time=current_time+30;
00058 
00059        if (rfc1035_mkaddress(n, &sin, raddr, htons(113), &addr, &addrlen) < 0)
00060        {
00061               sox_close(fd);
00062               return (0);
00063        }
00064 
00065        if (s_connect(fd, addr, addrlen, max_time - current_time) < 0)
00066        {
00067               sox_close(fd);
00068               return (0);
00069        }
00070 
00071        sprintf(buf, "%d,%d\r\n", ntohs(rport), ntohs(lport));
00072        bufptr=buf;
00073        bufleft=strlen(buf);
00074        while (bufleft)
00075        {
00076               time(&current_time);
00077               if (current_time >= max_time)
00078               {
00079                      sox_close(fd);
00080                      return (0);
00081               }
00082 
00083               FD_ZERO(&fds);
00084               FD_SET(fd, &fds);
00085               tv.tv_sec=max_time-current_time;
00086               tv.tv_usec=0;
00087               if (sox_select(fd+1, 0, &fds, 0, &tv) != 1 ||
00088                      !FD_ISSET(fd, &fds))
00089               {
00090                      sox_close(fd);
00091                      return (0);
00092               }
00093               n=sox_write(fd, bufptr, bufleft);
00094               if (n <= 0)
00095               {
00096                      sox_close(fd);
00097                      return (0);
00098               }
00099               bufptr += n;
00100               bufleft -= n;
00101        }
00102 
00103        bufptr=buf;
00104        bufleft=sizeof(buf);
00105        do
00106        {
00107               if (bufleft == 0)
00108               {
00109                      sox_close(fd);
00110                      return (0);
00111               }
00112 
00113               time(&current_time);
00114               if (current_time >= max_time)
00115               {
00116                      sox_close(fd);
00117                      return (0);
00118               }
00119 
00120               FD_ZERO(&fds);
00121               FD_SET(fd, &fds);
00122               tv.tv_sec=max_time-current_time;
00123               tv.tv_usec=0;
00124               if (sox_select(fd+1, &fds, 0, 0, &tv) != 1 ||
00125                      !FD_ISSET(fd, &fds))
00126               {
00127                      sox_close(fd);
00128                      return (0);
00129               }
00130 
00131               n=sox_read(fd, bufptr, bufleft);
00132               if (n <= 0)
00133               {
00134                      sox_close(fd);
00135                      return (0);
00136               }
00137               bufptr += n;
00138               bufleft -= n;
00139        } while (bufptr[-1] != '\n');
00140        sox_close(fd);
00141        bufptr[-1]=0;
00142        --bufptr;
00143        if (bufptr > buf && bufptr[-1] == '\r')
00144               bufptr[-1]=0;
00145 
00146        if ((p=strchr(buf, ':')) == 0)
00147               return (0);
00148 
00149        q=++p;
00150        if ((p=strchr(p, ':')) == 0)
00151               return (0);
00152 
00153        *p++=0;
00154        q=strtok(q, " \t");
00155        if (!q || strcmp(q, "USERID"))     return (0);
00156        if (ostype)   *ostype=p;
00157        if ((p=strchr(p, ':')) == 0)
00158               return (0);
00159        *p++=0;
00160        while (*p && (*p == ' ' || *p == '\t'))   p++;
00161        return (p);
00162 }
00163