]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/os-ip.c
misc cleanup
[openldap] / libraries / libldap / os-ip.c
index 79bd49af9fb7d9048a3091cd3e346dfdd80c4cc1..02006651cbdb7dc2b4bfb94773f628f79a171ecf 100644 (file)
@@ -198,6 +198,18 @@ ldap_pvt_connect(LDAP *ld, ber_socket_t s,
        fd_set          efds;
 #endif
 
+#ifdef LDAP_CONNECTIONLESS
+       /* We could do a connect() but that would interfere with
+        * attempts to poll a broadcast address
+        */
+       if (LDAP_IS_UDP(ld)) {
+               if (ld->ld_options.ldo_peer)
+                       ldap_memfree(ld->ld_options.ldo_peer);
+               ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
+               AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
+               return ( 0 );
+       }
+#endif
        if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
                tv.tv_usec = opt_tv->tv_usec;
                tv.tv_sec = opt_tv->tv_sec;
@@ -286,16 +298,25 @@ int
 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
        int proto,
        const char *host,
-       unsigned long address, int port, int async)
+       unsigned long address, int port, int async )
 {
        struct sockaddr_in      sin;
        ber_socket_t            s = AC_SOCKET_INVALID;
        int                     rc, i, use_hp = 0;
        struct hostent          *hp = NULL;
        char                    *ha_buf=NULL, *p, *q;
+       int                     socktype;
 
        osip_debug(ld, "ldap_connect_to_host: %s\n",host,0,0);
        
+       switch(proto) {
+       case LDAP_PROTO_TCP: socktype = SOCK_STREAM; break;
+       case LDAP_PROTO_UDP: socktype = SOCK_DGRAM; break;
+       default: osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
+                               proto, 0, 0);
+               return -1;
+       }
+
        if (host != NULL) {
 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
                char serv[7];
@@ -304,19 +325,25 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
 
                memset( &hints, '\0', sizeof(hints) );
                hints.ai_family = AF_UNSPEC;
-               hints.ai_socktype = SOCK_STREAM;
+               hints.ai_socktype = socktype;
 
-               snprintf(serv, sizeof serv, "%d", ntohs(port));
+               snprintf(serv, sizeof serv, "%d", port );
                if ( err = getaddrinfo(host, serv, &hints, &res) ) {
                        osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
                                AC_GAI_STRERROR(err), 0, 0);
                        return -1;
                }
-               sai = res;
                rc = -1;
-               do {
+
+               for( sai=res; sai != NULL; sai=sai->ai_next) {
+                       if( sai->ai_addr == NULL ) {
+                               osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
+                                       "ai_addr is NULL?\n", 0, 0, 0);
+                               continue;
+                       }
+
                        /* we assume AF_x and PF_x are equal for all x */
-                       s = ldap_int_socket( ld, sai->ai_family, SOCK_STREAM );
+                       s = ldap_int_socket( ld, sai->ai_family, socktype );
                        if ( s == AC_SOCKET_INVALID ) {
                                continue;
                        }
@@ -353,7 +380,7 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
                                break;
                        }
                        ldap_pvt_close_socket(ld, s);
-               } while ((sai = sai->ai_next) != NULL);
+               }
                freeaddrinfo(res);
                return rc;
 
@@ -383,8 +410,7 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
 
        rc = s = -1;
        for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
-
-               s = ldap_int_socket( ld, PF_INET, SOCK_STREAM );
+               s = ldap_int_socket( ld, PF_INET, socktype );
                if ( s == AC_SOCKET_INVALID ) {
                        /* use_hp ? continue : break; */
                        break;
@@ -397,13 +423,13 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
 
                (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
                sin.sin_family = AF_INET;
-               sin.sin_port = port;
+               sin.sin_port = htons((short) port);
                p = (char *)&sin.sin_addr;
                q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
                AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
 
                osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
-                               inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
+                       inet_ntoa(sin.sin_addr),port,0);
 
                rc = ldap_pvt_connect(ld, s,
                        (struct sockaddr *)&sin, sizeof(struct sockaddr_in),