HyperDbg Debugger
Loading...
Searching...
No Matches
tcpserver.cpp File Reference

Server functions over TCP. More...

#include "pch.h"

Macros

#define WIN32_LEAN_AND_MEAN
 

Functions

int CommunicationServerCreateServerAndWaitForClient (PCSTR Port, SOCKET *ClientSocketArg, SOCKET *ListenSocketArg)
 Create server and wait for a client to connect.
 
int CommunicationServerReceiveMessage (SOCKET ClientSocket, char *recvbuf, int recvbuflen)
 listen and receive message as the server
 
int CommunicationServerSendMessage (SOCKET ClientSocket, const char *sendbuf, int length)
 send message as the server
 
int CommunicationServerShutdownAndCleanupConnection (SOCKET ClientSocket, SOCKET ListenSocket)
 Shutdown and cleanup connection as server.
 

Detailed Description

Server functions over TCP.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-08-21

Macro Definition Documentation

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Function Documentation

◆ CommunicationServerCreateServerAndWaitForClient()

int CommunicationServerCreateServerAndWaitForClient ( PCSTR Port,
SOCKET * ClientSocketArg,
SOCKET * ListenSocketArg )

Create server and wait for a client to connect.

this function only accepts one client not multiple clients

Parameters
Port
ClientSocketArg
ListenSocketArg
Returns
int
34{
35 WSADATA wsaData;
36 int iResult;
37
38 SOCKET ListenSocket = INVALID_SOCKET;
39 SOCKET ClientSocket = INVALID_SOCKET;
40
41 struct addrinfo * result = NULL;
42 struct addrinfo hints;
43
44 //
45 // Initialize Winsock
46 //
47 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
48 if (iResult != 0)
49 {
50 ShowMessages("err, WSAStartup failed (%x)\n", iResult);
51 return 1;
52 }
53
54 ZeroMemory(&hints, sizeof(hints));
55 hints.ai_family = AF_INET;
56 hints.ai_socktype = SOCK_STREAM;
57 hints.ai_protocol = IPPROTO_TCP;
58 hints.ai_flags = AI_PASSIVE;
59
60 //
61 // Resolve the server address and port
62 //
63 iResult = getaddrinfo(NULL, Port, &hints, &result);
64 if (iResult != 0)
65 {
66 ShowMessages("err, getaddrinfo failed (%x)\n", iResult);
67 WSACleanup();
68 return 1;
69 }
70
71 //
72 // Create a SOCKET for connecting to server
73 //
74 ListenSocket =
75 socket(result->ai_family, result->ai_socktype, result->ai_protocol);
76 if (ListenSocket == INVALID_SOCKET)
77 {
78 ShowMessages("socket failed with error: %ld\n", WSAGetLastError());
79 freeaddrinfo(result);
80 WSACleanup();
81 return 1;
82 }
83
84 //
85 // Setup the TCP listening socket
86 //
87 iResult = ::bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
88 if (iResult == SOCKET_ERROR)
89 {
90 ShowMessages("err, bind failed (%x)\n", WSAGetLastError());
91 freeaddrinfo(result);
92 closesocket(ListenSocket);
93 WSACleanup();
94 return 1;
95 }
96
97 freeaddrinfo(result);
98
99 iResult = listen(ListenSocket, SOMAXCONN);
100 if (iResult == SOCKET_ERROR)
101 {
102 ShowMessages("err, listen failed (%x)\n", WSAGetLastError());
103 closesocket(ListenSocket);
104 WSACleanup();
105 return 1;
106 }
107
108 //
109 // Accept a client socket
110 //
111 sockaddr_in name = {0};
112 int addrlen = sizeof(name);
113
114 ClientSocket = accept(ListenSocket, (struct sockaddr *)&name, &addrlen);
115
116 if (ClientSocket == INVALID_SOCKET)
117 {
118 ShowMessages("err, accept failed (%x)\n", WSAGetLastError());
119 closesocket(ListenSocket);
120 WSACleanup();
121 return 1;
122 }
123
124 //
125 // Show that we connected to a client
126 //
127 ShowMessages("connected to : %s:%d\n", inet_ntoa(name.sin_addr), ntohs(name.sin_port));
128
129 //
130 // Set the argument
131 //
132 *ClientSocketArg = ClientSocket;
133 *ListenSocketArg = ListenSocket;
134
135 return 0;
136}
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
result
Definition modelsim.py:117
NULL()
Definition test-case-generator.py:530

◆ CommunicationServerReceiveMessage()

int CommunicationServerReceiveMessage ( SOCKET ClientSocket,
char * recvbuf,
int recvbuflen )

listen and receive message as the server

Parameters
ClientSocket
recvbuf
recvbuflen
Returns
int
148{
149 int iResult;
150
151 //
152 // Receive until the peer shuts down the connection
153 //
154 iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
155 if (iResult > 0)
156 {
157 //
158 // ShowMessages("bytes received: %d\n", iResult);
159 //
160 }
161 else if (iResult == 0)
162 {
163 //
164 // ShowMessages("connection closing...\n");
165 //
166 }
167 else
168 {
169 ShowMessages("err, recv failed (%x)\n", WSAGetLastError());
170 closesocket(ClientSocket);
171 WSACleanup();
172
173 return 1;
174 }
175
176 return 0;
177}

◆ CommunicationServerSendMessage()

int CommunicationServerSendMessage ( SOCKET ClientSocket,
const char * sendbuf,
int length )

send message as the server

Parameters
ClientSocket
sendbuf
length
Returns
int
189{
190 int iSendResult;
191
192 //
193 // Echo the buffer back to the sender
194 //
195 iSendResult = send(ClientSocket, sendbuf, length, 0);
196 if (iSendResult == SOCKET_ERROR)
197 {
198 /*
199 ShowMessages("err, send failed (%x)\n", WSAGetLastError());
200 closesocket(ClientSocket);
201 WSACleanup();
202 */
203 return 1;
204 }
205 return 0;
206}

◆ CommunicationServerShutdownAndCleanupConnection()

int CommunicationServerShutdownAndCleanupConnection ( SOCKET ClientSocket,
SOCKET ListenSocket )

Shutdown and cleanup connection as server.

Parameters
ClientSocket
ListenSocket
Returns
int
218{
219 int iResult;
220
221 //
222 // No longer need server socket
223 //
224 closesocket(ListenSocket);
225
226 //
227 // shutdown the connection since we're done
228 //
229 iResult = shutdown(ClientSocket, SD_SEND);
230 if (iResult == SOCKET_ERROR)
231 {
232 //
233 // We comment this line because the connection might be removed;
234 // thus, we don't need to show error
235 //
236
237 /*
238 ShowMessages("err, shutdown failed (%x)\n", WSAGetLastError());
239 */
240
241 closesocket(ClientSocket);
242 WSACleanup();
243 return 1;
244 }
245
246 //
247 // cleanup
248 //
249 closesocket(ClientSocket);
250 WSACleanup();
251
252 return 0;
253}