-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
307 lines (266 loc) · 8.76 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MSGBUFFERSIZE 1024 //String Nachrichtenbuffergröße für send/receive
#define DNS "rnit.space" //FQDN für Server
#define IDLEMSG "..." //Idle Nachrichten Trigger
#define PORT 8002 //Port für Server
/**
* Makes a DNS lookup for a given hostname.
*
* @param host Hostname to look up
* @param ipbuffer Buffer to write IPv4 with format *.*.*.*
* @return
*/
void dnsLookupIPv4(const char *host, char* ipBuffer, int ipBufferSize)
{
struct addrinfo hints, *result;
memset(ipBuffer, 0, sizeof(char) * ipBufferSize); //Initialisierung von ipBuffer
memset(&hints, 0, sizeof(hints)); //Initialisierung von hints
hints.ai_family = AF_INET; //IPv4
hints.ai_socktype = 0; //Alle Socket Typen
if(getaddrinfo(host, NULL, &hints, &result) != 0) //DNS lookup
{
perror("getaddrinfo");
exit(-1);
}
struct in_addr inAddr = (struct in_addr)((struct sockaddr_in *)result->ai_addr)->sin_addr;
//In Format für inet_ntoa bringen
strcpy(ipBuffer, inet_ntoa(inAddr)); //IP Adresse auslesen
printf ("~~~ Server ~~~\nHost DNS: %s\n", host); //Informationen ausgeben
printf ("IPv4 address: %s\n\n", ipBuffer);
}
/**
* Sends network packet with given socket.
*
* @param clientSocket Socket to use
* @param message Message to send
* @return
*/
void sendPacket(int clientSocket, char* message)
{
if(send(clientSocket, message, strlen(message), 0) < 0) //Nachricht senden
{
perror("send");
exit(-1);
}
printf("\n ~~ PACKET SEND BEGIN ~~~\n%s ~~ PACKET SEND END ~~~\n", message);
}
/**
* Receives network packet with given socket.
*
* @param clientSocket Socket to use
* @param buffer Buffer to write message
* @param buffersize Size of buffer
* @return
*/
int recvPacket(int clientSocket, char* buffer, int buffersize)
{
int msgByteCount = 0;
memset(buffer, 0, sizeof(char) * buffersize); //Buffer initialisieren
msgByteCount = recv(clientSocket, buffer, buffersize, 0); //Nachricht empfangen
if(msgByteCount < 0) //Error beim Nachrichtenempfang z.B. Timeout
{
perror("\nError: recv");
return -1;
}
else if(msgByteCount == 0) //Verbindung wurde von Server geschlossen
{
close(clientSocket); //Socket schließen
printf("\nInfo: Connection closed by peer.\n");
exit(0); //kein Return!!! in diesem Fall einfacher - anpassen bei Wiederbenützung
}
printf("\n ~~ PACKET RECEIVE BEGIN ~~~\n%s\n ~~ PACKET RECEIVE END ~~~\n", buffer);
return 1;
}
/**
* Calculates binary (integer) mathematical expression given with operators +, -, *, /
* i.e. "4 + 89", "47462 * 9042", etc.
*
* @param text Textual mathematical expression
* @return
*/
int calcBinMathText(char* text)
{
char operator[1]; //Operator für Ausdruck
char *saveptr; //Pointer für strtok_r
int left, right; //Linker und Rechter Teil des binären Ausdrucks
if(strstr(text, "+") != NULL) //Operator herausfiltern und setzen
{
operator[0] = '+';
}
else if(strstr(text, "-") != NULL)
{
operator[0] = '-';
}
else if(strstr(text, "*") != NULL)
{
operator[0] = '*';
}
else if(strstr(text, "/") != NULL)
{
operator[0] = '/';
}
char* split = strtok_r(text, (const char*)operator, &saveptr); //Linken Teil filtern
left = atoi((const char*)split);
split = strtok_r(NULL, (const char*)operator, &saveptr); //Rechten Teil filtern
right = atoi((const char*)split);
if(operator[0] == '+') //Operation ausführen
{
return left + right;
}
else if(operator[0] == '-')
{
return left - right;
}
else if(operator[0] == '*')
{
return left * right;
}
else if(operator[0] == '/')
{
return left / right;
}
printf("\ncalcBinMathText: Calculation failed. No valid operator found.\n");
return 0;
}
/**
* Checks if string starts with certain character.
*
* @param pre Character to check for.
* @param str String to check with pre.
* @return True or False
*/
_Bool startsWith(const char pre, const char *str)
{
return str[0] == pre;
}
/**
* Trims certain leading character of string.
* Will trim until character can't be found at string[0].
* CREDITS GO TO
* https://www.daniweb.com/programming/software-development/code/216919/implementing-string-trimming-ltrim-and-rtrim-in-c
*
* @param string String to trim.
* @param trimChar Character to trim.
* @return Trimmed String.
*/
void ltrim(char *string, char junk)
{
char* original = string;
char *p = original;
int trimmed = 0;
do
{
if (*original != junk || trimmed)
{
trimmed = 1;
*p++ = *original;
}
}
while (*original++ != '\0');
}
/**
* Solves riddles given within a string.
* Relies on function calcBinMathText for solving riddles.
*
* @param buffer String containing riddles.
* @param msgBufferSize Size of string buffer.
* @param results String to save results to.
* @param riddleToken Tokens to trigger riddle solving.
* @param riddleTokenCount Count of tokens to trigger riddle solving.
* @return
*/
void solveRiddle(char* buffer, int msgBufferSize, char* results,
char* riddleTokens, int riddleTokenCount)
{
char *saveptr; //saveptr für strtok_r
char* token = strtok_r(buffer, riddleTokens, &saveptr); //Ersten Teil des Strings filtern
int i = 0; //Zählervariable
_Bool tokenCheck = 0; //Wahrheitswert für Token Check
memset(results, 0, sizeof(char) * msgBufferSize); //Initialisiere results
ltrim(buffer, ' '); //Trimme führende Spaces
while(!tokenCheck && i < riddleTokenCount) //Suche ob Token in buffer
{
tokenCheck |= startsWith((const char)riddleTokens[i], buffer);
i++;
}
if(!tokenCheck) //Falls buffer nicht mit Token beginnt -> springe zum ersten Token
{
token = strtok_r(NULL, riddleTokens, &saveptr);
}
char result[msgBufferSize]; //String Zwischenspeicher für einzelne Ergebnisse
while (token != NULL) //Solange ein Token gefunden wird
{
memset(result, 0, sizeof(char) * msgBufferSize);
sprintf(result, "%i", calcBinMathText(token)); //Löse Rätsel
strcat(results, result); //Schreibe Ergebnis
strcat(results, "\n");
token = strtok_r(NULL, riddleTokens, &saveptr); //Springe zu nächstem Token
}
}
int main(int argc, char const *argv[])
{
char* calcTokens = ">›"; //Token um Berechnung durchzuführen
int calcTokenCount = 2; //Anzahl der Tokens in calcTokens
char srvIP[16] = {0}; //IP string *.*.*.*
int clientSocket = 0; //Socket für Servervebindung
struct sockaddr_in srvAdress; //Serverinfo
char packetRecvBuffer[MSGBUFFERSIZE] = {0}; //Buffer für einzelne erhaltene Pakete
char msgRecvBuffer[MSGBUFFERSIZE] = {0}; //Buffer für zusammengesetzte erhaltene Pakete
char packetSendBuffer[MSGBUFFERSIZE] = {0}; //Buffer zum Senden von Paketen
struct timeval tv; //struct für recv Timeout
tv.tv_sec = 5; //Standard Timeout 5.5s
tv.tv_usec = 500000;
dnsLookupIPv4(DNS, srvIP, 16); //DNS Lookup für Server
if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) //Erstelle Socket
{
perror("socket");
return -1;
}
memset(&srvAdress, '0', sizeof(srvAdress)); //Initialisiere Server Info
srvAdress.sin_family = AF_INET; //IPv4
srvAdress.sin_port = htons(PORT); //Port setzen
if(inet_pton(AF_INET, srvIP, &srvAdress.sin_addr) <= 0) //IP Adresse binär kodieren
{
perror("inte_pton");
return -1;
}
if(connect(clientSocket, (struct sockaddr *)&srvAdress, sizeof(srvAdress)) < 0) //Verbinde zu Server
{
perror("connect");
return -1;
}
while(1)
{
memset(msgRecvBuffer, 0, sizeof(char) * MSGBUFFERSIZE); //Initialisere alle Buffer
memset(packetRecvBuffer, 0, sizeof(char) * MSGBUFFERSIZE);
strcpy(packetRecvBuffer, IDLEMSG);
tv.tv_sec = 5; //Setze zurück auf Standard Timeout
setsockopt(clientSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
while(strstr(packetRecvBuffer, IDLEMSG) != NULL) //Warte solange Idle Nachrichten erhalten werden
{
recvPacket(clientSocket, packetRecvBuffer, MSGBUFFERSIZE);
}
strcpy(msgRecvBuffer, packetRecvBuffer); //Erstes Paket in msgRecvBuffer kopieren
tv.tv_sec = 0; //Setze Timeout für Paketempfang 0.5s
setsockopt(clientSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
//Solange weitere Pakete erhalten werden
while(recvPacket(clientSocket, packetRecvBuffer, MSGBUFFERSIZE) > 0)
{
strcat(msgRecvBuffer, packetRecvBuffer); //hänge weitere Pakete an msgRecvBuffer an
}
solveRiddle(msgRecvBuffer, MSGBUFFERSIZE, packetSendBuffer, calcTokens, calcTokenCount);
//Löse Rätsel
if(packetSendBuffer[0] != '\0') sendPacket(clientSocket, packetSendBuffer);
//Sende Antwort falls diese nicht leer ist
}
close(clientSocket); //Socket schließen
return 0;
}