| skipped 59 lines |
60 | 60 | | char *tbuf; // Pointer to temporary buffer for parsing domain name |
61 | 61 | | D(char *x;) |
62 | 62 | | |
63 | | - | if ((tbuf = calloc(strlen(ip)+1, 1)) == NULL) // Create temporary buffer |
| 63 | + | if ((tbuf = calloc(strlen(ip)+1, 1)) == NULL) { // Create temporary buffer |
| 64 | + | close(sock); |
64 | 65 | | return NULL; |
| 66 | + | } |
65 | 67 | | |
66 | 68 | | memcpy(tbuf, ip, strlen(ip)); |
67 | 69 | | qp = (char *) (buf + sizeof(DNS_header)); // Skip over header and build DNS formatted name |
| skipped 16 lines |
84 | 86 | | buflen = (size_t)qp - (size_t)buf; |
85 | 87 | | n = sendto(sock, buf, buflen, 0, (struct sockaddr *) &sin, sin_len); |
86 | 88 | | if (n < 0) { |
| 89 | + | close(sock); |
87 | 90 | | DLX(4, perror("Could not send DNS query")); |
88 | 91 | | return NULL; |
89 | 92 | | } |
| skipped 13 lines |
103 | 106 | | n = recv(sock, buf, MAX_MSG_LENGTH, 0); |
104 | 107 | | if (n < 0) { |
105 | 108 | | DLX(4, perror("Error receiving DNS response")); |
| 109 | + | close(sock); |
106 | 110 | | return NULL; |
107 | 111 | | } |
108 | 112 | | if (n < (int)sizeof(DNS_header)) // Must at least see a DNS-sized header |
| skipped 1 lines |
110 | 114 | | continue; |
111 | 115 | | header = (DNS_header *)buf; |
112 | 116 | | } while (ntohs(header->id) != queryID && !header->qr && response_timeout == WAITING); // QR must be set and the header ID must match the queryID |
| 117 | + | close(sock); |
113 | 118 | | alarm(0); // Kill timer |
114 | 119 | | |
115 | 120 | | if (response_timeout == TIMED_OUT) { |
| skipped 5 lines |
121 | 126 | | DLX(4, printf("%s did not resolve\n", ip)); |
122 | 127 | | return NULL; |
123 | 128 | | } |
124 | | - | |
125 | | - | #if 0 |
126 | | - | response = (DNS_response *)(buf + sizeof(DNS_header)); |
127 | | - | DPB(6, "DNS response: ", response, n - sizeof(DNS_header)); |
128 | | - | DLX(4, printf("RR Type: %d\n", ntohs(response->type))); |
129 | | - | if (ntohs(response->type) != A_RECORD) { |
130 | | - | DLX(4, printf("Response was not an A record.\n")); |
131 | | - | return NULL; |
132 | | - | } |
133 | | - | |
134 | | - | if ((resolved_ip = (char *)malloc(16)) == NULL) { |
135 | | - | DLX(4, printf("Failed to malloc resolved IP buffer.\n")); |
136 | | - | return NULL; |
137 | | - | } |
138 | | - | // Convert decimal IP address back to a character string |
139 | | - | if ((inet_ntop(AF_INET, response->rdata, resolved_ip, 16)) == NULL) { |
140 | | - | DLX(4, printf("inet_ntop() failed to convert IP to string.\n")); |
141 | | - | return NULL; |
142 | | - | } |
143 | | - | |
144 | | - | return resolved_ip; // Return IP address |
145 | | - | #endif |
146 | 129 | | |
147 | 130 | | return (decode_dns(response)); |
148 | 131 | | } |
| skipped 9 lines |