-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
263 lines (237 loc) · 5.67 KB
/
common.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
#include "common.h"
void logExit(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
// Used if user tries to run the program with a different number of params
void usage(int option, char **argv)
{
// Server usage
if (option)
{
printf("usage: %s <v4|v6> <server port>\n", argv[0]);
printf("example: %s v4 9000\n", argv[0]);
exit(EXIT_FAILURE);
}
// Client Usage
printf("usage: %s <server IP> <server port> start\n", argv[0]);
printf("example: %s 127.0.0.1 9000 start\n", argv[0]);
exit(EXIT_FAILURE);
}
int addrParse(const char *addrStr, const char *portStr,
struct sockaddr_storage *storage)
{
if (addrStr == NULL || portStr == NULL)
{
return -1;
}
uint16_t port = (uint16_t)atoi(portStr); // unsigned short
if (port == 0)
{
return -1;
}
port = htons(port); // host to network short
struct in_addr inaddr4; // 32-bit IP address
if (inet_pton(AF_INET, addrStr, &inaddr4))
{
struct sockaddr_in *addr4 = (struct sockaddr_in *)storage;
addr4->sin_family = AF_INET;
addr4->sin_port = port;
addr4->sin_addr = inaddr4;
return 0;
}
struct in6_addr inaddr6; // 128-bit IPv6 address
if (inet_pton(AF_INET6, addrStr, &inaddr6))
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)storage;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = port;
// addr6->sin6_addr = inaddr6
memcpy(&(addr6->sin6_addr), &inaddr6, sizeof(inaddr6));
return 0;
}
return -1;
}
void addrToStr(const struct sockaddr *addr, char *str, size_t strSize)
{
int version;
char addrStr[INET6_ADDRSTRLEN + 1] = "";
uint16_t port;
if (addr->sa_family == AF_INET)
{
version = 4;
struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
if (!inet_ntop(AF_INET, &(addr4->sin_addr), addrStr,
INET6_ADDRSTRLEN + 1))
{
logExit("ntop");
}
port = ntohs(addr4->sin_port); // network to host short
}
else if (addr->sa_family == AF_INET6)
{
version = 6;
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
if (!inet_ntop(AF_INET6, &(addr6->sin6_addr), addrStr,
INET6_ADDRSTRLEN + 1))
{
logExit("ntop");
}
port = ntohs(addr6->sin6_port); // network to host short
}
else
{
logExit("unknown protocol family.");
}
if (str)
{
snprintf(str, strSize, "IPv%d %s %hu", version, addrStr, port);
}
}
int serverSockaddrInit(const char *proto, const char *portStr,
struct sockaddr_storage *storage)
{
uint16_t port = (uint16_t)atoi(portStr); // unsigned short
if (port == 0)
{
return -1;
}
port = htons(port); // host to network short
memset(storage, 0, sizeof(*storage));
if (0 == strcmp(proto, "v4"))
{
struct sockaddr_in *addr4 = (struct sockaddr_in *)storage;
addr4->sin_family = AF_INET;
addr4->sin_addr.s_addr = INADDR_ANY;
addr4->sin_port = port;
return 0;
}
else if (0 == strcmp(proto, "v6"))
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)storage;
addr6->sin6_family = AF_INET6;
addr6->sin6_addr = in6addr_any;
addr6->sin6_port = port;
return 0;
}
else
{
return -1;
}
}
/*
* function to reverse a string
reference: https://www.techcrashcourse.com/2016/02/c-program-to-implement-your-own-itoa-function.html
*/
void my_reverse(char *str, int len)
{
int start, end;
char temp;
for (start = 0, end = len - 1; start < end; start++, end--)
{
temp = *(str + start);
*(str + start) = *(str + end);
*(str + end) = temp;
}
}
/*
* function to convert an integer into a string
reference: https://www.techcrashcourse.com/2016/02/c-program-to-implement-your-own-itoa-function.html
*/
char *itoa(int num, char *str, int base)
{
int i = 0;
int isNegative = FALSE;
/* A zero is same "0" string in all base */
if (num == 0)
{
str[i] = '0';
str[i + 1] = '\0';
return str;
}
/* negative numbers are only handled if base is 10
otherwise considered unsigned number */
if (num < 0 && base == 10)
{
isNegative = TRUE;
num = -num;
}
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'A' : rem + '0';
num = num / base;
}
/* Append negative sign for negative numbers */
if (isNegative)
{
str[i++] = '-';
}
str[i] = '\0';
my_reverse(str, i);
return str;
}
int createServerSocket(char *protocol, char *port)
{
struct sockaddr_storage storage;
if (serverSockaddrInit(protocol, port, &storage) != 0)
{
logExit("wrong params");
}
int sockfd = socket(storage.ss_family, SOCK_DGRAM, 0);
if (sockfd == -1)
{
logExit("socket");
}
int enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) != 0)
{
logExit("setsockopt");
}
struct sockaddr *serverAddr = (struct sockaddr *)(&storage);
if (bind(sockfd, serverAddr, sizeof(storage)) != 0)
{
logExit("bind");
}
return sockfd;
}
void generateRandomPokemon(struct Pokemon *pokemon, int id)
{
int poke = rand() % TYPES_OF_POKEMON;
if (poke == 0)
{
pokemon->hits = 0;
pokemon->maxHits = 1;
char nome[7];
strcpy(nome, ZUBAT);
strcpy(pokemon->name, nome);
//pokemon->name = nome;
pokemon->id = id;
}
else if (poke == 1)
{
pokemon->hits = 0;
pokemon->maxHits = 2;
char nome[7];
strcpy(nome, LUGIA);
strcpy(pokemon->name, nome);
//pokemon->name = nome;
pokemon->id = id;
}
else if (poke == 2)
{
pokemon->hits = 0;
pokemon->maxHits = 3;
char nome[7];
strcpy(nome, MILTON);
strcpy(pokemon->name, nome);
//pokemon->name = nome;
pokemon->id = id;
}
else
{
printf("Dunno how you reached here pal\n");
exit(EXIT_FAILURE);
}
}