Skip to content

Commit 44676e6

Browse files
author
Wei Li
committed
fix dns not found server crash error
1 parent 5f2b2bf commit 44676e6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

proxylab-handout/csapp.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,15 @@ int open_clientfd(char *hostname, char *port) {
981981
hints.ai_socktype = SOCK_STREAM; /* Open a connection */
982982
hints.ai_flags = AI_NUMERICSERV; /* ... using a numeric port arg. */
983983
hints.ai_flags |= AI_ADDRCONFIG; /* Recommended for connections */
984-
Getaddrinfo(hostname, port, &hints, &listp);
984+
985+
/* fixed by weili.
986+
* To avoid crashing the server, we don't report an error directly
987+
*/
988+
if (getaddrinfo(hostname, port, &hints, &listp) != 0) {
989+
fprintf(stderr, "Could not resolve host: %s\n", hostname);
990+
return -1;
991+
}
992+
// Getaddrinfo(hostname, port, &hints, &listp);
985993

986994
/* Walk the list for one that we can successfully connect to */
987995
for (p = listp; p; p = p->ai_next) {

proxylab-handout/proxy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void forward_response(const char *key, int infd, int outfd)
102102
bytes_free(&response);
103103
}
104104

105+
105106
/* forward -- forward the request to remote server and the response from
106107
* remote server
107108
*/
@@ -211,6 +212,8 @@ void forward(int fromfd)
211212
#endif
212213
int clientfd = open_clientfd_ww(host, port);
213214
if (clientfd < 0) {
215+
// TODO: is it ok to directly return?
216+
// Maybe better error handling expected.
214217
return ;
215218
}
216219
if (rio_writen_ww(clientfd, request_buf, strlen(request_buf)) < 0) {
@@ -223,6 +226,7 @@ void forward(int fromfd)
223226
close_ww(clientfd);
224227
}
225228

229+
226230
/* thread -- the things to do for each thread */
227231
void *thread(void *vargp)
228232
{

0 commit comments

Comments
 (0)