- TCPContext *s = NULL;
- fd_set wfds;
- int fd_max, ret;
- struct timeval tv;
- socklen_t optlen;
- char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
-
- url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
- &port, path, sizeof(path), uri); // PETR: use url_split
- if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
- if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
-
- s = av_malloc(sizeof(TCPContext));
- if (!s)
- return AVERROR(ENOMEM);
- h->priv_data = s;
-
- if (port <= 0 || port >= 65536)
- goto fail;
-
- if(!ff_network_init())
+ TCPContext *s = h->priv_data;
+ const char *p;
+ char buf[256];
+ int ret;
+ char hostname[1024],proto[1024],path[1024];
+ char portstr[10];
+
+ av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
+ &port, path, sizeof(path), uri);
+ if (strcmp(proto, "tcp"))
+ return AVERROR(EINVAL);
+ if (port <= 0 || port >= 65536) {
+ av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
+ return AVERROR(EINVAL);
+ }
+ p = strchr(uri, '?');
+ if (p) {
+ if (av_find_info_tag(buf, sizeof(buf), "listen", p))
+ s->listen = 1;
+ if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
+ s->timeout = strtol(buf, NULL, 10) * 100;
+ }
+ if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
+ s->listen_timeout = strtol(buf, NULL, 10);
+ }
+ }
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ snprintf(portstr, sizeof(portstr), "%d", port);
+ if (s->listen)
+ hints.ai_flags |= AI_PASSIVE;
+ if (!hostname[0])
+ ret = getaddrinfo(NULL, portstr, &hints, &ai);
+ else
+ ret = getaddrinfo(hostname, portstr, &hints, &ai);
+ if (ret) {
+ av_log(h, AV_LOG_ERROR,
+ "Failed to resolve hostname %s: %s\n",
+ hostname, gai_strerror(ret));