-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinsock.c
74 lines (67 loc) · 1.92 KB
/
winsock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* $Id: //devel/tools/main/nbtscan/winsock.c#1 $
*
* This module is responsible for initailizing the Winsock modules
* under Win32. We would rather use Winsock 2, but this is not
* universally available so we live with 1.1.
*
* We're a bit fuzzy on the version number business and on whether
* we really need to call WSAStartup() more than once. Ugh. We'll
* add more when we find it.
*/
#ifndef COMMONFILE
# define COMMONFILE "libcommon.h"
#endif
#include COMMONFILE
#include <stdio.h>
#include "penlib.h"
#if _WIN32
const char * __stdcall printable_wserror(DWORD dwErr, char *obuf, size_t osize)
{
UNUSED_PARAMETER(osize);
switch ( dwErr )
{
case WSASYSNOTREADY: return "Network not ready";
case WSAVERNOTSUPPORTED: return "Version not supported";
case WSAEINPROGRESS: return "WS1.1 op in progress";
case WSAEPROCLIM: return "Too many tasks";
case WSAEFAULT: return "Data fault";
case WSAECONNREFUSED: return "Connection refused";
case WSAEWOULDBLOCK: return "Operation would block";
default: sprintf(obuf, "WS Error#%ld", dwErr);
return obuf;
}
}
#endif
#ifdef _WIN32
void __stdcall init_winsock(int verbose, int level)
{
WORD wVersion = MAKEWORD(level,level);
WSADATA wsaData;
DWORD err;
/*---------------------------------------------------------------
* First try to initialize Winsock with version 2.2, then fall
* back to 1.1
*/
if ( (err = (DWORD)WSAStartup(wVersion, &wsaData)) != 0 )
{
char errbuf[256];
die("ERROR: initializing Winsock [%s]",
printable_wserror(err, errbuf, sizeof errbuf));
}
if ( verbose )
{
printf("Using Winsock %d.%d",
LOBYTE(wsaData.wVersion),
HIBYTE(wsaData.wVersion) );
if ( wsaData.wVersion != wsaData.wHighVersion )
{
printf(" (%d.%d available)",
LOBYTE(wsaData.wHighVersion),
HIBYTE(wsaData.wHighVersion) );
}
printf("\n");
fflush(stdout);
}
}
#endif /* _WIN32 */