monotone

monotone Mtn Source Tree

Root/netxx/osutil.cxx

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains definitions for OS utility functions.
35**/
36
37// declaration include
38#include "common.h"
39#include <sstream>
40
41//####################################################################
42Netxx::error_type Netxx::get_last_error (void)
43{
44#if defined (WIN32)
45
46 return WSAGetLastError();
47
48#else
49
50 return errno;
51
52#endif
53}
54//####################################################################
55std::string Netxx::str_error(Netxx::error_type errnum)
56{
57#if defined (WIN32)
58
59 std::ostringstream s;
60
61 // try FormatMessage first--this will probably fail for anything < Win2k.
62 LPTSTR msg;
63 DWORD len;
64 if ((len = FormatMessage(
65 FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
66 0, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
67 reinterpret_cast<LPTSTR>(&msg), 0,
68 static_cast<va_list *>(0))) != 0) {
69 for (LPTSTR p = msg + len - 1; p > msg; --p)
70 if (*p == '\r' || *p == '\n')
71 *p = '\0';
72 s << msg;
73 LocalFree(msg);
74 } else {
75 // if it failed, try using our own table.
76 struct {
77 DWORD n;
78 char const * m;
79 } static const error_msgs[] = {
80 { WSAEINTR, "interrupted function call" },
81 { WSAEBADF, "invalid socket handle" },
82 { WSAEACCES, "access denied" },
83 { WSAEFAULT, "invalid address" },
84 { WSAEINVAL, "invalid argument" },
85 { WSAEMFILE, "too many open files" },
86 { WSAEWOULDBLOCK, "resource temporarily unavailable" },
87 { WSAEINPROGRESS, "operation in progress" },
88 { WSAEALREADY, "operating already in progress" },
89 { WSAENOTSOCK, "not a socket" },
90 { WSAEDESTADDRREQ, "destination address required" },
91 { WSAEMSGSIZE, "message too long" },
92 { WSAEPROTOTYPE, "incorrect protocol for socket" },
93 { WSAENOPROTOOPT, "invalid protocol option" },
94 { WSAEPROTONOSUPPORT, "protocol not supported" },
95 { WSAESOCKTNOSUPPORT, "socket type not supported" },
96 { WSAEOPNOTSUPP, "operation not supported" },
97 { WSAEPFNOSUPPORT, "protocol family not supported" },
98 { WSAEAFNOSUPPORT, "address family not supported" },
99 { WSAEADDRINUSE, "address already in use" },
100 { WSAEADDRNOTAVAIL, "unable to assign requested address" },
101 { WSAENETDOWN, "network down" },
102 { WSAENETUNREACH, "network unreachable" },
103 { WSAENETRESET, "dropped connection on reset" },
104 { WSAECONNABORTED, "connection aborted" },
105 { WSAECONNRESET, "connect reset by peer" },
106 { WSAENOBUFS, "no buffer space available" },
107 { WSAEISCONN, "socket already connected" },
108 { WSAENOTCONN, "socket not connected" },
109 { WSAESHUTDOWN, "connection shut down" },
110 { WSAETOOMANYREFS, "too many references to kernel object" },
111 { WSAETIMEDOUT, "connection timed out" },
112 { WSAECONNREFUSED, "connection refused" },
113 { WSAELOOP, "unable to translate name" },
114 { WSAENAMETOOLONG, "name or name component too long" },
115 { WSAEHOSTDOWN, "host down" },
116 { WSAEHOSTUNREACH, "host unreachable" },
117 { WSAENOTEMPTY, "unable to remove non-empty directory" },
118 { WSAEPROCLIM, "process limit exceeded" },
119 { WSAEUSERS, "quota exceeded" },
120 { WSAEDQUOT, "disk quota exceeded" },
121 { WSAESTALE, "stale socket handle" },
122 { WSAEREMOTE, "item not available locally" },
123 { WSASYSNOTREADY, "network service not available" },
124 { WSAVERNOTSUPPORTED, "unsupported winsock version requested" },
125 { WSANOTINITIALISED, "winsock not initialised" },
126 { WSAEDISCON, "peer disconnecting" },
127 { WSAENOMORE, "no further lookup results" },
128 { WSAECANCELLED, "lookup cancelled" },
129 { WSAEINVALIDPROCTABLE, "invalid procedure call table" },
130 { WSAEINVALIDPROVIDER, "invalid service provider" },
131 { WSAEPROVIDERFAILEDINIT, "service provider initialization failed" },
132 { WSASYSCALLFAILURE, "system call failure" },
133 { WSASERVICE_NOT_FOUND, "unknown service" },
134 { WSATYPE_NOT_FOUND, "unknown type" },
135 { WSA_E_NO_MORE, "no further lookup results" },
136 { WSA_E_CANCELLED, "lookup cancelled" },
137 { WSAEREFUSED, "lookup query refused" },
138 { WSAHOST_NOT_FOUND, "unknown host" },
139 { WSATRY_AGAIN, "try again" },
140 { WSANO_RECOVERY, "non-recoverable lookup failure" },
141 { WSANO_DATA, "no data found" },
142 { 0, 0 }
143 };
144
145 for (unsigned i = 0; error_msgs[i].m != 0; ++i)
146 if (error_msgs[i].n == errnum) {
147 s << error_msgs[i].m;
148 break;
149 }
150 }
151
152 s << " (" << errnum << ")";
153 return s.str();
154
155#else
156
157 return strerror(errnum);
158
159#endif
160}
161//####################################################################

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status