-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsockinfo.c
156 lines (135 loc) · 2.88 KB
/
sockinfo.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* TwinSock - "Troy's Windows Sockets"
*
* Copyright (C) 1994 Troy Rollo <troy@cbme.unsw.EDU.AU>
*
* 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 <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "sockinfo.h"
#include "tx.h"
tws_sockinfo *psiList = 0;
void
AddSocketEntry(int iClient, int iServer)
{
tws_sockinfo *psi;
psi = (tws_sockinfo *) malloc(sizeof(tws_sockinfo));
psi->iClientSocket = iClient;
psi->iServerSocket = iServer;
psi->pdata = 0;
psi->psiNext = psiList;
psi->ptxrConnect = 0;
psiList = psi;
return;
}
int
GetClientFromServer(int iServer)
{
tws_sockinfo *psi;
for (psi = psiList; psi; psi = psi->psiNext)
if (psi->iServerSocket == iServer)
return psi->iClientSocket;
return -1;
}
int
GetServerFromClient(int iClient)
{
tws_sockinfo *psi;
for (psi = psiList; psi; psi = psi->psiNext)
if (psi->iClientSocket == iClient)
return psi->iServerSocket;
return -1;
}
tws_sockinfo *
FindSocketEntry(int iServer)
{
tws_sockinfo *psi;
for (psi = psiList; psi; psi = psi->psiNext)
if (psi->iServerSocket == iServer)
return psi;
return 0;
}
void
ReleaseSocketEntry(int iClient)
{
tws_sockinfo **ppsi, *psi;
for (ppsi = &psiList; *ppsi; ppsi = &(*ppsi)->psiNext)
{
if ((*ppsi)->iClientSocket == iClient)
{
tws_data *pdata;
psi = (*ppsi)->psiNext;
if ((*ppsi)->ptxrConnect)
free((*ppsi)->ptxrConnect);
while ((*ppsi)->pdata)
{
pdata = (*ppsi)->pdata;
(*ppsi)->pdata = pdata->pdataNext;
free(pdata->pchData);
free(pdata);
}
free(*ppsi);
*ppsi = psi;
return;
}
}
}
void
QueueConnectWait(int iSocket,
struct tx_request *ptxr_)
{
tws_sockinfo *psi = FindSocketEntry(iSocket);
if (psi)
{
int iBytes = ntohs(ptxr_->nLen);
struct tx_request *ptxr = (struct tx_request *) malloc(iBytes);
memcpy(ptxr, ptxr_, iBytes);
psi->ptxrConnect = ptxr;
}
else
{
}
}
void
QueueSendRequest( int iServerSocket,
char *pchData,
int iBytes,
int iFlags,
struct sockaddr_in *psin)
{
tws_data *pdata, **ppdata;
tws_sockinfo *psi = FindSocketEntry(iServerSocket);
if (!psi)
return;
pdata = (tws_data *) malloc(sizeof(tws_data));
pdata->pchData = (char *) malloc(iBytes);
memcpy(pdata->pchData, pchData, iBytes);
pdata->nBytes = iBytes;
pdata->iLoc = 0;
pdata->pdataNext = 0;
pdata->iFlags = iFlags;
if (psin)
{
pdata->sinDest = *psin;
pdata->bTo = 1;
}
else
{
pdata->bTo = 0;
}
for (ppdata = &psi->pdata;
*ppdata;
ppdata = &(*ppdata)->pdataNext);
*ppdata = pdata;
if (psi->pdata == pdata)
WriteSocketData(psi);
}