Ignore:
Timestamp:
Nov 24, 2016, 1:14:11 PM (9 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to version 4.4.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/lib/addns/dnssock.c

    r740 r988  
    2828#include <unistd.h>
    2929#include "system/select.h"
     30#include "../lib/util/debug.h"
    3031
    3132static int destroy_dns_connection(struct dns_connection *conn)
     
    4142                               struct dns_connection **result )
    4243{
    43         uint32_t ulAddress;
    44         struct hostent *pHost;
    45         struct sockaddr_in s_in;
     44        struct addrinfo hints;
     45        struct addrinfo *ai_result = NULL;
     46        struct addrinfo *rp;
    4647        struct dns_connection *conn;
    47         int res;
     48        int ret;
     49        char service[16];
     50
     51        snprintf(service, sizeof(service), "%d", DNS_TCP_PORT);
    4852
    4953        if (!(conn = talloc(mem_ctx, struct dns_connection))) {
     
    5155        }
    5256
    53         if ( (ulAddress = inet_addr( nameserver )) == INADDR_NONE ) {
    54                 if ( (pHost = gethostbyname( nameserver )) == NULL ) {
    55                         TALLOC_FREE(conn);
    56                         return ERROR_DNS_INVALID_NAME_SERVER;
    57                 }
    58                 memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
    59         }
    60 
    61         conn->s = socket( PF_INET, SOCK_STREAM, 0 );
    62         if (conn->s == -1) {
     57        memset(&hints, 0, sizeof(struct addrinfo));
     58        hints.ai_family = AF_UNSPEC;
     59        hints.ai_socktype = SOCK_STREAM;
     60        hints.ai_flags = 0;
     61        hints.ai_protocol = IPPROTO_TCP;
     62
     63        ret = getaddrinfo(nameserver, service, &hints, &ai_result);
     64        if (ret != 0) {
     65                DEBUG(1,("dns_tcp_open: getaddrinfo: %s\n", gai_strerror(ret)));
     66                TALLOC_FREE(conn);
     67                return ERROR_DNS_INVALID_NAME_SERVER;
     68        }
     69
     70        for (rp = ai_result; rp != NULL; rp = rp->ai_next) {
     71                conn->s = socket(rp->ai_family,
     72                                rp->ai_socktype,
     73                                rp->ai_protocol);
     74                if (conn->s == -1) {
     75                        continue;
     76                }
     77                do {
     78                        ret = connect(conn->s, rp->ai_addr, rp->ai_addrlen);
     79                } while ((ret == -1) && (errno == EINTR));
     80                if (ret != -1) {
     81                        /* Successful connect */
     82                        break;
     83                }
     84                close(conn->s);
     85        }
     86
     87        freeaddrinfo(ai_result);
     88
     89        /* Failed to connect with any address */
     90        if (rp == NULL) {
    6391                TALLOC_FREE(conn);
    6492                return ERROR_DNS_CONNECTION_FAILED;
     
    6795        talloc_set_destructor(conn, destroy_dns_connection);
    6896
    69         s_in.sin_family = AF_INET;
    70         s_in.sin_addr.s_addr = ulAddress;
    71         s_in.sin_port = htons( DNS_TCP_PORT );
    72 
    73         res = connect(conn->s, (struct sockaddr*)&s_in, sizeof( s_in ));
    74         if (res == -1) {
    75                 TALLOC_FREE(conn);
    76                 return ERROR_DNS_CONNECTION_FAILED;
    77         }
    78 
    7997        conn->hType = DNS_TCP;
    80 
    8198        *result = conn;
    8299        return ERROR_DNS_SUCCESS;
     
    84101
    85102/********************************************************************
    86 ********************************************************************/
     103 * ********************************************************************/
    87104
    88105static DNS_ERROR dns_udp_open( const char *nameserver,
     
    90107                               struct dns_connection **result )
    91108{
    92         unsigned long ulAddress;
    93         struct hostent *pHost;
    94         struct sockaddr_in RecvAddr;
     109        struct addrinfo hints;
     110        struct addrinfo *ai_result = NULL;
     111        struct addrinfo *rp;
     112        struct sockaddr_storage RecvAddr;
    95113        struct dns_connection *conn;
     114        int ret;
     115        socklen_t RecvAddrLen;
     116        char service[16];
     117
     118        snprintf(service, sizeof(service), "%d", DNS_UDP_PORT);
    96119
    97120        if (!(conn = talloc(NULL, struct dns_connection))) {
     
    99122        }
    100123
    101         if ( (ulAddress = inet_addr( nameserver )) == INADDR_NONE ) {
    102                 if ( (pHost = gethostbyname( nameserver )) == NULL ) {
    103                         TALLOC_FREE(conn);
    104                         return ERROR_DNS_INVALID_NAME_SERVER;
    105                 }
    106                 memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
    107         }
    108 
    109         /* Create a socket for sending data */
    110 
    111         conn->s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
    112         if (conn->s == -1) {
     124        memset(&hints, 0, sizeof(struct addrinfo));
     125        hints.ai_family = AF_UNSPEC;
     126        hints.ai_socktype = SOCK_DGRAM;
     127        hints.ai_flags = 0;
     128        hints.ai_protocol = IPPROTO_UDP;
     129
     130        ret = getaddrinfo(nameserver, service, &hints, &ai_result);
     131        if (ret != 0) {
     132                DEBUG(1,("dns_ucp_open:getaddrinfo: %s\n", gai_strerror(ret)));
     133                TALLOC_FREE(conn);
     134                return ERROR_DNS_INVALID_NAME_SERVER;
     135        }
     136
     137        for (rp = ai_result; rp != NULL; rp = rp->ai_next) {
     138                conn->s = socket(rp->ai_family,
     139                                rp->ai_socktype,
     140                                rp->ai_protocol);
     141                if (conn->s == -1) {
     142                        continue;
     143                }
     144                ret = connect(conn->s, rp->ai_addr, rp->ai_addrlen);
     145                if (ret != -1) {
     146                        /* Successful connect */
     147                        break;
     148                }
     149                close(conn->s);
     150        }
     151
     152        freeaddrinfo(ai_result);
     153
     154        /* Failed to connect with any address */
     155        if (rp == NULL) {
    113156                TALLOC_FREE(conn);
    114157                return ERROR_DNS_CONNECTION_FAILED;
     
    118161
    119162        /* Set up the RecvAddr structure with the IP address of
    120            the receiver (in this example case "123.456.789.1")
    121            and the specified port number. */
    122 
    123         ZERO_STRUCT(RecvAddr);
    124         RecvAddr.sin_family = AF_INET;
    125         RecvAddr.sin_port = htons( DNS_UDP_PORT );
    126         RecvAddr.sin_addr.s_addr = ulAddress;
     163           the receiver and the specified port number. */
     164
     165        RecvAddrLen = sizeof(RecvAddr);
     166        if (getpeername(conn->s,
     167                        (struct sockaddr *)&RecvAddr,
     168                        &RecvAddrLen) == -1) {
     169                TALLOC_FREE(conn);
     170                return ERROR_DNS_CONNECTION_FAILED;
     171        }
    127172
    128173        conn->hType = DNS_UDP;
    129         memcpy( &conn->RecvAddr, &RecvAddr, sizeof( struct sockaddr_in ) );
     174        memcpy(&conn->RecvAddr, &RecvAddr, sizeof(struct sockaddr_storage));
    130175
    131176        *result = conn;
     
    136181********************************************************************/
    137182
    138 DNS_ERROR dns_open_connection( const char *nameserver, int32 dwType,
     183DNS_ERROR dns_open_connection( const char *nameserver, int32_t dwType,
    139184                    TALLOC_CTX *mem_ctx,
    140185                    struct dns_connection **conn )
     
    150195}
    151196
    152 static DNS_ERROR write_all(int fd, uint8 *data, size_t len)
     197static DNS_ERROR write_all(int fd, uint8_t *data, size_t len)
    153198{
    154199        size_t total = 0;
     
    156201        while (total < len) {
    157202
    158                 ssize_t ret = write(fd, data + total, len - total);
     203                ssize_t ret;
     204
     205                do {
     206                        ret = write(fd, data + total, len - total);
     207                } while ((ret == -1) && (errno == EINTR));
    159208
    160209                if (ret <= 0) {
     
    174223                              const struct dns_buffer *buf)
    175224{
    176         uint16 len = htons(buf->offset);
     225        uint16_t len = htons(buf->offset);
    177226        DNS_ERROR err;
    178227
    179         err = write_all(conn->s, (uint8 *)&len, sizeof(len));
     228        err = write_all(conn->s, (uint8_t *)&len, sizeof(len));
    180229        if (!ERR_DNS_IS_OK(err)) return err;
    181230
     
    188237        ssize_t ret;
    189238
    190         ret = sendto(conn->s, buf->data, buf->offset, 0,
     239        do {
     240                ret = sendto(conn->s, buf->data, buf->offset, 0,
    191241                     (struct sockaddr *)&conn->RecvAddr,
    192242                     sizeof(conn->RecvAddr));
     243        } while ((ret == -1) && (errno == EINTR));
    193244
    194245        if (ret != buf->offset) {
     
    212263}
    213264
    214 static DNS_ERROR read_all(int fd, uint8 *data, size_t len)
     265static DNS_ERROR read_all(int fd, uint8_t *data, size_t len)
    215266{
    216267        size_t total = 0;
     
    226277
    227278                fd_ready = poll(&pfd, 1, 10000);
     279                if (fd_ready == -1) {
     280                        if (errno == EINTR) {
     281                                continue;
     282                        }
     283                        return ERROR_DNS_SOCKET_ERROR;
     284                }
    228285                if ( fd_ready == 0 ) {
    229286                        /* read timeout */
     
    231288                }
    232289
    233                 ret = read(fd, data + total, len - total);
     290                do {
     291                        ret = read(fd, data + total, len - total);
     292                } while ((ret == -1) && (errno == EINTR));
     293
    234294                if (ret <= 0) {
    235295                        /* EOF or error */
     
    249309        struct dns_buffer *buf;
    250310        DNS_ERROR err;
    251         uint16 len;
    252 
    253         if (!(buf = TALLOC_ZERO_P(mem_ctx, struct dns_buffer))) {
    254                 return ERROR_DNS_NO_MEMORY;
    255         }
    256 
    257         err = read_all(conn->s, (uint8 *)&len, sizeof(len));
     311        uint16_t len;
     312
     313        if (!(buf = talloc_zero(mem_ctx, struct dns_buffer))) {
     314                return ERROR_DNS_NO_MEMORY;
     315        }
     316
     317        err = read_all(conn->s, (uint8_t *)&len, sizeof(len));
    258318        if (!ERR_DNS_IS_OK(err)) {
    259319                return err;
     
    262322        buf->size = ntohs(len);
    263323
    264         if (buf->size) {
    265                 if (!(buf->data = TALLOC_ARRAY(buf, uint8, buf->size))) {
    266                         TALLOC_FREE(buf);
    267                         return ERROR_DNS_NO_MEMORY;
    268                 }
    269         } else {
    270                 buf->data = NULL;
    271         }
    272 
    273         err = read_all(conn->s, buf->data, buf->size);
     324        if (buf->size == 0) {
     325                *presult = buf;
     326                return ERROR_DNS_SUCCESS;
     327        }
     328
     329        if (!(buf->data = talloc_array(buf, uint8_t, buf->size))) {
     330                TALLOC_FREE(buf);
     331                return ERROR_DNS_NO_MEMORY;
     332        }
     333
     334        err = read_all(conn->s, buf->data, talloc_get_size(buf->data));
    274335        if (!ERR_DNS_IS_OK(err)) {
    275336                TALLOC_FREE(buf);
     
    288349        ssize_t received;
    289350
    290         if (!(buf = TALLOC_ZERO_P(mem_ctx, struct dns_buffer))) {
     351        if (!(buf = talloc_zero(mem_ctx, struct dns_buffer))) {
    291352                return ERROR_DNS_NO_MEMORY;
    292353        }
     
    296357         */
    297358
    298         if (!(buf->data = TALLOC_ARRAY(buf, uint8, 512))) {
     359        if (!(buf->data = talloc_array(buf, uint8_t, 512))) {
    299360                TALLOC_FREE(buf);
    300361                return ERROR_DNS_NO_MEMORY;
    301362        }
    302363
    303         received = recv(conn->s, (void *)buf->data, 512, 0);
     364        do {
     365                received = recv(conn->s, (void *)buf->data, 512, 0);
     366        } while ((received == -1) && (errno == EINTR));
    304367
    305368        if (received == -1) {
     
    341404        DNS_ERROR err;
    342405
    343         err = dns_marshall_request(conn, req, &buf);
     406        err = dns_marshall_request(mem_ctx, req, &buf);
    344407        if (!ERR_DNS_IS_OK(err)) goto error;
    345408
Note: See TracChangeset for help on using the changeset viewer.