[1331] | 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 | |
---|
[1333] | 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) { |
---|
[1358] | 48 | #ifdef ROAR_HAVE_IPV4 |
---|
[1333] | 49 | case AF_INET: |
---|
| 50 | len = sizeof(struct sockaddr_in); |
---|
| 51 | |
---|
[1335] | 52 | if ( roar_vio_socket_init_inet4host_def(def) == -1 ) |
---|
| 53 | return -1; |
---|
| 54 | |
---|
[1333] | 55 | switch (def->d.socket.type) { |
---|
| 56 | case SOCK_STREAM: |
---|
| 57 | fh = roar_socket_new_tcp(); |
---|
| 58 | break; |
---|
| 59 | case SOCK_DGRAM: |
---|
| 60 | fh = roar_socket_new_udp(); |
---|
| 61 | break; |
---|
| 62 | default: |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
| 65 | break; |
---|
[1358] | 66 | #endif |
---|
[1333] | 67 | #ifdef ROAR_HAVE_UNIX |
---|
| 68 | case AF_UNIX: |
---|
| 69 | len = sizeof(struct sockaddr_un); |
---|
| 70 | |
---|
| 71 | switch (def->d.socket.type) { |
---|
| 72 | case SOCK_STREAM: |
---|
| 73 | fh = roar_socket_new_unix(); |
---|
| 74 | break; |
---|
| 75 | case SOCK_DGRAM: |
---|
| 76 | return -1; |
---|
| 77 | break; |
---|
| 78 | default: |
---|
| 79 | return -1; |
---|
| 80 | } |
---|
| 81 | break; |
---|
| 82 | #endif |
---|
| 83 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 84 | case AF_DECnet: |
---|
| 85 | len = sizeof(struct sockaddr_dn); |
---|
| 86 | |
---|
[1354] | 87 | if ( roar_vio_socket_init_decnetnode_def(def) == -1 ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
| 90 | switch (def->d.socket.type) { |
---|
| 91 | case SOCK_STREAM: |
---|
| 92 | fh = roar_socket_new_decnet_stream(); |
---|
| 93 | break; |
---|
| 94 | default: |
---|
| 95 | return -1; |
---|
| 96 | } |
---|
[1333] | 97 | break; |
---|
| 98 | #endif |
---|
| 99 | #ifdef ROAR_HAVE_IPV6 |
---|
| 100 | case AF_INET6: |
---|
| 101 | len = sizeof(struct sockaddr_in6); |
---|
[1332] | 102 | |
---|
[1333] | 103 | switch (def->d.socket.type) { |
---|
| 104 | case SOCK_STREAM: |
---|
| 105 | fh = roar_socket_new_tcp6(); |
---|
| 106 | break; |
---|
| 107 | case SOCK_DGRAM: |
---|
| 108 | fh = roar_socket_new_udp6(); |
---|
| 109 | break; |
---|
| 110 | default: |
---|
| 111 | return -1; |
---|
| 112 | } |
---|
| 113 | break; |
---|
| 114 | #endif |
---|
| 115 | #ifdef ROAR_HAVE_IPX |
---|
| 116 | case AF_IPX: |
---|
| 117 | len = sizeof(struct sockaddr_ipx); |
---|
| 118 | |
---|
| 119 | return -1; |
---|
| 120 | break; |
---|
| 121 | #endif |
---|
| 122 | default: |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | if ( fh == -1 ) |
---|
| 127 | return -1; |
---|
| 128 | |
---|
| 129 | if ( connect(fh, &(def->d.socket.sa.sa), len) == -1 ) { |
---|
| 130 | close(fh); |
---|
| 131 | return -1; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if ( roar_vio_open_fh_socket(calls, fh) == -1 ) { |
---|
| 135 | close(fh); |
---|
| 136 | return -1; |
---|
| 137 | } |
---|
| 138 | |
---|
[1338] | 139 | // there is no problem if the shutdown()s fail. |
---|
| 140 | // some socket domains don't support unidirectional connections |
---|
| 141 | // this just free()s some kernel buffers :) |
---|
| 142 | switch (def->o_flags & (O_RDONLY|O_WRONLY|O_RDWR)) { |
---|
| 143 | case O_RDONLY: |
---|
| 144 | shutdown(fh, SHUT_WR); |
---|
| 145 | break; |
---|
| 146 | case O_WRONLY: |
---|
| 147 | shutdown(fh, SHUT_RD); |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | |
---|
[1333] | 151 | return 0; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | int roar_vio_socket_init_socket_def (struct roar_vio_defaults * def, int domain, int type) { |
---|
| 155 | if ( def == NULL || domain == -1 || type == -1 ) |
---|
| 156 | return -1; |
---|
| 157 | |
---|
| 158 | // we do not memset(def, 0, sizeof(...)) here |
---|
| 159 | // because this is allready done in roar_vio_dstr_init_defaults() |
---|
| 160 | // if we would be would override o_flags/o_mode and maybe others |
---|
| 161 | |
---|
| 162 | memset(&(def->d.socket.sa), 0, sizeof(def->d.socket.sa)); |
---|
| 163 | |
---|
| 164 | def->type = ROAR_VIO_DEF_TYPE_SOCKET; |
---|
| 165 | def->d.socket.domain = domain; |
---|
| 166 | def->d.socket.type = type; |
---|
| 167 | def->d.socket.sa.sa.sa_family = domain; |
---|
| 168 | |
---|
| 169 | return 0; |
---|
| 170 | } |
---|
| 171 | |
---|
[1335] | 172 | int roar_vio_socket_init_dstr_def (struct roar_vio_defaults * def, char * dstr, int hint, int type, |
---|
| 173 | struct roar_vio_defaults * odef) { |
---|
| 174 | char * host; |
---|
[1358] | 175 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) |
---|
[1335] | 176 | int port; |
---|
[1358] | 177 | #endif |
---|
[1335] | 178 | |
---|
[1337] | 179 | if ( def == NULL ) |
---|
[1335] | 180 | return -1; |
---|
| 181 | |
---|
[1337] | 182 | if ( dstr == NULL && odef == NULL ) |
---|
| 183 | return -1; |
---|
| 184 | |
---|
| 185 | if ( dstr == NULL ) |
---|
| 186 | dstr = ""; |
---|
| 187 | |
---|
[1335] | 188 | ROAR_WARN("roar_vio_socket_init_dstr_def(def=%p, dstr='%s', hint=%i, type=%i, odef=%p) = ?", def, dstr, hint, type, odef); |
---|
| 189 | |
---|
| 190 | if ( hint == -1 ) { |
---|
| 191 | if ( 0 ) { // this is needed to keep the syntx ok, compiler will throw it away |
---|
| 192 | #ifdef ROAR_HAVE_IPV6 |
---|
| 193 | } else if ( strstr(dstr, "[") != NULL ) { // [ip]:service |
---|
| 194 | hint = AF_INET6; |
---|
| 195 | #endif |
---|
| 196 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 197 | } else if ( strstr(dstr, "::") != NULL ) { // node::object |
---|
| 198 | hint = AF_DECnet; |
---|
| 199 | #endif |
---|
| 200 | #ifdef ROAR_HAVE_IPX |
---|
| 201 | } else if ( strstr(dstr, "(") != NULL ) { // net:mac(service) |
---|
| 202 | hint = AF_IPX; |
---|
| 203 | #endif |
---|
| 204 | #ifdef ROAR_HAVE_UNIX |
---|
| 205 | } else if ( strstr(dstr, "/") != NULL ) { // /path/to/sock |
---|
| 206 | hint = AF_UNIX; |
---|
| 207 | #endif |
---|
[1358] | 208 | #ifdef ROAR_HAVE_IPV4 |
---|
[1335] | 209 | } else if ( strstr(dstr, ":") != NULL ) { // host:port |
---|
| 210 | hint = AF_INET; |
---|
[1358] | 211 | #endif |
---|
[1335] | 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | if ( hint == -1 && odef != NULL ) { // if we still don't know what this is we try |
---|
| 216 | // to use the parent objects request |
---|
[1350] | 217 | ROAR_DBG("roar_vio_socket_init_dstr_def(*): hint=-1 && odef!=NULL"); |
---|
[1335] | 218 | if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
[1350] | 219 | ROAR_DBG("roar_vio_socket_init_dstr_def(*): hint=-1 && odef!=NULL, using hint from odef"); |
---|
[1335] | 220 | hint = odef->d.socket.domain; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | if ( hint == -1 ) /* we really have no glue what this is... */ |
---|
| 225 | return -1; |
---|
| 226 | |
---|
| 227 | #ifdef ROAR_HAVE_UNIX |
---|
| 228 | if ( hint == AF_UNIX ) { |
---|
| 229 | if ( *dstr != 0 && strcmp(dstr, "//") != 0 ) { |
---|
| 230 | return roar_vio_socket_init_unix_def(def, dstr); |
---|
| 231 | } else { |
---|
| 232 | if ( roar_vio_socket_conv_def(odef, AF_UNIX) == -1 ) |
---|
| 233 | return -1; |
---|
| 234 | |
---|
| 235 | return roar_vio_socket_init_unix_def(def, odef->d.socket.sa.un.sun_path); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | #endif |
---|
| 239 | |
---|
[1354] | 240 | ROAR_DBG("roar_vio_socket_init_dstr_def(*) = ?"); |
---|
| 241 | |
---|
[1337] | 242 | if ( *dstr == 0 ) { |
---|
| 243 | if ( roar_vio_socket_conv_def(odef, hint) == -1 ) |
---|
| 244 | return -1; |
---|
| 245 | |
---|
| 246 | if ( odef->d.socket.type != type ) |
---|
| 247 | return -1; |
---|
| 248 | |
---|
[1353] | 249 | if ( def != odef ) |
---|
| 250 | memcpy(def, odef, sizeof(struct roar_vio_defaults)); |
---|
| 251 | |
---|
[1337] | 252 | return 0; |
---|
| 253 | } |
---|
| 254 | |
---|
[1335] | 255 | for (; *dstr == '/'; dstr++); |
---|
| 256 | |
---|
[1354] | 257 | ROAR_DBG("roar_vio_socket_init_dstr_def(*) = ?"); |
---|
| 258 | |
---|
[1335] | 259 | switch (hint) { |
---|
[1358] | 260 | #ifdef ROAR_HAVE_IPV4 |
---|
[1335] | 261 | case AF_INET: |
---|
| 262 | host = dstr; |
---|
| 263 | for (; *dstr != 0 && *dstr != ':'; dstr++); |
---|
| 264 | |
---|
| 265 | if ( *dstr == ':' ) { // we have a port :) |
---|
| 266 | *dstr++ = 0; |
---|
| 267 | if ( (port = roar_vio_socket_get_port(dstr, AF_INET, type)) == -1 ) |
---|
| 268 | return -1; |
---|
| 269 | |
---|
| 270 | return roar_vio_socket_init_inet4_def(def, host, port, type); |
---|
| 271 | } else { |
---|
| 272 | if ( roar_vio_socket_conv_def(odef, AF_INET) == -1 ) |
---|
| 273 | return -1; |
---|
| 274 | |
---|
| 275 | return roar_vio_socket_init_inet4_def(def, host, ROAR_NET2HOST16(odef->d.socket.sa.in.sin_port), type); |
---|
| 276 | } |
---|
| 277 | break; |
---|
[1358] | 278 | #endif |
---|
[1335] | 279 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 280 | case AF_DECnet: |
---|
[1354] | 281 | ROAR_DBG("roar_vio_socket_init_dstr_def(*) = ?"); |
---|
| 282 | host = dstr; |
---|
| 283 | |
---|
| 284 | if ( type != SOCK_STREAM ) |
---|
| 285 | return -1; |
---|
| 286 | |
---|
| 287 | if ( (dstr = strstr(dstr, "::")) == NULL ) { |
---|
| 288 | if ( roar_vio_socket_conv_def(odef, AF_DECnet) == -1 ) |
---|
| 289 | return -1; |
---|
| 290 | |
---|
| 291 | return -1; |
---|
| 292 | // return roar_vio_socket_init_decnet_def(def, host, -1, dstr); |
---|
| 293 | } else { |
---|
| 294 | *dstr = 0; |
---|
| 295 | dstr += 2; |
---|
| 296 | return roar_vio_socket_init_decnet_def(def, host, -1, dstr); |
---|
| 297 | } |
---|
[1335] | 298 | break; |
---|
| 299 | #endif |
---|
| 300 | #ifdef ROAR_HAVE_IPV6 |
---|
| 301 | case AF_INET6: |
---|
| 302 | return -1; |
---|
| 303 | break; |
---|
| 304 | #endif |
---|
| 305 | #ifdef ROAR_HAVE_IPX |
---|
| 306 | case AF_IPX: |
---|
| 307 | return -1; |
---|
| 308 | break; |
---|
| 309 | #endif |
---|
| 310 | default: |
---|
| 311 | return -1; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | return 0; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | int roar_vio_socket_conv_def (struct roar_vio_defaults * def, int domain) { |
---|
[1337] | 318 | if ( def == NULL || domain == -1 ) |
---|
| 319 | return -1; |
---|
| 320 | |
---|
| 321 | #ifdef ROAR_HAVE_UNIX |
---|
| 322 | if ( domain == AF_UNIX ) { |
---|
| 323 | if ( def->type == ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
| 324 | if ( def->d.socket.domain == AF_UNIX ) |
---|
| 325 | return 0; |
---|
| 326 | |
---|
| 327 | return -1; |
---|
| 328 | } else { |
---|
[1338] | 329 | if ( def->type == ROAR_VIO_DEF_TYPE_FILE ) |
---|
| 330 | return roar_vio_socket_init_unix_def(def, def->d.file); |
---|
| 331 | |
---|
[1337] | 332 | return -1; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | #endif |
---|
| 336 | |
---|
| 337 | if ( def->type != ROAR_VIO_DEF_TYPE_SOCKET ) |
---|
| 338 | return -1; |
---|
| 339 | |
---|
| 340 | if ( def->d.socket.domain == domain ) |
---|
| 341 | return 0; |
---|
| 342 | |
---|
[1339] | 343 | // we sould add support to convert IPv4 <-> IPv6 here |
---|
| 344 | |
---|
[1335] | 345 | return -1; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | int roar_vio_socket_get_port (char * service, int domain, int type) { |
---|
[1341] | 349 | struct servent * serv = NULL; |
---|
| 350 | char * proto = NULL; |
---|
| 351 | int port; |
---|
[1348] | 352 | char * ts; |
---|
[1341] | 353 | |
---|
[1335] | 354 | if ( service == NULL || domain == -1 || type == -1 ) |
---|
| 355 | return -1; |
---|
| 356 | |
---|
[1348] | 357 | if ( (ts = strstr(service, "/")) != NULL ) |
---|
| 358 | *ts = 0; |
---|
[1342] | 359 | |
---|
[1341] | 360 | if ( sscanf(service, "%i", &port) == 1 ) |
---|
| 361 | return port; |
---|
| 362 | |
---|
[1348] | 363 | if ( ts != NULL ) |
---|
| 364 | *ts = '/'; |
---|
| 365 | |
---|
[1341] | 366 | switch (domain) { |
---|
| 367 | #ifdef ROAR_HAVE_IPV6 |
---|
| 368 | case AF_INET6: |
---|
| 369 | #endif |
---|
[1358] | 370 | #ifdef ROAR_HAVE_IPV4 |
---|
[1341] | 371 | case AF_INET: |
---|
[1358] | 372 | #endif |
---|
| 373 | #if defined(ROAR_HAVE_IPV6) || defined(ROAR_HAVE_IPV4) |
---|
[1341] | 374 | switch (type) { |
---|
| 375 | case SOCK_STREAM: proto = "tcp"; break; |
---|
| 376 | case SOCK_DGRAM: proto = "udp"; break; |
---|
| 377 | default: |
---|
| 378 | return -1; |
---|
| 379 | } |
---|
| 380 | break; |
---|
[1358] | 381 | #endif |
---|
[1341] | 382 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 383 | case AF_DECnet: |
---|
| 384 | #ifdef ROAR_HAVE_GETOBJECTBYNAME |
---|
| 385 | return getobjectbyname(service); |
---|
| 386 | #else |
---|
| 387 | if ( !strcmp(service, "roar") ) |
---|
| 388 | return 0; |
---|
| 389 | |
---|
| 390 | return -1; |
---|
| 391 | #endif |
---|
| 392 | break; |
---|
| 393 | #endif |
---|
| 394 | default: |
---|
| 395 | return -1; |
---|
| 396 | } |
---|
| 397 | |
---|
[1348] | 398 | if ( ts != NULL ) |
---|
| 399 | *ts = 0; |
---|
| 400 | |
---|
[1341] | 401 | if ( (serv = getservbyname(service, proto)) == NULL ) { |
---|
| 402 | ROAR_ERR("roar_vio_socket_get_port(*): Unknown service: %s/%s: %s", service, proto, strerror(errno)); |
---|
[1348] | 403 | |
---|
| 404 | if ( ts != NULL ) |
---|
| 405 | *ts = '/'; |
---|
| 406 | |
---|
[1341] | 407 | return -1; |
---|
| 408 | } |
---|
| 409 | |
---|
[1348] | 410 | if ( ts != NULL ) |
---|
| 411 | *ts = '/'; |
---|
| 412 | |
---|
[1342] | 413 | return ROAR_NET2HOST16(serv->s_port); |
---|
[1335] | 414 | } |
---|
| 415 | |
---|
| 416 | // AF_UNIX: |
---|
[1333] | 417 | int roar_vio_socket_init_unix_def (struct roar_vio_defaults * def, char * path) { |
---|
[1335] | 418 | #ifdef ROAR_HAVE_UNIX |
---|
[1337] | 419 | if ( def == NULL || path == NULL ) |
---|
| 420 | return -1; |
---|
| 421 | |
---|
[1333] | 422 | if ( roar_vio_socket_init_socket_def(def, AF_UNIX, SOCK_STREAM) == -1 ) |
---|
| 423 | return -1; |
---|
| 424 | |
---|
| 425 | strncpy(def->d.socket.sa.un.sun_path, path, sizeof(def->d.socket.sa.un.sun_path) - 1); |
---|
| 426 | |
---|
| 427 | return 0; |
---|
[1335] | 428 | #else |
---|
| 429 | return -1; |
---|
| 430 | #endif |
---|
[1333] | 431 | } |
---|
| 432 | |
---|
[1335] | 433 | // AF_DECnet: |
---|
[1354] | 434 | int roar_vio_socket_init_decnetnode_def(struct roar_vio_defaults * def) { |
---|
[1355] | 435 | #ifdef ROAR_HAVE_LIBDNET |
---|
[1354] | 436 | char * node; |
---|
| 437 | char * ed; |
---|
| 438 | struct nodeent * ne; |
---|
| 439 | |
---|
| 440 | if ( def == NULL ) |
---|
| 441 | return -1; |
---|
| 442 | |
---|
| 443 | if ( (node = def->d.socket.host) == NULL ) |
---|
| 444 | return -1; |
---|
| 445 | |
---|
| 446 | if ( (ed = strstr(node, "/")) != NULL ) |
---|
| 447 | *ed = 0; |
---|
| 448 | |
---|
| 449 | if ( (ne = getnodebyname(node)) == NULL ) { |
---|
| 450 | ROAR_ERR("roar_vio_socket_init_decnetnode_def(*): Can\'t resolve node name '%s'", node); |
---|
| 451 | if ( ed != NULL ) *ed = '/'; |
---|
| 452 | return -1; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | memcpy(&(def->d.socket.sa.dn.sdn_add.a_addr), ne->n_addr, 2); |
---|
| 456 | |
---|
| 457 | if ( ed != NULL ) *ed = '/'; |
---|
| 458 | |
---|
| 459 | |
---|
| 460 | return 0; |
---|
[1355] | 461 | #else |
---|
| 462 | return -1; |
---|
| 463 | #endif |
---|
[1354] | 464 | } |
---|
| 465 | |
---|
[1345] | 466 | int roar_vio_socket_init_decnet_def (struct roar_vio_defaults * def, char * node, int object, char * objname) { |
---|
| 467 | #ifdef ROAR_HAVE_LIBDNET |
---|
[1354] | 468 | struct sockaddr_dn * dn; |
---|
| 469 | |
---|
[1345] | 470 | if ( def == NULL ) |
---|
| 471 | return -1; |
---|
| 472 | |
---|
| 473 | if ( object < 1 && objname == NULL ) |
---|
| 474 | return -1; |
---|
| 475 | |
---|
| 476 | if ( object == -1 ) |
---|
[1348] | 477 | object = roar_vio_socket_get_port(objname, AF_DECnet, SOCK_STREAM); |
---|
[1345] | 478 | |
---|
[1354] | 479 | if ( object == -1 ) { |
---|
| 480 | if ( objname == NULL ) { |
---|
| 481 | return -1; |
---|
| 482 | } else { |
---|
| 483 | object = 0; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | if ( roar_vio_socket_init_socket_def(def, AF_DECnet, SOCK_STREAM) == -1 ) |
---|
[1345] | 488 | return -1; |
---|
| 489 | |
---|
[1354] | 490 | def->d.socket.host = node; |
---|
| 491 | dn = &(def->d.socket.sa.dn); |
---|
| 492 | dn->sdn_flags = 0; |
---|
| 493 | dn->sdn_objnum = object; |
---|
| 494 | dn->sdn_nodeaddrl = 2; |
---|
| 495 | |
---|
| 496 | if ( objname == NULL ) { |
---|
| 497 | dn->sdn_objnamel = 0; |
---|
| 498 | } else { |
---|
| 499 | dn->sdn_objnamel = strlen(objname); |
---|
| 500 | if ( dn->sdn_objnamel > DN_MAXOBJL ) |
---|
| 501 | dn->sdn_objnamel = DN_MAXOBJL; |
---|
| 502 | |
---|
| 503 | memcpy(&(dn->sdn_objname), objname, dn->sdn_objnamel); |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | return 0; |
---|
[1345] | 507 | #else |
---|
| 508 | return -1; |
---|
| 509 | #endif |
---|
| 510 | } |
---|
[1332] | 511 | |
---|
[1335] | 512 | |
---|
| 513 | // AF_INET: |
---|
| 514 | int roar_vio_socket_init_inet4host_def(struct roar_vio_defaults * def) { |
---|
[1358] | 515 | #ifdef ROAR_HAVE_IPV4 |
---|
[1333] | 516 | struct hostent * he; |
---|
[1349] | 517 | char * ed; |
---|
[1333] | 518 | |
---|
[1335] | 519 | if ( def == NULL ) |
---|
[1333] | 520 | return -1; |
---|
| 521 | |
---|
[1335] | 522 | if ( def->d.socket.host == NULL ) |
---|
| 523 | return -1; |
---|
| 524 | |
---|
[1349] | 525 | if ( (ed = strstr(def->d.socket.host, "/")) != NULL ) |
---|
| 526 | *ed = 0; |
---|
| 527 | |
---|
[1335] | 528 | if ( (he = gethostbyname(def->d.socket.host)) == NULL ) { |
---|
[1333] | 529 | ROAR_ERR("roar_vio_socket_init_inet4host_def(*): Can\'t resolve host name '%s'", |
---|
[1335] | 530 | def->d.socket.host); |
---|
[1349] | 531 | if ( ed != NULL ) *ed = '/'; |
---|
[1333] | 532 | return -1; |
---|
| 533 | } |
---|
| 534 | |
---|
[1349] | 535 | if ( ed != NULL ) *ed = '/'; |
---|
| 536 | |
---|
[1333] | 537 | memcpy((struct in_addr *)&def->d.socket.sa.in.sin_addr, he->h_addr, sizeof(struct in_addr)); |
---|
| 538 | |
---|
| 539 | return 0; |
---|
[1358] | 540 | #else |
---|
| 541 | return -1; |
---|
| 542 | #endif |
---|
[1333] | 543 | } |
---|
| 544 | |
---|
[1335] | 545 | int roar_vio_socket_init_inet4_def (struct roar_vio_defaults * def, char * host, int port, int type) { |
---|
[1358] | 546 | #ifdef ROAR_HAVE_IPV4 |
---|
[1335] | 547 | if ( roar_vio_socket_init_socket_def(def, AF_INET, type) == -1 ) |
---|
[1333] | 548 | return -1; |
---|
| 549 | |
---|
[1335] | 550 | def->d.socket.host = host; |
---|
[1333] | 551 | |
---|
| 552 | def->d.socket.sa.in.sin_port = ROAR_HOST2NET16(port); |
---|
| 553 | |
---|
| 554 | return 0; |
---|
[1358] | 555 | #else |
---|
| 556 | return -1; |
---|
| 557 | #endif |
---|
[1333] | 558 | } |
---|
| 559 | |
---|
[1335] | 560 | int roar_vio_socket_init_tcp4_def (struct roar_vio_defaults * def, char * host, int port) { |
---|
| 561 | return roar_vio_socket_init_inet4_def(def, host, port, SOCK_STREAM); |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | int roar_vio_socket_init_udp4_def (struct roar_vio_defaults * def, char * host, int port) { |
---|
| 565 | return roar_vio_socket_init_inet4_def(def, host, port, SOCK_DGRAM); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | |
---|
| 569 | // AF_INET6: |
---|
| 570 | int roar_vio_socket_init_inet6host_def(struct roar_vio_defaults * def); |
---|
| 571 | int roar_vio_socket_init_inet6_def (struct roar_vio_defaults * def, char * host, int port, int type) { |
---|
| 572 | return -1; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | int roar_vio_socket_init_tcp6_def (struct roar_vio_defaults * def, char * host, int port) { |
---|
| 576 | return roar_vio_socket_init_inet6_def(def, host, port, SOCK_STREAM); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | int roar_vio_socket_init_udp6_def (struct roar_vio_defaults * def, char * host, int port) { |
---|
| 580 | return roar_vio_socket_init_inet6_def(def, host, port, SOCK_DGRAM); |
---|
| 581 | } |
---|
[1332] | 582 | |
---|
[1331] | 583 | //ll |
---|