2086
|
1 /* This file is part of the Project Athena Zephyr Notification System.
|
|
2 * It contains source for the ZOpenPort function.
|
|
3 *
|
|
4 * Created by: Robert French
|
|
5 *
|
|
6 * $Source$
|
|
7 * $Author: warmenhoven $
|
|
8 *
|
|
9 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
|
|
10 * For copying and distribution information, see the file
|
|
11 * "mit-copyright.h".
|
|
12 */
|
|
13 /* $Header$ */
|
|
14
|
|
15 #ifndef lint
|
|
16 static char rcsid_ZOpenPort_c[] = "$Header$";
|
|
17 #endif
|
|
18
|
|
19 #include <internal.h>
|
|
20 #include <sys/socket.h>
|
|
21
|
|
22 Code_t ZOpenPort(port)
|
|
23 u_short *port;
|
|
24 {
|
|
25 struct sockaddr_in bindin;
|
|
26 int len;
|
|
27
|
|
28 (void) ZClosePort();
|
|
29
|
|
30 if ((__Zephyr_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
31 __Zephyr_fd = -1;
|
|
32 return (errno);
|
|
33 }
|
|
34
|
|
35 #ifdef SO_BSDCOMPAT
|
|
36 {
|
|
37 int on = 1;
|
|
38
|
|
39 setsockopt(__Zephyr_fd, SOL_SOCKET, SO_BSDCOMPAT, (char *)&on,
|
|
40 sizeof(on));
|
|
41 }
|
|
42 #endif
|
|
43
|
|
44 bindin.sin_family = AF_INET;
|
|
45
|
|
46 if (port && *port)
|
|
47 bindin.sin_port = *port;
|
|
48 else
|
|
49 bindin.sin_port = 0;
|
|
50
|
|
51 bindin.sin_addr.s_addr = INADDR_ANY;
|
|
52
|
|
53 if (bind(__Zephyr_fd, (struct sockaddr *)&bindin, sizeof(bindin)) < 0) {
|
|
54 if (errno == EADDRINUSE && port && *port)
|
|
55 return (ZERR_PORTINUSE);
|
|
56 else
|
|
57 return (errno);
|
|
58 }
|
|
59
|
|
60 if (!bindin.sin_port) {
|
|
61 len = sizeof(bindin);
|
|
62 if (getsockname(__Zephyr_fd, (struct sockaddr *)&bindin, &len))
|
|
63 return (errno);
|
|
64 }
|
|
65
|
|
66 __Zephyr_port = bindin.sin_port;
|
|
67 __Zephyr_open = 1;
|
|
68
|
|
69 if (port)
|
|
70 *port = bindin.sin_port;
|
|
71
|
|
72 return (ZERR_NONE);
|
|
73 }
|