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