]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - lib/ZOpenPort.c
fix zwgc man page on linux and note
[1ts-debian.git] / lib / ZOpenPort.c
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  *      $Id: ZOpenPort.c,v 1.15 1999/01/22 23:19:17 ghudson Exp $
7  *
8  *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #ifndef lint
14 static char rcsid_ZOpenPort_c[] = "$Id: ZOpenPort.c,v 1.15 1999/01/22 23:19:17 ghudson Exp $";
15 #endif
16
17 #include <internal.h>
18 #include <sys/socket.h>
19
20 Code_t ZOpenPort(port)
21     u_short *port;
22 {
23     struct sockaddr_in bindin;
24     int len;
25 #ifdef SO_BSDCOMPAT
26     int on = 1;
27 #endif
28     
29     (void) ZClosePort();
30
31     if ((__Zephyr_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
32         __Zephyr_fd = -1;
33         return (errno);
34     }
35
36 #ifdef SO_BSDCOMPAT
37     /* Prevent Linux from giving us socket errors we don't care about. */
38     setsockopt(__Zephyr_fd, SOL_SOCKET, SO_BSDCOMPAT, &on, sizeof(on));
39 #endif
40
41     bindin.sin_family = AF_INET;
42
43     if (port && *port)
44         bindin.sin_port = *port;
45     else
46         bindin.sin_port = 0;
47
48     bindin.sin_addr.s_addr = INADDR_ANY;
49
50     if (bind(__Zephyr_fd, (struct sockaddr *)&bindin, sizeof(bindin)) < 0) {
51         if (errno == EADDRINUSE && port && *port)
52             return (ZERR_PORTINUSE);
53         else
54             return (errno);
55     }
56
57     if (!bindin.sin_port) {
58         len = sizeof(bindin);
59         if (getsockname(__Zephyr_fd, (struct sockaddr *)&bindin, &len))
60             return (errno);
61     }
62     
63     __Zephyr_port = bindin.sin_port;
64     __Zephyr_open = 1;
65
66     if (port)
67         *port = bindin.sin_port;
68
69     return (ZERR_NONE);
70 }