-
Notifications
You must be signed in to change notification settings - Fork 2
/
getsock.c
139 lines (120 loc) · 2.58 KB
/
getsock.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* TwinSock - "Troy's Windows Sockets"
*
* Copyright (C) 1994-1995 Troy Rollo <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the license in the file LICENSE.TXT included
* with the TwinSock distribution.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "twinsock.h"
#include <string.h>
/* The following defines the maximum for each type of socket */
#define MAX_SOCKETS 256
#define INT_BITS 32
#define MAX_SOCKINTS (MAX_SOCKETS / INT_BITS)
static unsigned long aiClientSockets[MAX_SOCKINTS];
static unsigned long aiServerSockets[MAX_SOCKINTS];
static int iInitialised = 0;
static void
InitSocks(void)
{
if (!iInitialised)
{
iInitialised = 1;
memset(aiClientSockets, 0, sizeof(aiClientSockets));
memset(aiServerSockets, 0, sizeof(aiServerSockets));
}
}
static int
GetNewSocket(unsigned long *piSockets)
{
unsigned long iNow;
int i, j;
for (i = 0; i < MAX_SOCKINTS; i++)
{
for (j = 0, iNow = 1; j < INT_BITS; j++, iNow <<= 1)
{
if (!(piSockets[i] & iNow))
{
piSockets[i] |= iNow;
return i * INT_BITS + j;
}
}
}
return -1;
}
int
GetClientSocket(void)
{
int iSocket;
InitSocks();
iSocket = GetNewSocket(aiClientSockets);
if (iSocket == -1)
return iSocket;
else
return iSocket * 2;
}
int
GetServerSocket(void)
{
int iSocket;
InitSocks();
iSocket = GetNewSocket(aiServerSockets);
if (iSocket == -1)
return iSocket;
else
return iSocket * 2 + 1;
}
static void
ReleaseSocket( unsigned long *piSockets,
int iSocket)
{
int i;
int j;
i = iSocket / INT_BITS;
j = iSocket % INT_BITS;
piSockets[i] &= ~(1L << j);
}
void
ReleaseClientSocket(int iSocket)
{
ReleaseSocket(aiClientSockets, iSocket / 2);
}
void
ReleaseServerSocket(int iSocket)
{
ReleaseSocket(aiServerSockets, (iSocket - 1) / 2);
}
/* Fortunately all the calls which include sockets in their
* arguments have them as argument 1. HasSocketArg returns
* true if the function specified has this argument. If it
* does, we will need to replace the argument.
*/
int
HasSocketArg(enum Functions fn)
{
switch(fn)
{
case FN_Bind:
case FN_Close:
case FN_Connect:
case FN_IOCtl:
case FN_GetPeerName: /* Obsolete */
case FN_GetSockName: /* Obsolete */
case FN_Listen:
case FN_Select: /* Obsolete */
case FN_Send:
case FN_SendTo:
case FN_SetSockOpt:
case FN_Shutdown:
case FN_Socket: /* Has the client's idea in it */
return 1;
default:
return 0;
}
}