]> arthur.barton.de Git - netatalk.git/blob - libatalk/netddp/netddp_open.c
Forward-Ports from the stabilizing branch.
[netatalk.git] / libatalk / netddp / netddp_open.c
1 /* 
2  * $Id: netddp_open.c,v 1.4 2001-11-25 21:55:10 srittau Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5  * Copyright (c) 1990,1991 Regents of The University of Michigan.
6  * All Rights Reserved. See COPYRIGHT.
7  *
8  * open a ddp socket and return the port and address assigned. return
9  * various address info if requested as well.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 static int _netddp_open_dummy;
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22
23 #ifdef MACOSX_SERVER
24 #include <netat/appletalk.h>
25 #include <netat/ddp.h>
26 #endif /* MACOSX_SERVER */
27
28 #include <netatalk/at.h>
29 #include <atalk/netddp.h>
30
31 int netddp_open(struct sockaddr_at *addr, struct sockaddr_at *bridge)
32 {
33     int s;
34
35 #ifdef MACOSX_SERVER
36     at_inet_t address, baddress;
37
38     if ((s = ddp_open(addr ? &addr->sat_port : NULL)) < 0)
39         return -1;
40
41     if (!addr)
42       return s;
43
44     if (rtmp_netinfo(s, &address, &baddress) < 0) {
45         ddp_close(s);
46         return -1;
47     }
48     
49     memcpy(&addr->sat_addr.s_net, &address.net, sizeof(addr->sat_addr.s_net));
50     addr->sat_addr.s_node = address.node;
51     addr->sat_port = address.socket;
52     if (bridge) {
53       memcpy(&bridge->sat_addr.s_net, &baddress.net, 
54              sizeof(bridge->sat_addr.s_net));
55       bridge->sat_addr.s_node = baddress.node;
56       bridge->sat_port = baddress.socket;
57     }
58 #else /* MACOSX_SERVER */
59     int len;
60
61     if ((s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0) 
62         return -1;
63     
64     if (!addr)
65         return s;
66
67     addr->sat_family = AF_APPLETALK;
68     /* rest of address should be initialized by the caller */
69     if (bind(s, (struct sockaddr *) addr, sizeof( struct sockaddr_at )) < 0 ) {
70         close(s);
71         return -1;
72     }
73
74     /* get the real address from the kernel */
75     len = sizeof( struct sockaddr_at);
76     if ( getsockname( s, (struct sockaddr *) addr, &len ) != 0 ) {
77         close(s);
78         return -1;
79     }
80 #endif /* MACOSX_SERVER */
81
82     return s;
83 }