1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
11 /*************************************************
12 * Extract port from address string *
13 *************************************************/
15 /* In the spool file, and in the -oMa and -oMi options, a host plus port is
16 given as an IP address followed by a dot and a port number. This function
19 An alternative format for the -oMa and -oMi options is [ip address]:port which
20 is what Exim 4 uses for output, because it seems to becoming commonly used,
21 whereas the dot form confuses some programs/people. So we recognize that form
25 address points to the string; if there is a port, the '.' in the string
26 is overwritten with zero to terminate the address; if the string
27 is in the [xxx]:ppp format, the address is shifted left and the
30 Returns: 0 if there is no port, else the port number. If there's a syntax
31 error, leave the incoming address alone, and return 0.
35 host_address_extract_port(uschar * address)
40 /* Handle the "bracketed with colon on the end" format */
44 uschar *rb = address + 1;
45 while (*rb != 0 && *rb != ']') rb++;
46 if (*rb++ == 0) return 0; /* Missing ]; leave invalid address */
49 port = Ustrtol(rb + 1, &endptr, 10);
50 if (*endptr != 0) return 0; /* Invalid port; leave invalid address */
52 else if (*rb != 0) return 0; /* Bad syntax; leave invalid address */
53 memmove(address, address + 1, rb - address - 2);
57 /* Handle the "dot on the end" format */
61 int skip = -3; /* Skip 3 dots in IPv4 addresses */
63 while (*(++address) != 0)
66 if (ch == ':') skip = 0; /* Skip 0 dots in IPv6 addresses */
67 else if (ch == '.' && skip++ >= 0) break;
69 if (*address == 0) return 0;
70 port = Ustrtol(address + 1, &endptr, 10);
71 if (*endptr != 0) return 0; /* Invalid port; leave invalid address */