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