]> arthur.barton.de Git - netatalk.git/blob - libatalk/netddp/netddp_open.c
Big configure.in cleanup
[netatalk.git] / libatalk / netddp / netddp_open.c
1 /* 
2  * $Id: netddp_open.c,v 1.9 2005-04-28 20:50:02 bfernhomberg 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 #include <stdio.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20
21 #ifdef MACOSX_SERVER
22 #include <netat/appletalk.h>
23 #include <netat/ddp.h>
24 #endif /* MACOSX_SERVER */
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
32 #ifdef NO_DDP
33     return -1;
34 #else /* !NO_DDP */
35
36     int s;
37
38 #ifdef MACOSX_SERVER
39     at_inet_t address, baddress;
40
41     if ((s = ddp_open(addr ? &addr->sat_port : NULL)) < 0)
42         return -1;
43
44     if (!addr)
45       return s;
46
47     if (rtmp_netinfo(s, &address, &baddress) < 0) {
48         ddp_close(s);
49         return -1;
50     }
51     
52     memcpy(&addr->sat_addr.s_net, &address.net, sizeof(addr->sat_addr.s_net));
53     addr->sat_addr.s_node = address.node;
54     addr->sat_port = address.socket;
55     if (bridge) {
56       memcpy(&bridge->sat_addr.s_net, &baddress.net, 
57              sizeof(bridge->sat_addr.s_net));
58       bridge->sat_addr.s_node = baddress.node;
59       bridge->sat_port = baddress.socket;
60     }
61 #else /* MACOSX_SERVER */
62     socklen_t len;
63
64     if ((s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0) 
65         return -1;
66     
67     if (!addr)
68         return s;
69
70     addr->sat_family = AF_APPLETALK;
71     /* rest of address should be initialized by the caller */
72     if (bind(s, (struct sockaddr *) addr, sizeof( struct sockaddr_at )) < 0 ) {
73         close(s);
74         return -1;
75     }
76
77     /* get the real address from the kernel */
78     len = sizeof( struct sockaddr_at);
79     if ( getsockname( s, (struct sockaddr *) addr, &len ) != 0 ) {
80         close(s);
81         return -1;
82     }
83 #endif /* MACOSX_SERVER */
84
85     return s;
86 #endif /* NO_DDP */
87 }