source: roaraudio/libroar/vio_socket.c @ 1335:548e9191245b

Last change on this file since 1335:548e9191245b was 1335:548e9191245b, checked in by phi, 15 years ago

got socket: and tcp: working, most others only need backend to be completed

File size: 8.5 KB
Line 
1//vio_socket.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int     roar_vio_open_def_socket          (struct roar_vio_calls * calls, struct roar_vio_defaults * def) {
38 int       fh  = -1;
39 socklen_t len =  0;
40
41 if ( calls == NULL || def == NULL )
42  return -1;
43
44 if ( def->type != ROAR_VIO_DEF_TYPE_SOCKET )
45  return -1;
46
47 switch (def->d.socket.domain) {
48  case AF_INET:
49    len = sizeof(struct sockaddr_in);
50
51    if ( roar_vio_socket_init_inet4host_def(def) == -1 )
52     return -1;
53
54    switch (def->d.socket.type) {
55     case SOCK_STREAM:
56       fh = roar_socket_new_tcp();
57      break;
58     case SOCK_DGRAM:
59       fh = roar_socket_new_udp();
60      break;
61     default:
62       return -1;
63    }
64   break;
65#ifdef ROAR_HAVE_UNIX
66  case AF_UNIX:
67    len = sizeof(struct sockaddr_un);
68
69    switch (def->d.socket.type) {
70     case SOCK_STREAM:
71       fh = roar_socket_new_unix();
72      break;
73     case SOCK_DGRAM:
74       return -1;
75      break;
76     default:
77       return -1;
78    }
79   break;
80#endif
81#ifdef ROAR_HAVE_LIBDNET
82  case AF_DECnet:
83    len = sizeof(struct sockaddr_dn);
84
85    return -1;
86   break;
87#endif
88#ifdef ROAR_HAVE_IPV6
89  case AF_INET6:
90    len = sizeof(struct sockaddr_in6);
91
92    switch (def->d.socket.type) {
93     case SOCK_STREAM:
94       fh = roar_socket_new_tcp6();
95      break;
96     case SOCK_DGRAM:
97       fh = roar_socket_new_udp6();
98      break;
99     default:
100       return -1;
101    }
102   break;
103#endif
104#ifdef ROAR_HAVE_IPX
105  case AF_IPX:
106    len = sizeof(struct sockaddr_ipx);
107
108    return -1;
109   break;
110#endif
111  default:
112    return -1;
113 }
114
115 if ( fh == -1 )
116  return -1;
117
118 if ( connect(fh, &(def->d.socket.sa.sa), len) == -1 ) {
119  close(fh);
120  return -1;
121 }
122
123 if ( roar_vio_open_fh_socket(calls, fh) == -1 ) {
124  close(fh);
125  return -1;
126 }
127
128 return 0;
129}
130
131int     roar_vio_socket_init_socket_def   (struct roar_vio_defaults * def, int domain, int type) {
132 if ( def == NULL || domain == -1 || type == -1 )
133  return -1;
134
135 // we do not memset(def, 0, sizeof(...)) here
136 // because this is allready done in roar_vio_dstr_init_defaults()
137 // if we would be would override o_flags/o_mode and maybe others
138
139 memset(&(def->d.socket.sa), 0, sizeof(def->d.socket.sa));
140
141 def->type                     = ROAR_VIO_DEF_TYPE_SOCKET;
142 def->d.socket.domain          = domain;
143 def->d.socket.type            = type;
144 def->d.socket.sa.sa.sa_family = domain;
145
146 return 0;
147}
148
149int     roar_vio_socket_init_dstr_def     (struct roar_vio_defaults * def, char * dstr, int hint, int type,
150                                           struct roar_vio_defaults * odef) {
151 char * host;
152 int    port;
153
154 if ( def == NULL || dstr == NULL )
155  return -1;
156
157 ROAR_WARN("roar_vio_socket_init_dstr_def(def=%p, dstr='%s', hint=%i, type=%i, odef=%p) = ?", def, dstr, hint, type, odef);
158
159 if ( hint == -1 ) {
160  if ( 0 ) { // this is needed to keep the syntx ok, compiler will throw it away
161#ifdef ROAR_HAVE_IPV6
162  } else if ( strstr(dstr, "[") != NULL ) { // [ip]:service
163   hint = AF_INET6;
164#endif
165#ifdef ROAR_HAVE_LIBDNET
166  } else if ( strstr(dstr, "::") != NULL ) { // node::object
167   hint = AF_DECnet;
168#endif
169#ifdef ROAR_HAVE_IPX
170  } else if ( strstr(dstr, "(") != NULL ) { // net:mac(service)
171   hint = AF_IPX;
172#endif
173#ifdef ROAR_HAVE_UNIX
174  } else if ( strstr(dstr, "/") != NULL ) { // /path/to/sock
175   hint = AF_UNIX;
176#endif
177  } else if ( strstr(dstr, ":") != NULL ) { // host:port
178   hint = AF_INET;
179  }
180 }
181
182 if ( hint == -1 && odef != NULL ) { // if we still don't know what this is we try
183                                     // to use the parent objects request
184  if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
185   hint = odef->d.socket.domain;
186  }
187 }
188
189 if ( hint == -1 ) /* we really have no glue what this is... */
190  return -1;
191
192#ifdef ROAR_HAVE_UNIX
193 if ( hint == AF_UNIX ) {
194  if ( *dstr != 0 && strcmp(dstr, "//") != 0 ) {
195   return roar_vio_socket_init_unix_def(def, dstr);
196  } else {
197   if ( roar_vio_socket_conv_def(odef, AF_UNIX) == -1 )
198    return -1;
199
200   return roar_vio_socket_init_unix_def(def, odef->d.socket.sa.un.sun_path);
201  }
202 }
203#endif
204
205 for (; *dstr == '/'; dstr++);
206
207 switch (hint) {
208  case AF_INET:
209    host = dstr;
210    for (; *dstr != 0 && *dstr != ':'; dstr++);
211
212    if ( *dstr == ':' ) { // we have a port :)
213     *dstr++ = 0;
214     if ( (port = roar_vio_socket_get_port(dstr, AF_INET, type)) == -1 )
215      return -1;
216
217     return roar_vio_socket_init_inet4_def(def, host, port, type);
218    } else {
219     if ( roar_vio_socket_conv_def(odef, AF_INET) == -1 )
220      return -1;
221
222     return roar_vio_socket_init_inet4_def(def, host, ROAR_NET2HOST16(odef->d.socket.sa.in.sin_port), type);
223    }
224   break;
225#ifdef ROAR_HAVE_LIBDNET
226  case AF_DECnet:
227    return -1;
228   break;
229#endif
230#ifdef ROAR_HAVE_IPV6
231  case AF_INET6:
232    return -1;
233   break;
234#endif
235#ifdef ROAR_HAVE_IPX
236  case AF_IPX:
237    return -1;
238   break;
239#endif
240  default:
241    return -1;
242 }
243
244 return 0;
245}
246
247int     roar_vio_socket_conv_def          (struct roar_vio_defaults * def, int domain) {
248 return -1;
249}
250
251int     roar_vio_socket_get_port          (char * service, int domain, int type) {
252 if ( service == NULL || domain == -1 || type == -1 )
253  return -1;
254
255 // TODO: we should write something better
256 return atoi(service);
257}
258
259// AF_UNIX:
260int     roar_vio_socket_init_unix_def     (struct roar_vio_defaults * def, char * path) {
261#ifdef ROAR_HAVE_UNIX
262 if ( roar_vio_socket_init_socket_def(def, AF_UNIX, SOCK_STREAM) == -1 )
263  return -1;
264
265 strncpy(def->d.socket.sa.un.sun_path, path, sizeof(def->d.socket.sa.un.sun_path) - 1);
266
267 return 0;
268#else
269 return -1;
270#endif
271}
272
273// AF_DECnet:
274int     roar_vio_socket_init_decnet_def   (struct roar_vio_defaults * def, char * node, int object, char * objname);
275
276
277// AF_INET:
278int     roar_vio_socket_init_inet4host_def(struct roar_vio_defaults * def) {
279 struct hostent     * he;
280
281 if ( def == NULL )
282  return -1;
283
284 if ( def->d.socket.host == NULL )
285  return -1;
286
287 if ( (he = gethostbyname(def->d.socket.host)) == NULL ) {
288  ROAR_ERR("roar_vio_socket_init_inet4host_def(*): Can\'t resolve host name '%s'",
289                    def->d.socket.host);
290  return -1;
291 }
292
293 memcpy((struct in_addr *)&def->d.socket.sa.in.sin_addr, he->h_addr, sizeof(struct in_addr));
294
295 return 0;
296}
297
298int     roar_vio_socket_init_inet4_def    (struct roar_vio_defaults * def, char * host, int port, int type) {
299 if ( roar_vio_socket_init_socket_def(def, AF_INET, type) == -1 )
300  return -1;
301
302 def->d.socket.host             = host;
303
304 def->d.socket.sa.in.sin_port   = ROAR_HOST2NET16(port);
305
306 return 0;
307}
308
309int     roar_vio_socket_init_tcp4_def     (struct roar_vio_defaults * def, char * host, int port) {
310 return roar_vio_socket_init_inet4_def(def, host, port, SOCK_STREAM);
311}
312
313int     roar_vio_socket_init_udp4_def     (struct roar_vio_defaults * def, char * host, int port) {
314 return roar_vio_socket_init_inet4_def(def, host, port, SOCK_DGRAM);
315}
316
317
318// AF_INET6:
319int     roar_vio_socket_init_inet6host_def(struct roar_vio_defaults * def);
320int     roar_vio_socket_init_inet6_def    (struct roar_vio_defaults * def, char * host, int port, int type) {
321 return -1;
322}
323
324int     roar_vio_socket_init_tcp6_def     (struct roar_vio_defaults * def, char * host, int port) {
325 return roar_vio_socket_init_inet6_def(def, host, port, SOCK_STREAM);
326}
327
328int     roar_vio_socket_init_udp6_def     (struct roar_vio_defaults * def, char * host, int port) {
329 return roar_vio_socket_init_inet6_def(def, host, port, SOCK_DGRAM);
330}
331
332//ll
Note: See TracBrowser for help on using the repository browser.