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 | |
---|
37 | int 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 | switch (def->d.socket.type) { |
---|
52 | case SOCK_STREAM: |
---|
53 | fh = roar_socket_new_tcp(); |
---|
54 | break; |
---|
55 | case SOCK_DGRAM: |
---|
56 | fh = roar_socket_new_udp(); |
---|
57 | break; |
---|
58 | default: |
---|
59 | return -1; |
---|
60 | } |
---|
61 | break; |
---|
62 | #ifdef ROAR_HAVE_UNIX |
---|
63 | case AF_UNIX: |
---|
64 | len = sizeof(struct sockaddr_un); |
---|
65 | |
---|
66 | switch (def->d.socket.type) { |
---|
67 | case SOCK_STREAM: |
---|
68 | fh = roar_socket_new_unix(); |
---|
69 | break; |
---|
70 | case SOCK_DGRAM: |
---|
71 | return -1; |
---|
72 | break; |
---|
73 | default: |
---|
74 | return -1; |
---|
75 | } |
---|
76 | break; |
---|
77 | #endif |
---|
78 | #ifdef ROAR_HAVE_LIBDNET |
---|
79 | case AF_DECnet: |
---|
80 | len = sizeof(struct sockaddr_dn); |
---|
81 | |
---|
82 | return -1; |
---|
83 | break; |
---|
84 | #endif |
---|
85 | #ifdef ROAR_HAVE_IPV6 |
---|
86 | case AF_INET6: |
---|
87 | len = sizeof(struct sockaddr_in6); |
---|
88 | |
---|
89 | switch (def->d.socket.type) { |
---|
90 | case SOCK_STREAM: |
---|
91 | fh = roar_socket_new_tcp6(); |
---|
92 | break; |
---|
93 | case SOCK_DGRAM: |
---|
94 | fh = roar_socket_new_udp6(); |
---|
95 | break; |
---|
96 | default: |
---|
97 | return -1; |
---|
98 | } |
---|
99 | break; |
---|
100 | #endif |
---|
101 | #ifdef ROAR_HAVE_IPX |
---|
102 | case AF_IPX: |
---|
103 | len = sizeof(struct sockaddr_ipx); |
---|
104 | |
---|
105 | return -1; |
---|
106 | break; |
---|
107 | #endif |
---|
108 | default: |
---|
109 | return -1; |
---|
110 | } |
---|
111 | |
---|
112 | if ( fh == -1 ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | if ( connect(fh, &(def->d.socket.sa.sa), len) == -1 ) { |
---|
116 | close(fh); |
---|
117 | return -1; |
---|
118 | } |
---|
119 | |
---|
120 | if ( roar_vio_open_fh_socket(calls, fh) == -1 ) { |
---|
121 | close(fh); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | int roar_vio_socket_init_socket_def (struct roar_vio_defaults * def, int domain, int type) { |
---|
129 | if ( def == NULL || domain == -1 || type == -1 ) |
---|
130 | return -1; |
---|
131 | |
---|
132 | // we do not memset(def, 0, sizeof(...)) here |
---|
133 | // because this is allready done in roar_vio_dstr_init_defaults() |
---|
134 | // if we would be would override o_flags/o_mode and maybe others |
---|
135 | |
---|
136 | memset(&(def->d.socket.sa), 0, sizeof(def->d.socket.sa)); |
---|
137 | |
---|
138 | def->type = ROAR_VIO_DEF_TYPE_SOCKET; |
---|
139 | def->d.socket.domain = domain; |
---|
140 | def->d.socket.type = type; |
---|
141 | def->d.socket.sa.sa.sa_family = domain; |
---|
142 | |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | int roar_vio_socket_init_unix_def (struct roar_vio_defaults * def, char * path) { |
---|
147 | if ( roar_vio_socket_init_socket_def(def, AF_UNIX, SOCK_STREAM) == -1 ) |
---|
148 | return -1; |
---|
149 | |
---|
150 | strncpy(def->d.socket.sa.un.sun_path, path, sizeof(def->d.socket.sa.un.sun_path) - 1); |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | int roar_vio_socket_init_decnet_def (struct roar_vio_defaults * def, char * node, int object, char * objname); |
---|
156 | |
---|
157 | int roar_vio_socket_init_inet4host_def(struct roar_vio_defaults * def, char * host) { |
---|
158 | struct hostent * he; |
---|
159 | |
---|
160 | if ( def == NULL || host == NULL ) |
---|
161 | return -1; |
---|
162 | |
---|
163 | if ( (he = gethostbyname(host)) == NULL ) { |
---|
164 | ROAR_ERR("roar_vio_socket_init_inet4host_def(*): Can\'t resolve host name '%s'", |
---|
165 | host); |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | memcpy((struct in_addr *)&def->d.socket.sa.in.sin_addr, he->h_addr, sizeof(struct in_addr)); |
---|
170 | |
---|
171 | return 0; |
---|
172 | } |
---|
173 | |
---|
174 | int roar_vio_socket_init_tcp4_def (struct roar_vio_defaults * def, char * host, int port) { |
---|
175 | if ( roar_vio_socket_init_socket_def(def, AF_INET, SOCK_STREAM) == -1 ) |
---|
176 | return -1; |
---|
177 | |
---|
178 | if ( roar_vio_socket_init_inet4host_def(def, host) == -1 ) |
---|
179 | return -1; |
---|
180 | |
---|
181 | def->d.socket.sa.in.sin_port = ROAR_HOST2NET16(port); |
---|
182 | |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | int roar_vio_socket_init_udp4_def (struct roar_vio_defaults * def, char * host, int port); |
---|
187 | int roar_vio_socket_init_inet6host_def(struct roar_vio_defaults * def, char * host); |
---|
188 | int roar_vio_socket_init_tcp6_def (struct roar_vio_defaults * def, char * host, int port); |
---|
189 | int roar_vio_socket_init_udp6_def (struct roar_vio_defaults * def, char * host, int port); |
---|
190 | |
---|
191 | //ll |
---|