]> arthur.barton.de Git - netatalk.git/blob - libatalk/netddp/netddp_open.c
Trunk-BP: NODDP guard, NetBSD compat fixes.
[netatalk.git] / libatalk / netddp / netddp_open.c
1 /* 
2  * $Id: netddp_open.c,v 1.3.2.3 2002-02-08 00:05:20 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
34 #ifdef NO_DDP
35     return -1;
36 #else /* !NO_DDP */
37
38     int s;
39
40 #ifdef MACOSX_SERVER
41     at_inet_t address, baddress;
42
43     if ((s = ddp_open(addr ? &addr->sat_port : NULL)) < 0)
44         return -1;
45
46     if (!addr)
47       return s;
48
49     if (rtmp_netinfo(s, &address, &baddress) < 0) {
50         ddp_close(s);
51         return -1;
52     }
53     
54     memcpy(&addr->sat_addr.s_net, &address.net, sizeof(addr->sat_addr.s_net));
55     addr->sat_addr.s_node = address.node;
56     addr->sat_port = address.socket;
57     if (bridge) {
58       memcpy(&bridge->sat_addr.s_net, &baddress.net, 
59              sizeof(bridge->sat_addr.s_net));
60       bridge->sat_addr.s_node = baddress.node;
61       bridge->sat_port = baddress.socket;
62     }
63 #else /* MACOSX_SERVER */
64     size_t len;
65
66     if ((s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0) 
67         return -1;
68     
69     if (!addr)
70         return s;
71
72     addr->sat_family = AF_APPLETALK;
73     /* rest of address should be initialized by the caller */
74     if (bind(s, (struct sockaddr *) addr, sizeof( struct sockaddr_at )) < 0 ) {
75         close(s);
76         return -1;
77     }
78
79     /* get the real address from the kernel */
80     len = sizeof( struct sockaddr_at);
81     if ( getsockname( s, (struct sockaddr *) addr, &len ) != 0 ) {
82         close(s);
83         return -1;
84     }
85 #endif /* MACOSX_SERVER */
86
87     return s;
88 #endif /* NO_DDP */
89 }