udphole

Basic UDP wormhole proxy
git clone git://git.finwo.net/app/udphole
Log | Files | Refs | README | LICENSE

url_utils.c (1167B)


      1 #include "url_utils.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "rxi/log.h"
      8 
      9 int parse_address_url(const char *addr, struct parsed_url **out) {
     10   if (!addr || !addr[0]) {
     11     return -1;
     12   }
     13 
     14   if (strstr(addr, "://") != NULL) {
     15     struct parsed_url *purl = parse_url(addr);
     16     if (!purl) {
     17       log_error("url_utils: failed to parse URL '%s'", addr);
     18       return -1;
     19     }
     20     *out = purl;
     21     return 0;
     22   }
     23 
     24   size_t len        = strlen(addr);
     25   char  *normalized = NULL;
     26 
     27   if (addr[0] == ':') {
     28     normalized = malloc(len + 8);
     29     if (!normalized) return -1;
     30     snprintf(normalized, len + 8, "tcp://*%s", addr);
     31   } else if (addr[0] >= '0' && addr[0] <= '9') {
     32     normalized = malloc(len + 8);
     33     if (!normalized) return -1;
     34     snprintf(normalized, len + 8, "tcp://*:%s", addr);
     35   } else {
     36     normalized = malloc(len + 8);
     37     if (!normalized) return -1;
     38     snprintf(normalized, len + 8, "tcp://%s", addr);
     39   }
     40 
     41   struct parsed_url *purl = parse_url(normalized);
     42   free(normalized);
     43 
     44   if (!purl) {
     45     log_error("url_utils: failed to parse normalized address '%s'", addr);
     46     return -1;
     47   }
     48 
     49   *out = purl;
     50   return 0;
     51 }