dinhvh / libetpan

Mail Framework for C Language
www.etpan.org
Other
612 stars 284 forks source link

Fix mailsmtp_init_with_ip for IPv6 on Linux #433

Open d99kris opened 1 year ago

d99kris commented 1 year ago

Hi! Firstly, thanks for the great work on this library! I've been using it in my TUI email client nmail for 4 years and it's been very solid to work with.

Now to my problem: It appears that mailsmtp_init_with_ip(smtp, 1) (i.e. using ip address instead of hostname) fails on Linux when socket address is of IPv6 type.

There are two reasons for this:

  1. getnameinfo() is called with sizeof(addr) (which is 16) so this call fails with EAI_FAMILY (-6) for IPv6 addresses. We can fix this by using addr_len which is populated with the actual address length by getsockname().

  2. The host address populated by getnameinfo() may be suffixed by % and a network interface specifier, and at least Gmail does not accept this format, and returns an error like:

    501-5.5.4 HELO/EHLO argument "[2400:e800:2000:b00:4000:0:600:0%808590000]"
    501-5.5.4 invalid, closing connection.
    501 5.5.4 https://support.google.com/mail/?p=helo a14-20020a62bd0e000000b0064d413caea6sm3299641pff.179 - gsmtp

    This can be fixed by simply dropping the interface specifier from the IPv6 address.

I've tested the patch on IPv6 and IPv4 on Linux.