~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ports/winnt/libisc/net.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1999-2001  Internet Software Consortium.
 
3
 *
 
4
 * Permission to use, copy, modify, and distribute this software for any
 
5
 * purpose with or without fee is hereby granted, provided that the above
 
6
 * copyright notice and this permission notice appear in all copies.
 
7
 *
 
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 
9
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 
10
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 
11
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
 
12
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 
13
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 
14
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 
15
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
 */
 
17
 
 
18
/* $Id: net.c,v 1.4 2001/11/21 05:07:25 mayer Exp $ */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <errno.h>
 
23
#include <unistd.h>
 
24
 
 
25
#include <isc/net.h>
 
26
#include <isc/strerror.h>
 
27
#include <isc/string.h>
 
28
#include <isc/util.h>
 
29
 
 
30
#if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
 
31
const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
 
32
#endif
 
33
 
 
34
static isc_boolean_t    once = ISC_FALSE;
 
35
static isc_result_t     ipv4_result = ISC_R_NOTFOUND;
 
36
static isc_result_t     ipv6_result = ISC_R_NOTFOUND;
 
37
 
 
38
static isc_result_t
 
39
try_proto(int domain) {
 
40
        SOCKET s;
 
41
        isc_result_t result = ISC_R_SUCCESS;
 
42
        char strbuf[ISC_STRERRORSIZE];
 
43
        int errval;
 
44
 
 
45
        s = socket(domain, SOCK_STREAM, 0);
 
46
        if (s == INVALID_SOCKET) {
 
47
                errval = WSAGetLastError();
 
48
                switch (errval) {
 
49
                case WSAEAFNOSUPPORT:
 
50
                case WSAEPROTONOSUPPORT:
 
51
                case WSAEINVAL:
 
52
                        return (ISC_R_NOTFOUND);
 
53
                default:
 
54
                        isc__strerror(errval, strbuf, sizeof(strbuf));
 
55
                        UNEXPECTED_ERROR(__FILE__, __LINE__,
 
56
                                         "socket() %s failed",
 
57
                                         strbuf);
 
58
                        return (ISC_R_UNEXPECTED);
 
59
                }
 
60
        }
 
61
 
 
62
#ifdef ISC_PLATFORM_HAVEIPV6
 
63
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
 
64
        if (domain == PF_INET6) {
 
65
                struct sockaddr_in6 sin6;
 
66
                unsigned int len;
 
67
 
 
68
                /*
 
69
                 * Check to see if IPv6 is broken, as is common on Linux.
 
70
                 */
 
71
                len = sizeof(sin6);
 
72
                if (getsockname(s, (struct sockaddr *)&sin6, (void *)&len) < 0)
 
73
                {
 
74
                        result = ISC_R_NOTFOUND;
 
75
                } else {
 
76
                        if (len == sizeof(struct sockaddr_in6))
 
77
                                result = ISC_R_SUCCESS;
 
78
                        else {
 
79
                                result = ISC_R_NOTFOUND;
 
80
                        }
 
81
                }
 
82
        }
 
83
#endif
 
84
#endif
 
85
 
 
86
        closesocket(s);
 
87
 
 
88
        return (result);
 
89
}
 
90
 
 
91
static void
 
92
initialize_action(void) {
 
93
        ipv4_result = try_proto(PF_INET);
 
94
#ifdef ISC_PLATFORM_HAVEIPV6
 
95
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
 
96
        ipv6_result = try_proto(PF_INET6);
 
97
#endif
 
98
#endif
 
99
}
 
100
 
 
101
static void
 
102
initialize(void) {
 
103
        if(once == ISC_FALSE) {
 
104
                initialize_action();
 
105
                once = ISC_TRUE;
 
106
        }
 
107
}
 
108
 
 
109
isc_result_t
 
110
isc_net_probeipv4(void) {
 
111
        initialize();
 
112
        return (ipv4_result);
 
113
}
 
114
 
 
115
isc_result_t
 
116
isc_net_probeipv6(void) {
 
117
        initialize();
 
118
        return (ipv6_result);
 
119
}
 
120
/*
 
121
 * Initialize socket services
 
122
 */
 
123
BOOL Win32InitSockets() {
 
124
        WORD wVersionRequested;
 
125
        WSADATA wsaData;
 
126
        int err;
 
127
 
 
128
        /* Need Winsock 2.0 or better */
 
129
        wVersionRequested = MAKEWORD(2, 0);
 
130
 
 
131
        err = WSAStartup(wVersionRequested, &wsaData);
 
132
        if ( err != 0 ) {
 
133
                /* Tell the user that we could not find a usable Winsock DLL */
 
134
                return(FALSE);
 
135
        }
 
136
        return(TRUE);
 
137
}