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

Server functions over TCP. More...

#include "pch.h"

Functions

int CommunicationClientConnectToServer (PCSTR Ip, PCSTR Port, SOCKET *ConnectSocketArg)
 communication for client, connecting to the server
 
int CommunicationClientSendMessage (SOCKET ConnectSocket, const char *sendbuf, int buflen)
 Send message a client.
 
int CommunicationClientShutdownConnection (SOCKET ConnectSocket)
 shutdown the connection as a client
 
int CommunicationClientReceiveMessage (SOCKET ConnectSocket, CHAR *RecvBuf, UINT32 MaxBuffLen, PUINT32 BuffLenRecvd)
 Receive message as a client.
 
int CommunicationClientCleanup (SOCKET ConnectSocket)
 cleanup the connection as client
 

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

Function Documentation

◆ CommunicationClientCleanup()

int CommunicationClientCleanup ( SOCKET ConnectSocket)

cleanup the connection as client

Parameters
ConnectSocket
Returns
int
217{
218 //
219 // cleanup
220 //
221 closesocket(ConnectSocket);
222 WSACleanup();
223
224 return 0;
225}

◆ CommunicationClientConnectToServer()

int CommunicationClientConnectToServer ( PCSTR Ip,
PCSTR Port,
SOCKET * ConnectSocketArg )

communication for client, connecting to the server

Parameters
Ip
Port
ConnectSocketArg
Returns
int
24{
25 WSADATA wsaData;
26 SOCKET ConnectSocket = INVALID_SOCKET;
27 struct addrinfo *result = NULL, *ptr = NULL, hints;
28 int iResult;
29
30 //
31 // Initialize Winsock
32 //
33 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
34 if (iResult != 0)
35 {
36 ShowMessages("err, WSAStartup failed (%x)\n", iResult);
37 return 1;
38 }
39
40 ZeroMemory(&hints, sizeof(hints));
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_socktype = SOCK_STREAM;
43 hints.ai_protocol = IPPROTO_TCP;
44
45 //
46 // Resolve the server address and port
47 //
48 iResult = getaddrinfo(Ip, Port, &hints, &result);
49 if (iResult != 0)
50 {
51 ShowMessages("getaddrinfo failed (%x)\n", iResult);
52 WSACleanup();
53 return 1;
54 }
55
56 //
57 // Attempt to connect to an address until one succeeds
58 //
59 for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
60 {
61 //
62 // Create a SOCKET for connecting to server
63 //
64 ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
65 if (ConnectSocket == INVALID_SOCKET)
66 {
67 ShowMessages("socket failed with error: %ld\n", WSAGetLastError());
68 WSACleanup();
69 return 1;
70 }
71
72 //
73 // Connect to server.
74 //
75 iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
76 if (iResult == SOCKET_ERROR)
77 {
78 closesocket(ConnectSocket);
79 ConnectSocket = INVALID_SOCKET;
80 continue;
81 }
82 break;
83 }
84
85 freeaddrinfo(result);
86
87 if (ConnectSocket == INVALID_SOCKET)
88 {
89 ShowMessages("unable to connect to the server\n");
90 WSACleanup();
91 return 1;
92 }
93
94 //
95 // Store the arguments
96 //
97 *ConnectSocketArg = ConnectSocket;
98
99 return 0;
100}
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
result
Definition modelsim.py:117
NULL()
Definition test-case-generator.py:530

◆ CommunicationClientReceiveMessage()

int CommunicationClientReceiveMessage ( SOCKET ConnectSocket,
CHAR * RecvBuf,
UINT32 MaxBuffLen,
PUINT32 BuffLenRecvd )

Receive message as a client.

Parameters
ConnectSocket
RecvBuf
MaxBuffLen
BuffLenRecvd
Returns
int
174{
175 int Result;
176
177 //
178 // Receive until the peer closes the connection
179 //
180 Result = recv(ConnectSocket, RecvBuf, MaxBuffLen, 0);
181 if (Result > 0)
182 {
183 //
184 // Set recvd buff len
185 //
186 *BuffLenRecvd = Result;
187
188 /*
189 ShowMessages("bytes received: %d\n", iResult);
190 */
191 }
192 else if (Result == 0)
193 {
194 //
195 // Last packet
196 //
197 }
198 else
199 {
200 ShowMessages("\nrecv failed with error: %d\n", WSAGetLastError());
201 ShowMessages("the remote system closes the connection.\n\n");
202
203 return 1;
204 }
205
206 return 0;
207}

◆ CommunicationClientSendMessage()

int CommunicationClientSendMessage ( SOCKET ConnectSocket,
const char * sendbuf,
int buflen )

Send message a client.

Parameters
ConnectSocket
sendbuf
buflen
Returns
int
112{
113 int iResult;
114
115 //
116 // Send an initial buffer
117 //
118 iResult = send(ConnectSocket, sendbuf, buflen, 0);
119 if (iResult == SOCKET_ERROR)
120 {
121 ShowMessages("err, send failed (%x)\n", WSAGetLastError());
122 closesocket(ConnectSocket);
123 WSACleanup();
124 return 1;
125 }
126
127 return 0;
128}

◆ CommunicationClientShutdownConnection()

int CommunicationClientShutdownConnection ( SOCKET ConnectSocket)

shutdown the connection as a client

Parameters
ConnectSocket
Returns
int
138{
139 int iResult;
140
141 //
142 // shutdown the connection since no more data will be sent
143 //
144 iResult = shutdown(ConnectSocket, SD_SEND);
145 if (iResult == SOCKET_ERROR)
146 {
147 //
148 // We comment this line because the connection might be removed;
149 // thus, we don't need to show error
150 //
151
152 /*
153 ShowMessages("err, shutdown failed (%x)\n", WSAGetLastError());
154 */
155
156 closesocket(ConnectSocket);
157 WSACleanup();
158 return 1;
159 }
160 return 0;
161}