@@ -133,15 +133,27 @@ status_t HTTPStream::connect(const char *server, int port) {
133133 return ERROR_ALREADY_CONNECTED;
134134 }
135135
136- struct hostent *ent = gethostbyname (server);
137- if (ent == NULL ) {
136+ if (port < 0 || port > (int ) USHRT_MAX) {
137+ return UNKNOWN_ERROR;
138+ }
139+
140+ char service[sizeof (" 65536" )];
141+ sprintf (service, " %d" , port);
142+ struct addrinfo hints, *ai;
143+ memset (&hints, 0 , sizeof (hints));
144+ hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
145+ hints.ai_socktype = SOCK_STREAM;
146+
147+ int ret = getaddrinfo (server, service, &hints, &ai);
148+ if (ret) {
138149 return ERROR_UNKNOWN_HOST;
139150 }
140151
141152 CHECK_EQ (mSocket , -1 );
142- mSocket = socket (AF_INET, SOCK_STREAM, 0 );
153+ mSocket = socket (ai[ 0 ]. ai_family , ai[ 0 ]. ai_socktype , ai[ 0 ]. ai_protocol );
143154
144155 if (mSocket < 0 ) {
156+ freeaddrinfo (ai);
145157 return UNKNOWN_ERROR;
146158 }
147159
@@ -153,13 +165,9 @@ status_t HTTPStream::connect(const char *server, int port) {
153165
154166 mLock .unlock ();
155167
156- struct sockaddr_in addr;
157- addr.sin_family = AF_INET;
158- addr.sin_port = htons (port);
159- addr.sin_addr .s_addr = *(in_addr_t *)ent->h_addr ;
160- memset (addr.sin_zero , 0 , sizeof (addr.sin_zero ));
168+ status_t res = MyConnect (s, ai[0 ].ai_addr , ai[0 ].ai_addrlen );
161169
162- status_t res = MyConnect (s, ( const struct sockaddr *)&addr, sizeof (addr) );
170+ freeaddrinfo (ai );
163171
164172 mLock .lock ();
165173
0 commit comments