muduo icon indicating copy to clipboard operation
muduo copied to clipboard

gethostbyname_r ?

Open gh2517956473 opened this issue 3 years ago • 2 comments

Why not use getaddrinfo instead of gethostbyname_r ?

gh2517956473 avatar Mar 08 '21 03:03 gh2517956473

I guess for MT-safe.

https://man7.org/linux/man-pages/man3/gethostbyname_r.3.html#ATTRIBUTES

┌───────────────────┬───────────────┬───────────────────────────────┐
│Interface          │ Attribute     │ Value                         │
├───────────────────┼───────────────┼───────────────────────────────┤
│gethostbyname()    │ Thread safety │ MT-Unsafe race:hostbyname env │
│                   │               │ locale                        │
├───────────────────┼───────────────┼───────────────────────────────┤
│gethostbyaddr()    │ Thread safety │ MT-Unsafe race:hostbyaddr env │
│                   │               │ locale                        │
├───────────────────┼───────────────┼───────────────────────────────┤
│sethostent(),      │ Thread safety │ MT-Unsafe race:hostent env    │
│endhostent(),      │               │ locale                        │
│gethostent_r()     │               │                               │
├───────────────────┼───────────────┼───────────────────────────────┤
│herror(),          │ Thread safety │ MT-Safe                       │
│hstrerror()        │               │                               │
├───────────────────┼───────────────┼───────────────────────────────┤
│gethostent()       │ Thread safety │ MT-Unsafe race:hostent        │
│                   │               │ race:hostentbuf env locale    │
├───────────────────┼───────────────┼───────────────────────────────┤
│gethostbyname2()   │ Thread safety │ MT-Unsafe race:hostbyname2    │
│                   │               │ env locale                    │
├───────────────────┼───────────────┼───────────────────────────────┤
│gethostbyaddr_r(), │ Thread safety │ MT-Safe env locale            │
│gethostbyname_r(), │               │                               │
│gethostbyname2_r() │               │                               │
└───────────────────┴───────────────┴───────────────────────────────┘

DigGe avatar Nov 25 '21 06:11 DigGe

The same page also says:

       The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror()
       functions are obsolete.  Applications should use [getaddrinfo(3)](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html),
       [getnameinfo(3)](https://man7.org/linux/man-pages/man3/getnameinfo.3.html), and [gai_strerror(3)](https://man7.org/linux/man-pages/man3/gai_strerror.3.html) instead.

and getaddrinfo(3) is also MT-Safe env locale.

ericonr avatar Sep 05 '22 14:09 ericonr