On 2010-07-22 14:50, Romulo Goncalves wrote: >>>>> please check the man page of gethostbyname, first. >>>>> >>>>> It only mentions h_errno, but does not mention errno. >>>>> >>>>> It also says: >>>>> " >>>>> The *(obsolete)* herror() function prints the error message associated >>>>> with the current value of h_errno on stderr. >>>>> >>>>> The *(obsolete)* hstrerror() function takes an error number (typically >>>>> h_errno) and returns the corresponding message string. >>>>> " >>>>> >>>>> Hence, I'm not sure how portable these "obsolete" functions are ... >>>>> >>>>> But then, it also says >>>>> " >>>>> POSIX.1-2008 removes the specifications of gethostbyname(), gethost- >>>>> byaddr(), and h_errno, recommending the use of getaddrinfo(3) and >>>>> getnameinfo(3) instead. >>>>> " >>>>> >>>>> Hence, even gethostbyname() itself might eventually disappear ... >>>>> >>>>> Stefan > > Ok. > > Lets clean the code. > I did not write this code, so I am still trying to understand all the code. > > Here, I use gai_strerror to print the error. The new error message is > *Name or service not known*, instead of, *Unknown host*. > > I had the following extension to test my code: > connect to 'localhost' port 55000 database 'demo' USER 'monetdb' > PASSWORD 'monetdb' LANGUAGE 'sql'; > disconnect 'localhost_demo_monetdb'; > > And it works. > > > To avoid more check ins and complains I want green light for the > following code:
You're not getting it. ;-)
In case HAVE_SYS_UN_H is not defined (Windows), you'll probably get
errors about unused variables (serv and servize). Move those to where
userver is declared.
I also don't like the way you assign the port. I'd rather you use
char port[32];
snprintf(buf, sizeof(port), miod->port);
and use port instead of NULL in the call to getaddrinfo. Then you don't
have to assign the port anymore.
Furthermore, if you look at the example in the getaddrinfo manual page,
you'll see that it uses a loop to connect. This is because the function
can return multiple results, and the first one doesn't necessarily work,
so you have to try them all. You should do that too.
Also fix your indents. Use tabs.
>
> [gonca...@alviss mapilib]$ hg diff
> diff -r f9af81c7874b clients/src/mapilib/Mapi.mx
> --- a/clients/src/mapilib/Mapi.mx Thu Jul 22 13:18:20 2010 +0200
> +++ b/clients/src/mapilib/Mapi.mx Thu Jul 22 14:47:58 2010 +0200
> @@ -2329,8 +2329,6 @@
> static MapiMsg
> connect_to_server(Mapi mid)
> {
> - struct sockaddr_in server;
> -
> #ifdef HAVE_SYS_UN_H
> struct sockaddr_un userver;
> #endif
> @@ -2399,29 +2397,28 @@
> } else
> #endif
> {
> - struct hostent *hp;
> + struct addrinfo hints, *res;
> + int err;
>
> if (mid->hostname == NULL)
> mid->hostname = strdup("localhost");
> -
> - if ((hp = gethostbyname(mid->hostname)) == NULL) {
> - snprintf(errbuf, sizeof(errbuf), "gethostbyname failed:
> %s", errno ? strerror(errno) : hstrerror(h_errno));
> + memset(&hints, 0, sizeof(hints));
> + hints.ai_socktype = SOCK_STREAM;
> + hints.ai_protocol = IPPROTO_TCP;
> +
> + if ((err = getaddrinfo(mid->hostname,NULL,&hints, &res)) != 0) {
> + snprintf(errbuf, sizeof(errbuf), "gethostbyname failed:
> %s", gai_strerror(err));
> return mapi_setError(mid, errbuf, "mapi_reconnect", MERROR);
> }
> - memset(&server, 0, sizeof(server));
> - memcpy(&server.sin_addr, hp->h_addr_list[0], hp->h_length);
> - server.sin_family = hp->h_addrtype;
> - server.sin_port = htons((unsigned short) (mid->port & 0xFFFF));
> - serv = (struct sockaddr *) &server;
> - servsize = sizeof(server);
> - s = socket(server.sin_family, SOCK_STREAM, IPPROTO_TCP);
> + ((struct sockaddr_in*)res->ai_addr)->sin_port = htons((unsigned
> short) (mid->port & 0xFFFF));
> + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
>
> if (s == INVALID_SOCKET) {
> snprintf(errbuf, sizeof(errbuf), "opening socket failed:
> %s", strerror(errno));
> return mapi_setError(mid, errbuf, "mapi_reconnect", MERROR);
> }
>
> - if (connect(s, serv, servsize) < 0) {
> + if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
> snprintf(errbuf, sizeof(errbuf),
> "initiating connection on socket failed: %s",
> strerror(errno));
>
>
> Regards,
> Romulo
> _______________________________________________
> Checkin-list mailing list
> [email protected]
> http://mail.monetdb.org/mailman/listinfo/checkin-list
--
Sjoerd Mullender
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Checkin-list mailing list [email protected] http://mail.monetdb.org/mailman/listinfo/checkin-list
