[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / zephyr / ZOpenPort.c
blobc6f2024c0442c8f5b107b84d965a563394fb0a12
1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZOpenPort function.
4 * Created by: Robert French
6 * $Source$
7 * $Author: warmenhoven $
9 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
11 * "mit-copyright.h".
13 /* $Header$ */
15 #ifndef lint
16 static char rcsid_ZOpenPort_c[] = "$Header$";
17 #endif
19 #include <internal.h>
20 #include <sys/socket.h>
22 Code_t ZOpenPort(port)
23 u_short *port;
25 struct sockaddr_in bindin;
26 int len;
28 (void) ZClosePort();
30 if ((__Zephyr_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
31 __Zephyr_fd = -1;
32 return (errno);
35 #ifdef SO_BSDCOMPAT
37 int on = 1;
39 setsockopt(__Zephyr_fd, SOL_SOCKET, SO_BSDCOMPAT, (char *)&on,
40 sizeof(on));
42 #endif
44 bindin.sin_family = AF_INET;
46 if (port && *port)
47 bindin.sin_port = *port;
48 else
49 bindin.sin_port = 0;
51 bindin.sin_addr.s_addr = INADDR_ANY;
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);
60 if (!bindin.sin_port) {
61 len = sizeof(bindin);
62 if (getsockname(__Zephyr_fd, (struct sockaddr *)&bindin, &len))
63 return (errno);
66 __Zephyr_port = bindin.sin_port;
67 __Zephyr_open = 1;
69 if (port)
70 *port = bindin.sin_port;
72 return (ZERR_NONE);