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 
29 #include <sys/un.h>
30 
31 #include "libavutil/avstring.h"
32 #include "libavutil/opt.h"
33 #include "os_support.h"
34 #include "network.h"
35 #include "url.h"
36 
37 typedef struct UnixContext {
38  const AVClass *class;
39  struct sockaddr_un addr;
40  int timeout;
41  int listen;
42  int type;
43  int fd;
44 } UnixContext;
45 
46 #define OFFSET(x) offsetof(UnixContext, x)
47 #define ED AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_ENCODING_PARAM
48 static const AVOption unix_options[] = {
49  { "listen", "Open socket for listening", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ED },
50  { "timeout", "Timeout in ms", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ED },
51  { "type", "Socket type", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, "type" },
52  { "stream", "Stream (reliable stream-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, "type" },
53  { "datagram", "Datagram (unreliable packet-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_DGRAM }, INT_MIN, INT_MAX, ED, "type" },
54  { "seqpacket", "Seqpacket (reliable packet-oriented", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_SEQPACKET }, INT_MIN, INT_MAX, ED, "type" },
55  { NULL }
56 };
57 
58 static const AVClass unix_class = {
59  .class_name = "unix",
60  .item_name = av_default_item_name,
61  .option = unix_options,
62  .version = LIBAVUTIL_VERSION_INT,
63 };
64 
65 static int unix_open(URLContext *h, const char *filename, int flags)
66 {
67  UnixContext *s = h->priv_data;
68  int fd, ret;
69 
70  av_strstart(filename, "unix:", &filename);
71  s->addr.sun_family = AF_UNIX;
72  av_strlcpy(s->addr.sun_path, filename, sizeof(s->addr.sun_path));
73 
74  if ((fd = ff_socket(AF_UNIX, s->type, 0)) < 0)
75  return ff_neterrno();
76 
77  if (s->listen) {
78  fd = ff_listen_bind(fd, (struct sockaddr *)&s->addr,
79  sizeof(s->addr), s->timeout, h);
80  if (fd < 0) {
81  ret = fd;
82  goto fail;
83  }
84  } else {
85  ret = ff_listen_connect(fd, (struct sockaddr *)&s->addr,
86  sizeof(s->addr), s->timeout, h, 0);
87  if (ret < 0)
88  goto fail;
89  }
90 
91  s->fd = fd;
92 
93  return 0;
94 
95 fail:
96  if (s->listen && AVUNERROR(ret) != EADDRINUSE)
97  unlink(s->addr.sun_path);
98  if (fd >= 0)
99  closesocket(fd);
100  return ret;
101 }
102 
103 static int unix_read(URLContext *h, uint8_t *buf, int size)
104 {
105  UnixContext *s = h->priv_data;
106  int ret;
107 
108  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
109  ret = ff_network_wait_fd(s->fd, 0);
110  if (ret < 0)
111  return ret;
112  }
113  ret = recv(s->fd, buf, size, 0);
114  return ret < 0 ? ff_neterrno() : ret;
115 }
116 
117 static int unix_write(URLContext *h, const uint8_t *buf, int size)
118 {
119  UnixContext *s = h->priv_data;
120  int ret;
121 
122  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
123  ret = ff_network_wait_fd(s->fd, 1);
124  if (ret < 0)
125  return ret;
126  }
127  ret = send(s->fd, buf, size, 0);
128  return ret < 0 ? ff_neterrno() : ret;
129 }
130 
131 static int unix_close(URLContext *h)
132 {
133  UnixContext *s = h->priv_data;
134  if (s->listen)
135  unlink(s->addr.sun_path);
136  closesocket(s->fd);
137  return 0;
138 }
139 
141 {
142  UnixContext *s = h->priv_data;
143  return s->fd;
144 }
145 
147  .name = "unix",
148  .url_open = unix_open,
149  .url_read = unix_read,
150  .url_write = unix_write,
151  .url_close = unix_close,
152  .url_get_file_handle = unix_get_file_handle,
153  .priv_data_size = sizeof(UnixContext),
154  .priv_data_class = &unix_class,
156 };
static const AVClass unix_class
Definition: unix.c:58
int size
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
AVOption.
Definition: opt.h:234
int timeout
Definition: unix.c:40
static int unix_write(URLContext *h, const uint8_t *buf, int size)
Definition: unix.c:117
static int unix_get_file_handle(URLContext *h)
Definition: unix.c:140
int flags
Definition: url.h:46
#define OFFSET(x)
Definition: unix.c:46
int ff_socket(int af, int type, int proto)
Definition: network.c:214
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:38
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:232
uint8_t
AVOptions.
miscellaneous OS support macros and functions.
static int flags
Definition: log.c:44
int fd
Definition: unix.c:43
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:261
#define ED
Definition: unix.c:47
int type
Definition: unix.c:42
static int unix_close(URLContext *h)
Definition: unix.c:131
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
int listen
Definition: unix.c:41
#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:311
av_default_item_name
Definition: dnxhdenc.c:52
Definition: url.h:41
Describe the class of an AVClass context structure.
Definition: log.h:33
void * priv_data
Definition: url.h:44
static const AVOption unix_options[]
Definition: unix.c:48
static int unix_read(URLContext *h, uint8_t *buf, int size)
Definition: unix.c:103
static int unix_open(URLContext *h, const char *filename, int flags)
Definition: unix.c:65
const char * name
Definition: url.h:54
URLProtocol ff_unix_protocol
Definition: unix.c:146
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
int ff_network_wait_fd(int fd, int write)
Definition: network.c:141
unbuffered private I/O API
struct sockaddr_un addr
Definition: unix.c:39