FFmpeg
gopher.c
Go to the documentation of this file.
1 /*
2  * Gopher protocol
3  *
4  * Copyright (c) 2009 Toshimitsu Kimura
5  * Copyright (c) 2021 parazyd <parazyd@dyne.org>
6  *
7  * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "config.h"
27 #include "config_components.h"
28 
29 #include "libavutil/avstring.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "network.h"
33 #include "url.h"
34 
35 typedef struct GopherContext {
38 
39 static int gopher_write(URLContext *h, const uint8_t *buf, int size)
40 {
41  GopherContext *s = h->priv_data;
42  return ffurl_write(s->hd, buf, size);
43 }
44 
45 static int gopher_connect(URLContext *h, const char *path)
46 {
47  char buffer[1024];
48 
49  if (!*path) return AVERROR(EINVAL);
50  switch (*++path) {
51  case '5':
52  case '9':
53  path = strchr(path, '/');
54  if (!path) return AVERROR(EINVAL);
55  break;
56  default:
58  "Gopher protocol type '%c' not supported yet!\n",
59  *path);
60  return AVERROR(EINVAL);
61  }
62 
63  /* send gopher sector */
64  snprintf(buffer, sizeof(buffer), "%s\r\n", path);
65 
66  if (gopher_write(h, buffer, strlen(buffer)) < 0)
67  return AVERROR(EIO);
68 
69  return 0;
70 }
71 
73 {
74  GopherContext *s = h->priv_data;
75  ffurl_closep(&s->hd);
76  return 0;
77 }
78 
79 static int gopher_open(URLContext *h, const char *uri, int flags)
80 {
81  GopherContext *s = h->priv_data;
82  char proto[10], hostname[1024], auth[1024], path[1024], buf[1024];
83  int port, err;
84  const char *lower_proto = "tcp";
85 
86  h->is_streamed = 1;
87 
88  /* needed in any case to build the host string */
89  av_url_split(proto, sizeof(proto), auth, sizeof(auth),
90  hostname, sizeof(hostname), &port, path, sizeof(path), uri);
91 
92  if (port < 0)
93  port = 70;
94 
95  if (!strcmp(proto, "gophers"))
96  lower_proto = "tls";
97 
98  ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
99 
100  s->hd = NULL;
102  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
103  if (err < 0)
104  goto fail;
105 
106  if ((err = gopher_connect(h, path)) < 0)
107  goto fail;
108  return 0;
109  fail:
110  gopher_close(h);
111  return err;
112 }
113 
114 static int gopher_read(URLContext *h, uint8_t *buf, int size)
115 {
116  GopherContext *s = h->priv_data;
117  int len = ffurl_read(s->hd, buf, size);
118  return len;
119 }
120 
121 #if CONFIG_GOPHER_PROTOCOL
123  .name = "gopher",
124  .url_open = gopher_open,
125  .url_read = gopher_read,
126  .url_write = gopher_write,
127  .url_close = gopher_close,
128  .priv_data_size = sizeof(GopherContext),
130  .default_whitelist = "gopher,tcp"
131 };
132 #endif /* CONFIG_GOPHER_PROTOCOL */
133 
134 #if CONFIG_GOPHERS_PROTOCOL
136  .name = "gophers",
137  .url_open = gopher_open,
138  .url_read = gopher_read,
139  .url_write = gopher_write,
140  .url_close = gopher_close,
141  .priv_data_size = sizeof(GopherContext),
143  .default_whitelist = "gopher,gophers,tcp,tls"
144 };
145 #endif /* CONFIG_GOPHERS_PROTOCOL */
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
URL_PROTOCOL_FLAG_NETWORK
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:33
AVIO_FLAG_READ_WRITE
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:638
ffurl_write
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: url.h:208
GopherContext
Definition: gopher.c:35
gopher_open
static int gopher_open(URLContext *h, const char *uri, int flags)
Definition: gopher.c:79
URLProtocol
Definition: url.h:53
fail
#define fail()
Definition: checkasm.h:138
gopher_close
static int gopher_close(URLContext *h)
Definition: gopher.c:72
ffurl_open_whitelist
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition: avio.c:300
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_url_join
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: url.c:40
internal.h
NULL
#define NULL
Definition: coverity.c:32
gopher_write
static int gopher_write(URLContext *h, const uint8_t *buf, int size)
Definition: gopher.c:39
gopher_connect
static int gopher_connect(URLContext *h, const char *path)
Definition: gopher.c:45
size
int size
Definition: twinvq_data.h:10344
URLProtocol::name
const char * name
Definition: url.h:54
URLContext
Definition: url.h:37
av_url_split
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:358
url.h
len
int len
Definition: vorbis_enc_data.h:426
ff_gophers_protocol
const URLProtocol ff_gophers_protocol
ffurl_closep
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:439
gopher_read
static int gopher_read(URLContext *h, uint8_t *buf, int size)
Definition: gopher.c:114
avformat.h
network.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
GopherContext::hd
URLContext * hd
Definition: gopher.c:36
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038
ff_gopher_protocol
const URLProtocol ff_gopher_protocol
avstring.h
snprintf
#define snprintf
Definition: snprintf.h:34
ffurl_read
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: url.h:183