diff options
Diffstat (limited to 'utils.c')
| -rw-r--r-- | utils.c | 57 |
1 files changed, 57 insertions, 0 deletions
| @@ -912,3 +912,60 @@ void import_csv(snac *user) | |||
| 912 | else | 912 | else |
| 913 | snac_log(user, xs_fmt("Cannot open file %s", fn)); | 913 | snac_log(user, xs_fmt("Cannot open file %s", fn)); |
| 914 | } | 914 | } |
| 915 | |||
| 916 | static const struct { | ||
| 917 | const char *proto; | ||
| 918 | unsigned short default_port; | ||
| 919 | } FALLBACK_PORTS[] = { | ||
| 920 | /* caution: https > http, smpts > smtp */ | ||
| 921 | {"https", 443}, | ||
| 922 | {"http", 80}, | ||
| 923 | {"smtps", 465}, | ||
| 924 | {"smtp", 25} | ||
| 925 | }; | ||
| 926 | |||
| 927 | int parse_port(const char *url, const char **errstr) | ||
| 928 | { | ||
| 929 | const char *col, *rcol; | ||
| 930 | int tmp, ret = -1; | ||
| 931 | |||
| 932 | if (errstr) | ||
| 933 | *errstr = NULL; | ||
| 934 | |||
| 935 | if (!(col = strchr(url, ':'))) { | ||
| 936 | if (errstr) | ||
| 937 | *errstr = "bad url"; | ||
| 938 | |||
| 939 | return -1; | ||
| 940 | } | ||
| 941 | |||
| 942 | for (size_t i = 0; i < sizeof(FALLBACK_PORTS) / sizeof(*FALLBACK_PORTS); ++i) { | ||
| 943 | if (memcmp(url, FALLBACK_PORTS[i].proto, strlen(FALLBACK_PORTS[i].proto)) == 0) { | ||
| 944 | ret = FALLBACK_PORTS[i].default_port; | ||
| 945 | break; | ||
| 946 | } | ||
| 947 | } | ||
| 948 | |||
| 949 | if (!(rcol = strchr(col + 1, ':'))) | ||
| 950 | rcol = col; | ||
| 951 | |||
| 952 | if (rcol) { | ||
| 953 | tmp = atoi(rcol + 1); | ||
| 954 | if (tmp == 0) { | ||
| 955 | if (ret != -1) | ||
| 956 | return ret; | ||
| 957 | |||
| 958 | if (errstr) | ||
| 959 | *errstr = strerror(errno); | ||
| 960 | |||
| 961 | return -1; | ||
| 962 | } | ||
| 963 | |||
| 964 | return tmp; | ||
| 965 | } | ||
| 966 | |||
| 967 | if (errstr) | ||
| 968 | *errstr = "unknown protocol"; | ||
| 969 | |||
| 970 | return -1; | ||
| 971 | } | ||