Libav
unix.c
Go to the documentation of this file.
1 /*
2  * Unix socket protocol
3  * Copyright (c) 2013 Luca Barbato
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include <sys/un.h>
29 
30 #include "libavutil/avstring.h"
31 #include "libavutil/opt.h"
32 #include "os_support.h"
33 #include "network.h"
34 #include "url.h"
35 
36 typedef struct UnixContext {
37  const AVClass *class;
38  struct sockaddr_un addr;
39  int timeout;
40  int listen;
41  int type;
42  int fd;
43 } UnixContext;
44 
45 #define OFFSET(x) offsetof(UnixContext, x)
46 #define ED AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_ENCODING_PARAM
47 static const AVOption unix_options[] = {
48  { "listen", "Open socket for listening", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ED },
49  { "timeout", "Timeout in ms", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ED },
50  { "type", "Socket type", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, "type" },
51  { "stream", "Stream (reliable stream-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, "type" },
52  { "datagram", "Datagram (unreliable packet-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_DGRAM }, INT_MIN, INT_MAX, ED, "type" },
53  { "seqpacket", "Seqpacket (reliable packet-oriented", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_SEQPACKET }, INT_MIN, INT_MAX, ED, "type" },
54  { NULL }
55 };
56 
57 static const AVClass unix_class = {
58  .class_name = "unix",
59  .item_name = av_default_item_name,
60  .option = unix_options,
61  .version = LIBAVUTIL_VERSION_INT,
62 };
63 
64 static int unix_open(URLContext *h, const char *filename, int flags)
65 {
66  UnixContext *s = h->priv_data;
67  int fd, ret;
68 
69  av_strstart(filename, "unix:", &filename);
70  s->addr.sun_family = AF_UNIX;
71  av_strlcpy(s->addr.sun_path, filename, sizeof(s->addr.sun_path));
72 
73  if ((fd = ff_socket(AF_UNIX, s->type, 0)) < 0)
74  return ff_neterrno();
75 
76  if (s->timeout < 0 && h->rw_timeout)
77  s->timeout = h->rw_timeout / 1000;
78 
79  if (s->listen) {
80  ret = ff_listen_bind(fd, (struct sockaddr *)&s->addr,
81  sizeof(s->addr), s->timeout, h);
82  if (ret < 0)
83  goto fail;
84  fd = ret;
85  } else {
86  ret = ff_listen_connect(fd, (struct sockaddr *)&s->addr,
87  sizeof(s->addr), s->timeout, h, 0);
88  if (ret < 0)
89  goto fail;
90  }
91 
92  s->fd = fd;
93 
94  return 0;
95 
96 fail:
97  if (s->listen && AVUNERROR(ret) != EADDRINUSE)
98  unlink(s->addr.sun_path);
99  if (fd >= 0)
100  closesocket(fd);
101  return ret;
102 }
103 
104 static int unix_read(URLContext *h, uint8_t *buf, int size)
105 {
106  UnixContext *s = h->priv_data;
107  int ret;
108 
109  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
110  ret = ff_network_wait_fd(s->fd, 0);
111  if (ret < 0)
112  return ret;
113  }
114  ret = recv(s->fd, buf, size, 0);
115  return ret < 0 ? ff_neterrno() : ret;
116 }
117 
118 static int unix_write(URLContext *h, const uint8_t *buf, int size)
119 {
120  UnixContext *s = h->priv_data;
121  int ret;
122 
123  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
124  ret = ff_network_wait_fd(s->fd, 1);
125  if (ret < 0)
126  return ret;
127  }
128  ret = send(s->fd, buf, size, MSG_NOSIGNAL);
129  return ret < 0 ? ff_neterrno() : ret;
130 }
131 
132 static int unix_close(URLContext *h)
133 {
134  UnixContext *s = h->priv_data;
135  if (s->listen)
136  unlink(s->addr.sun_path);
137  closesocket(s->fd);
138  return 0;
139 }
140 
142 {
143  UnixContext *s = h->priv_data;
144  return s->fd;
145 }
146 
148  .name = "unix",
149  .url_open = unix_open,
150  .url_read = unix_read,
151  .url_write = unix_write,
152  .url_close = unix_close,
153  .url_get_file_handle = unix_get_file_handle,
154  .priv_data_size = sizeof(UnixContext),
155  .priv_data_class = &unix_class,
157 };
static const AVClass unix_class
Definition: unix.c:57
int size
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
AVOption.
Definition: opt.h:234
int timeout
Definition: unix.c:39
static int unix_write(URLContext *h, const uint8_t *buf, int size)
Definition: unix.c:118
static int unix_get_file_handle(URLContext *h)
Definition: unix.c:141
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in microseconds ...
Definition: url.h:52
int flags
Definition: url.h:47
#define OFFSET(x)
Definition: unix.c:45
int ff_socket(int af, int type, int proto)
Definition: network.c:141
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
Definition: network.c:163
uint8_t
AVOptions.
miscellaneous OS support macros and functions.
static int flags
Definition: log.c:50
int fd
Definition: unix.c:42
int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
Connect to a file descriptor and poll for result.
Definition: network.c:192
const URLProtocol ff_unix_protocol
Definition: unix.c:147
#define ED
Definition: unix.c:46
int type
Definition: unix.c:41
static int unix_close(URLContext *h)
Definition: unix.c:132
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define fail()
Definition: checkasm.h:80
int listen
Definition: unix.c:40
#define ff_neterrno()
Definition: network.h:63
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:387
av_default_item_name
Definition: dnxhdenc.c:55
Definition: url.h:38
Describe the class of an AVClass context structure.
Definition: log.h:34
void * priv_data
Definition: url.h:45
static const AVOption unix_options[]
Definition: unix.c:47
static int unix_read(URLContext *h, uint8_t *buf, int size)
Definition: unix.c:104
static int unix_open(URLContext *h, const char *filename, int flags)
Definition: unix.c:64
const char * name
Definition: url.h:56
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:32
#define AVUNERROR(e)
Definition: error.h:44
#define MSG_NOSIGNAL
Definition: network.h:102
int ff_network_wait_fd(int fd, int write)
Definition: network.c:68
unbuffered private I/O API
struct sockaddr_un addr
Definition: unix.c:38