]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/url.c
ITS#7428 Use non-blocking IO during SSL Handshake
[openldap] / libraries / libldap / url.c
index 30b15d11ddf5ed1c79b58f4ac4ea350316de024f..88f19c742efe2a457fe77d7e7ab3b4a6a6c93f1d 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2007 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -552,7 +552,7 @@ desc2str_len( LDAPURLDesc *u )
        }
 
        if ( u->lud_filter ) {
-               len +=  hex_escape_len( u->lud_filter, URLESC_NONE );
+               len += hex_escape_len( u->lud_filter, URLESC_NONE );
                if ( !sep ) {
                        sep = 4;
                }
@@ -566,7 +566,7 @@ desc2str_len( LDAPURLDesc *u )
        }
 
        if ( u->lud_attrs ) {
-               len +=  hex_escape_len_list( u->lud_attrs, URLESC_NONE );
+               len += hex_escape_len_list( u->lud_attrs, URLESC_NONE );
                if ( !sep ) {
                        sep = 2;
                }
@@ -582,19 +582,19 @@ desc2str_len( LDAPURLDesc *u )
        len += sep;
 
        if ( u->lud_port ) {
-               char    buf[] = ":65535";
+               unsigned p = u->lud_port;
+               if ( p > 65535 )
+                       return -1;
 
-               len += snprintf( buf, sizeof( buf ), ":%d", u->lud_port );
-               if ( u->lud_host && u->lud_host[0] ) {
-                       len += strlen( u->lud_host );
-               }
+               len += (p > 999 ? 5 + (p > 9999) : p > 99 ? 4 : 2 + (p > 9));
+       }
 
-       } else {
-               if ( u->lud_host && u->lud_host[0] ) {
-                       len += hex_escape_len( u->lud_host, URLESC_SLASH );
-                       if ( !is_ipc && strchr( u->lud_host, ':' )) {
+       if ( u->lud_host && u->lud_host[0] ) {
+               char *ptr;
+               len += hex_escape_len( u->lud_host, URLESC_SLASH );
+               if ( !is_ipc && ( ptr = strchr( u->lud_host, ':' ))) {
+                       if ( strchr( ptr+1, ':' ))
                                len += 2;       /* IPv6, [] */
-                       }
                }
        }
 
@@ -612,6 +612,7 @@ desc2str( LDAPURLDesc *u, char *s, int len )
        int             is_v6 = 0;
        int             is_ipc = 0;
        struct berval   scope = BER_BVNULL;
+       char            *ptr;
 
        if ( u == NULL ) {
                return -1;
@@ -639,19 +640,22 @@ desc2str( LDAPURLDesc *u, char *s, int len )
                sep = 1;
        }
 
-       if ( !is_ipc && u->lud_host && strchr( u->lud_host, ':' )) {
-               is_v6 = 1;
+       if ( !is_ipc && u->lud_host && ( ptr = strchr( u->lud_host, ':' ))) {
+               if ( strchr( ptr+1, ':' ))
+                       is_v6 = 1;
        }
 
        if ( u->lud_port ) {
-               len -= sprintf( s, "%s://%s%s%s:%d%n", u->lud_scheme,
+               sofar = sprintf( s, "%s://%s%s%s:%d", u->lud_scheme,
                                is_v6 ? "[" : "",
                                u->lud_host ? u->lud_host : "",
                                is_v6 ? "]" : "",
-                               u->lud_port, &sofar );
+                               u->lud_port );
+               len -= sofar;
 
        } else {
-               len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
+               sofar = sprintf( s, "%s://", u->lud_scheme );
+               len -= sofar;
                if ( u->lud_host && u->lud_host[0] ) {
                        if ( is_v6 ) {
                                s[sofar++] = '[';
@@ -928,7 +932,11 @@ ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp, unsigned flags
                        }
                        /* check for Novell kludge */
                        if ( !p ) {
-                               q = next+1;
+                               if ( *next != '\0' ) {
+                                       q = &next[1];
+                               } else {
+                                       q = NULL;
+                               }
                        }
                }
 
@@ -1417,6 +1425,7 @@ ldap_url_list2hosts (LDAPURLDesc *ludlist)
        /* figure out how big the string is */
        size = 1;       /* nul-term */
        for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
+               if ( ludp->lud_host == NULL ) continue;
                size += strlen(ludp->lud_host) + 1;             /* host and space */
                if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
                        size += 2;
@@ -1429,6 +1438,7 @@ ldap_url_list2hosts (LDAPURLDesc *ludlist)
 
        p = s;
        for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
+               if ( ludp->lud_host == NULL ) continue;
                if (strchr(ludp->lud_host, ':')) {
                        p += sprintf(p, "[%s]", ludp->lud_host);
                } else {
@@ -1441,7 +1451,7 @@ ldap_url_list2hosts (LDAPURLDesc *ludlist)
        }
        if (p != s)
                p--;    /* nuke that extra space */
-       *p = 0;
+       *p = '\0';
        return s;
 }