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