Changeset 988 for vendor/current/lib/addns/dnssock.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/lib/addns/dnssock.c
r740 r988 28 28 #include <unistd.h> 29 29 #include "system/select.h" 30 #include "../lib/util/debug.h" 30 31 31 32 static int destroy_dns_connection(struct dns_connection *conn) … … 41 42 struct dns_connection **result ) 42 43 { 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; 46 47 struct dns_connection *conn; 47 int res; 48 int ret; 49 char service[16]; 50 51 snprintf(service, sizeof(service), "%d", DNS_TCP_PORT); 48 52 49 53 if (!(conn = talloc(mem_ctx, struct dns_connection))) { … … 51 55 } 52 56 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) { 63 91 TALLOC_FREE(conn); 64 92 return ERROR_DNS_CONNECTION_FAILED; … … 67 95 talloc_set_destructor(conn, destroy_dns_connection); 68 96 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 79 97 conn->hType = DNS_TCP; 80 81 98 *result = conn; 82 99 return ERROR_DNS_SUCCESS; … … 84 101 85 102 /******************************************************************** 86 ********************************************************************/103 * ********************************************************************/ 87 104 88 105 static DNS_ERROR dns_udp_open( const char *nameserver, … … 90 107 struct dns_connection **result ) 91 108 { 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; 95 113 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); 96 119 97 120 if (!(conn = talloc(NULL, struct dns_connection))) { … … 99 122 } 100 123 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) { 113 156 TALLOC_FREE(conn); 114 157 return ERROR_DNS_CONNECTION_FAILED; … … 118 161 119 162 /* 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 } 127 172 128 173 conn->hType = DNS_UDP; 129 memcpy( &conn->RecvAddr, &RecvAddr, sizeof( struct sockaddr_in ));174 memcpy(&conn->RecvAddr, &RecvAddr, sizeof(struct sockaddr_storage)); 130 175 131 176 *result = conn; … … 136 181 ********************************************************************/ 137 182 138 DNS_ERROR dns_open_connection( const char *nameserver, int32 dwType,183 DNS_ERROR dns_open_connection( const char *nameserver, int32_t dwType, 139 184 TALLOC_CTX *mem_ctx, 140 185 struct dns_connection **conn ) … … 150 195 } 151 196 152 static DNS_ERROR write_all(int fd, uint8 *data, size_t len)197 static DNS_ERROR write_all(int fd, uint8_t *data, size_t len) 153 198 { 154 199 size_t total = 0; … … 156 201 while (total < len) { 157 202 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)); 159 208 160 209 if (ret <= 0) { … … 174 223 const struct dns_buffer *buf) 175 224 { 176 uint16 len = htons(buf->offset);225 uint16_t len = htons(buf->offset); 177 226 DNS_ERROR err; 178 227 179 err = write_all(conn->s, (uint8 *)&len, sizeof(len));228 err = write_all(conn->s, (uint8_t *)&len, sizeof(len)); 180 229 if (!ERR_DNS_IS_OK(err)) return err; 181 230 … … 188 237 ssize_t ret; 189 238 190 ret = sendto(conn->s, buf->data, buf->offset, 0, 239 do { 240 ret = sendto(conn->s, buf->data, buf->offset, 0, 191 241 (struct sockaddr *)&conn->RecvAddr, 192 242 sizeof(conn->RecvAddr)); 243 } while ((ret == -1) && (errno == EINTR)); 193 244 194 245 if (ret != buf->offset) { … … 212 263 } 213 264 214 static DNS_ERROR read_all(int fd, uint8 *data, size_t len)265 static DNS_ERROR read_all(int fd, uint8_t *data, size_t len) 215 266 { 216 267 size_t total = 0; … … 226 277 227 278 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 } 228 285 if ( fd_ready == 0 ) { 229 286 /* read timeout */ … … 231 288 } 232 289 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 234 294 if (ret <= 0) { 235 295 /* EOF or error */ … … 249 309 struct dns_buffer *buf; 250 310 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)); 258 318 if (!ERR_DNS_IS_OK(err)) { 259 319 return err; … … 262 322 buf->size = ntohs(len); 263 323 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)); 274 335 if (!ERR_DNS_IS_OK(err)) { 275 336 TALLOC_FREE(buf); … … 288 349 ssize_t received; 289 350 290 if (!(buf = TALLOC_ZERO_P(mem_ctx, struct dns_buffer))) {351 if (!(buf = talloc_zero(mem_ctx, struct dns_buffer))) { 291 352 return ERROR_DNS_NO_MEMORY; 292 353 } … … 296 357 */ 297 358 298 if (!(buf->data = TALLOC_ARRAY(buf, uint8, 512))) {359 if (!(buf->data = talloc_array(buf, uint8_t, 512))) { 299 360 TALLOC_FREE(buf); 300 361 return ERROR_DNS_NO_MEMORY; 301 362 } 302 363 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)); 304 367 305 368 if (received == -1) { … … 341 404 DNS_ERROR err; 342 405 343 err = dns_marshall_request( conn, req, &buf);406 err = dns_marshall_request(mem_ctx, req, &buf); 344 407 if (!ERR_DNS_IS_OK(err)) goto error; 345 408
Note:
See TracChangeset
for help on using the changeset viewer.