[BACK]Return to sio.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Diff for /OpenXM_contrib2/asir2000/io/sio.c between version 1.9 and 1.9.2.1

version 1.9, 2000/11/07 06:35:39 version 1.9.2.1, 2000/11/08 08:18:14
Line 44 
Line 44 
  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY   * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,   * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.   * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
  * $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.8 2000/10/06 06:05:23 noro Exp $   * $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.9 2000/11/07 06:35:39 noro Exp $
 */  */
 #if INET  #if INET
 #include "ca.h"  #include "ca.h"
Line 86  void getremotename(s,name)
Line 86  void getremotename(s,name)
 int s;  int s;
 char *name;  char *name;
 {  {
         struct sockaddr_in peer;          union {
         struct hostent *hp;                  struct sockaddr sa;
         int peerlen;                  char data[SOCK_MAXADDRLEN];
           } dummy;
           struct sockaddr *sa;
           socklen_t len;
           char host[NI_MAXHOST];
           int rs;
   
         peerlen = sizeof(peer);          rs = getremotesocket(s);
         getpeername(getremotesocket(s),(struct sockaddr *)&peer,&peerlen);          len = SOCK_MAXADDRLEN;
         hp = gethostbyaddr((char *)&peer.sin_addr,sizeof(struct in_addr),AF_INET);          getpeername(rs, (struct sockaddr *)dummy.data, &len);
         if ( hp )          sa = &(dummy.sa);
                 strcpy(name,hp->h_name);          getnameinfo(sa, sa->sa_len, host, sizeof(host), NULL, 0, 0);
         else          strcpy(name, host);
                 strcpy(name,(char *)inet_ntoa(peer.sin_addr));  
 }  }
   
 int generate_port(use_unix,port_str)  int generate_port(use_unix,port_str)
