source: roaraudio/libroar/vio_socket.c @ 1339:818d2eb4e11a

Last change on this file since 1339:818d2eb4e11a was 1339:818d2eb4e11a, checked in by phi, 15 years ago

added comment about converting

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