source: roaraudio/libroar/vio_socket.c @ 1337:133222591226

Last change on this file since 1337:133222591226 was 1337:133222591226, checked in by phi, 15 years ago

support sockets with no dst to use defaults from parent object

File size: 9.2 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 )
155  return -1;
156
157 if ( dstr == NULL && odef == NULL )
158  return -1;
159
160 if ( dstr == NULL )
161  dstr = "";
162
163 ROAR_WARN("roar_vio_socket_init_dstr_def(def=%p, dstr='%s', hint=%i, type=%i, odef=%p) = ?", def, dstr, hint, type, odef);
164
165 if ( hint == -1 ) {
166  if ( 0 ) { // this is needed to keep the syntx ok, compiler will throw it away
167#ifdef ROAR_HAVE_IPV6
168  } else if ( strstr(dstr, "[") != NULL ) { // [ip]:service
169   hint = AF_INET6;
170#endif
171#ifdef ROAR_HAVE_LIBDNET
172  } else if ( strstr(dstr, "::") != NULL ) { // node::object
173   hint = AF_DECnet;
174#endif
175#ifdef ROAR_HAVE_IPX
176  } else if ( strstr(dstr, "(") != NULL ) { // net:mac(service)
177   hint = AF_IPX;
178#endif
179#ifdef ROAR_HAVE_UNIX
180  } else if ( strstr(dstr, "/") != NULL ) { // /path/to/sock
181   hint = AF_UNIX;
182#endif
183  } else if ( strstr(dstr, ":") != NULL ) { // host:port
184   hint = AF_INET;
185  }
186 }
187
188 if ( hint == -1 && odef != NULL ) { // if we still don't know what this is we try
189                                     // to use the parent objects request
190  if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
191   hint = odef->d.socket.domain;
192  }
193 }
194
195 if ( hint == -1 ) /* we really have no glue what this is... */
196  return -1;
197
198#ifdef ROAR_HAVE_UNIX
199 if ( hint == AF_UNIX ) {
200  if ( *dstr != 0 && strcmp(dstr, "//") != 0 ) {
201   return roar_vio_socket_init_unix_def(def, dstr);
202  } else {
203   if ( roar_vio_socket_conv_def(odef, AF_UNIX) == -1 )
204    return -1;
205
206   return roar_vio_socket_init_unix_def(def, odef->d.socket.sa.un.sun_path);
207  }
208 }
209#endif
210
211 if ( *dstr == 0 ) {
212  if ( roar_vio_socket_conv_def(odef, hint) == -1 )
213   return -1;
214
215  if ( odef->d.socket.type != type )
216   return -1;
217
218  memcpy(def, odef, sizeof(struct roar_vio_defaults));
219  return 0;
220 }
221
222 for (; *dstr == '/'; dstr++);
223
224 switch (hint) {
225  case AF_INET:
226    host = dstr;
227    for (; *dstr != 0 && *dstr != ':'; dstr++);
228
229    if ( *dstr == ':' ) { // we have a port :)
230     *dstr++ = 0;
231     if ( (port = roar_vio_socket_get_port(dstr, AF_INET, type)) == -1 )
232      return -1;
233
234     return roar_vio_socket_init_inet4_def(def, host, port, type);
235    } else {
236     if ( roar_vio_socket_conv_def(odef, AF_INET) == -1 )
237      return -1;
238
239     return roar_vio_socket_init_inet4_def(def, host, ROAR_NET2HOST16(odef->d.socket.sa.in.sin_port), type);
240    }
241   break;
242#ifdef ROAR_HAVE_LIBDNET
243  case AF_DECnet:
244    return -1;
245   break;
246#endif
247#ifdef ROAR_HAVE_IPV6
248  case AF_INET6:
249    return -1;
250   break;
251#endif
252#ifdef ROAR_HAVE_IPX
253  case AF_IPX:
254    return -1;
255   break;
256#endif
257  default:
258    return -1;
259 }
260
261 return 0;
262}
263
264int     roar_vio_socket_conv_def          (struct roar_vio_defaults * def, int domain) {
265 if ( def == NULL || domain == -1 )
266  return -1;
267
268#ifdef ROAR_HAVE_UNIX
269 if ( domain == AF_UNIX ) {
270  if ( def->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
271   if ( def->d.socket.domain == AF_UNIX )
272    return 0;
273
274   return -1;
275  } else {
276   return -1;
277  }
278 }
279#endif
280
281 if ( def->type != ROAR_VIO_DEF_TYPE_SOCKET )
282  return -1;
283
284 if ( def->d.socket.domain == domain )
285  return 0;
286
287 return -1;
288}
289
290int     roar_vio_socket_get_port          (char * service, int domain, int type) {
291 if ( service == NULL || domain == -1 || type == -1 )
292  return -1;
293
294 // TODO: we should write something better
295 return atoi(service);
296}
297
298// AF_UNIX:
299int     roar_vio_socket_init_unix_def     (struct roar_vio_defaults * def, char * path) {
300#ifdef ROAR_HAVE_UNIX
301 if ( def == NULL || path == NULL )
302  return -1;
303
304 if ( roar_vio_socket_init_socket_def(def, AF_UNIX, SOCK_STREAM) == -1 )
305  return -1;
306
307 strncpy(def->d.socket.sa.un.sun_path, path, sizeof(def->d.socket.sa.un.sun_path) - 1);
308
309 return 0;
310#else
311 return -1;
312#endif
313}
314
315// AF_DECnet:
316int     roar_vio_socket_init_decnet_def   (struct roar_vio_defaults * def, char * node, int object, char * objname);
317
318
319// AF_INET:
320int     roar_vio_socket_init_inet4host_def(struct roar_vio_defaults * def) {
321 struct hostent     * he;
322
323 if ( def == NULL )
324  return -1;
325
326 if ( def->d.socket.host == NULL )
327  return -1;
328
329 if ( (he = gethostbyname(def->d.socket.host)) == NULL ) {
330  ROAR_ERR("roar_vio_socket_init_inet4host_def(*): Can\'t resolve host name '%s'",
331                    def->d.socket.host);
332  return -1;
333 }
334
335 memcpy((struct in_addr *)&def->d.socket.sa.in.sin_addr, he->h_addr, sizeof(struct in_addr));
336
337 return 0;
338}
339
340int     roar_vio_socket_init_inet4_def    (struct roar_vio_defaults * def, char * host, int port, int type) {
341 if ( roar_vio_socket_init_socket_def(def, AF_INET, type) == -1 )
342  return -1;
343
344 def->d.socket.host             = host;
345
346 def->d.socket.sa.in.sin_port   = ROAR_HOST2NET16(port);
347
348 return 0;
349}
350
351int     roar_vio_socket_init_tcp4_def     (struct roar_vio_defaults * def, char * host, int port) {
352 return roar_vio_socket_init_inet4_def(def, host, port, SOCK_STREAM);
353}
354
355int     roar_vio_socket_init_udp4_def     (struct roar_vio_defaults * def, char * host, int port) {
356 return roar_vio_socket_init_inet4_def(def, host, port, SOCK_DGRAM);
357}
358
359
360// AF_INET6:
361int     roar_vio_socket_init_inet6host_def(struct roar_vio_defaults * def);
362int     roar_vio_socket_init_inet6_def    (struct roar_vio_defaults * def, char * host, int port, int type) {
363 return -1;
364}
365
366int     roar_vio_socket_init_tcp6_def     (struct roar_vio_defaults * def, char * host, int port) {
367 return roar_vio_socket_init_inet6_def(def, host, port, SOCK_STREAM);
368}
369
370int     roar_vio_socket_init_udp6_def     (struct roar_vio_defaults * def, char * host, int port) {
371 return roar_vio_socket_init_inet6_def(def, host, port, SOCK_DGRAM);
372}
373
374//ll
Note: See TracBrowser for help on using the repository browser.