diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 30 |
1 files changed, 21 insertions, 9 deletions
@@ -153,17 +153,16 @@ void set_tox_username(Tox *tox) | |||
153 | // freeaddrinfo(info); | 153 | // freeaddrinfo(info); |
154 | } | 154 | } |
155 | // get sockaddr, IPv4 or IPv6: | 155 | // get sockaddr, IPv4 or IPv6: |
156 | /* From Beej */ | ||
157 | void *get_in_addr(struct sockaddr *sa) | 156 | void *get_in_addr(struct sockaddr *sa) |
158 | { | 157 | { |
159 | if (sa->sa_family == AF_INET) { | 158 | if (sa->sa_family == AF_INET) |
159 | { | ||
160 | return &(((struct sockaddr_in*)sa)->sin_addr); | 160 | return &(((struct sockaddr_in*)sa)->sin_addr); |
161 | } | 161 | } |
162 | 162 | ||
163 | return &(((struct sockaddr_in6*)sa)->sin6_addr); | 163 | return &(((struct sockaddr_in6*)sa)->sin6_addr); |
164 | } | 164 | } |
165 | 165 | ||
166 | /* From Beej */ | ||
167 | int get_client_socket(char *hostname, int port) | 166 | int get_client_socket(char *hostname, int port) |
168 | { | 167 | { |
169 | int sockfd, numbytes; | 168 | int sockfd, numbytes; |
@@ -179,13 +178,27 @@ int get_client_socket(char *hostname, int port) | |||
179 | hints.ai_family = AF_INET; | 178 | hints.ai_family = AF_INET; |
180 | hints.ai_socktype = SOCK_STREAM; | 179 | hints.ai_socktype = SOCK_STREAM; |
181 | 180 | ||
182 | if ((rv = getaddrinfo(hostname, port_str, &hints, &servinfo)) != 0) { | 181 | if ((rv = getaddrinfo(hostname, port_str, &hints, &servinfo)) != 0) |
183 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); | 182 | { |
184 | return -1; | 183 | /* Add a special case for "localhost" when name resolution is broken */ |
184 | if(!strncmp("localhost", hostname, 256)) | ||
185 | { | ||
186 | const char localhostname[] = "127.0.0.1"; | ||
187 | if ((rv = getaddrinfo(localhostname, port_str, &hints, &servinfo)) != 0) { | ||
188 | fprintf(stderr, "getaddrinfo failed for 127.0.0.1: %s\n", gai_strerror(rv)); | ||
189 | return -1; | ||
190 | } | ||
191 | } | ||
192 | else | ||
193 | { | ||
194 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); | ||
195 | return -1; | ||
196 | } | ||
185 | } | 197 | } |
186 | 198 | ||
187 | // loop through all the results and connect to the first we can | 199 | // loop through all the results and connect to the first we can |
188 | for(p = servinfo; p != NULL; p = p->ai_next) { | 200 | for(p = servinfo; p != NULL; p = p->ai_next) |
201 | { | ||
189 | if (p->ai_family != AF_INET && p->ai_family != AF_INET6) | 202 | if (p->ai_family != AF_INET && p->ai_family != AF_INET6) |
190 | continue; | 203 | continue; |
191 | 204 | ||
@@ -209,8 +222,7 @@ int get_client_socket(char *hostname, int port) | |||
209 | return -1; | 222 | return -1; |
210 | } | 223 | } |
211 | 224 | ||
212 | inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), | 225 | inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); |
213 | s, sizeof s); | ||
214 | fprintf(stderr, "connecting to %s\n", s); | 226 | fprintf(stderr, "connecting to %s\n", s); |
215 | 227 | ||
216 | freeaddrinfo(servinfo); // all done with this structure | 228 | freeaddrinfo(servinfo); // all done with this structure |