Line 126  int try_bind_listen(use_unix,port_str)
Line 130  int try_bind_listen(use_unix,port_str)
 int use_unix;  int use_unix;
 char *port_str;  char *port_str;
 {  {
         struct sockaddr_in sin;          struct addrinfo hints, *res, *ai;
         struct sockaddr *saddr;          int s, error;
         int len;          char *errstr;
         int service;  
 #if !defined(VISUAL)  
         struct sockaddr_un s_un;  
   
         if ( use_unix ) {          memset(&hints, 0, sizeof(hints));
                 service = socket(AF_UNIX, SOCK_STREAM, 0);  #if defined(VISUAL)
                 if (service < 0) {          hints.ai_family = PF_UNSPEC;
                         perror("in socket");  
                         return -1;  
                 }  
                 s_un.sun_family = AF_UNIX;  
                 strcpy(s_un.sun_path,port_str);  
 #if defined(__FreeBSD__)  
                 len = SUN_LEN(&s_un);  
                 s_un.sun_len = len+1; /* XXX */  
 #else  #else
                 len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);          if (use_unix)
 #endif                  hints.ai_family = PF_UNIX;
                 saddr = (struct sockaddr *)&s_un;          else
         } else                  hints.ai_family = PF_UNSPEC;
 #endif  #endif /* VISUAL */
         {          hints.ai_socktype = SOCK_STREAM;
                 service = socket(AF_INET, SOCK_STREAM, 0);  
                 if ( service < 0 ) {          error = getaddrinfo(NULL, port_str, &hints, &res);
                         perror("in socket");          if (error) {
                         return -1;                  warnx("try_bind_listen(): %s", gai_strerror(error));
                   return (-1);
           }
   
           for (ai = res ; ai != NULL ; ai = ai->ai_next) {
                   if ((s = socket(ai->ai_family, ai->ai_socktype,
                                   ai->ai_protocol)) < 0 ) {
                           errstr = "in socket";
                           continue;
                 }                  }
                 sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;  
                 sin.sin_port = htons(atoi(port_str));                  if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                 len = sizeof(sin);                          errstr = "in bind";
                 saddr = (struct sockaddr *)&sin;                          close(s);
                           s = -1;
                           continue;
                   }
   
                   if (listen(s, SOCKQUEUELENGTH) < 0) {
                           errstr = "in listen";
                           close(s);
                           s = -1;
                           continue;
                   }
   
                   /* established connection */
                   break;
         }          }
         if (bind(service, saddr, len) < 0) {          freeaddrinfo(res);
                 perror("in bind");  
                 close(service);          if (s < 0)
                 return -1;                  perror(errstr);
         }          return (s);
         if (getsockname(service,saddr, &len) < 0) {  
             perror("in getsockname");  
             close(service);  
             return -1;  
         }  
         if (listen(service, SOCKQUEUELENGTH) < 0) {  
                 perror("in listen");  
                 close(service);  
                 return -1;  
         }  
         return service;  
 }  }
   
 /*  /*
Line 196  char *port_str;
Line 199  char *port_str;
 int try_accept(af_unix,s)  int try_accept(af_unix,s)
 int af_unix,s;  int af_unix,s;
 {  {
         int len,c,i;          union {
         struct sockaddr_in sin;                  struct sockaddr sa;
                   char data[SOCK_MAXADDRLEN];
           } dummy;
           socklen_t len;
           int c, i;
   
 #if !defined(VISUAL)          len = SOCK_MAXADDRLEN;
         struct sockaddr_un s_un;          if (getsockname(s, (struct sockaddr *)dummy.data, &len) < 0) {
         if ( af_unix ) {                  close(s);
                 len = sizeof(s_un);                  return (-1)
                 for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )          }
                         c = accept(s, (struct sockaddr *) &s_un, &len);  
         } else  
 #endif  
         {  
   
                 len = sizeof(sin);          for (i = 0 ; i < 10 ; i++) {
                 for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )                  c = accept(s, &(dummy.sa), &len);
                         c = accept(s, (struct sockaddr *) &sin, &len);                  if (c >= 0) {
                           close(s);
                           return (c);
                   }
         }          }
         if ( i == 10 )  
                 c = -1;  
         close(s);          close(s);
         return c;          return (-1);
 }  }
   
 int try_connect(use_unix,host,port_str)  int try_connect(use_unix,host,port_str)
 int use_unix;  int use_unix;
 char *host,*port_str;  char *host,*port_str;
 {  {
         struct sockaddr_in sin;          struct addrinfo hints, *res, *ai;
         struct sockaddr *saddr;          int s, error, i;
         struct hostent *hp;          char *errstr;
         int len,s,i;  
 #if !defined(VISUAL)  
         struct sockaddr_un s_un;  
 #endif  
   
         for ( i = 0; i < 10; i++ ) {          memset(&hints, 0, sizeof(hints));
 #if !defined(VISUAL)  #if defined(VISUAL)
                 if ( use_unix ) {          hints.ai_family = PF_UNSPEC;
                         if ( (s = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) {  
                                 perror("socket");  
                                 return -1;  
                         }  
                         bzero(&s_un,sizeof(s_un));  
                         s_un.sun_family = AF_UNIX;  
                         strcpy(s_un.sun_path,port_str);  
 #if defined(__FreeBSD__)  
                         len = SUN_LEN(&s_un);  
                         s_un.sun_len = len+1; /* XXX */  
 #else  #else
                         len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);          if (use_unix)
 #endif                  hints.ai_family = PF_UNIX;
                         saddr = (struct sockaddr *)&s_un;          else
                 } else                  hints.ai_family = PF_UNSPEC;
 #endif /* VISUAL */  #endif /* VISUAL */
                 {          hints.ai_socktype = SOCK_STREAM;
                         if ( (s = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {  
                                 perror("socket");          error = getaddrinfo(host, port_str, &hints, &res);
                                 return -1;          if (error) {
                   warnx("try_connect: %s", gai_strerror(error));
                   return (-1);
           }
           for (i = 0 ; i < 10 ; i++) {
                   for (ai = res ; ai != NULL ; ai = ai->ai_next) {
                           if ((s = socket(ai->ai_family, ai->ai_socktype,
                                           ai->ai_protocol)) < 0 ) {
                                   errstr = "socket";
                                   continue;
                         }                          }
                         bzero(&sin,sizeof(sin));                          if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                         sin.sin_port = htons(atoi(port_str));                                  errstr = "connect";
                         sin.sin_addr.s_addr = inet_addr(host);                                  close(s);
                         if ( sin.sin_addr.s_addr != -1 ) {                                  s = -1;
                                 sin.sin_family = AF_INET;                                  continue;
                         } else {  
                                 hp = gethostbyname(host);  
                                 bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);  
                                 sin.sin_family = hp->h_addrtype;  
                         }                          }
                         len = sizeof(sin);  
                         saddr = (struct sockaddr *)&sin;                          /* established a connection */
                 }  
                 if ( connect(s,saddr,len) >= 0 )  
                         break;                          break;
                 else {                  }
                         close(s);  
                   if (s >= 0) {
                           freeaddrinfo(res);
                           return (s);
                   }
   
 #if defined(VISUAL)  #if defined(VISUAL)
                         Sleep(100);                  Sleep(100);
 #else  #else
                         usleep(100000);                  usleep(100000);
 #endif  #endif
                 }  
         }          }
         if ( i == 10 ) {          freeaddrinfo(res);
                 perror("connect");  
                 return -1;          perror(errstr);
         } else          return (-1);
                 return s;  
 }  }
   
 #if 0  #if 0

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.9.2.1

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>