-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.h
216 lines (173 loc) · 5.09 KB
/
helper.h
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
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"
void printMessage(char * message, int len){
int i;
for(i = 0; i < len; i++){
printf("%02x ", *(message+i));
}
printf("\n");
}
//Helper: handle the various task in clients
// TESTED
int handlelogin(char* name,int sock){
int i = 0;
int j;
// Header
struct header *hdr = (struct header *) malloc(sizeof(int)); // remember to free this
struct login_request * payload = (struct login_request *) malloc(sizeof(int)*3); // remember to free this
hdr->version = 0x04;
hdr->len = htons(0x0010);
hdr->msgtype = 0x1;
strcpy(payload->name,name);
unsigned char * payload_c = (unsigned char*) payload;
unsigned char * header_c = (unsigned char *) hdr;
unsigned char tosent[16];
for(j = 0; j < 16; j++){
if(j<4) tosent[j] = header_c[j];
else tosent[j] = payload_c[j-4];
}
// Send a login message to the server
int bytes_sent = send(sock, tosent,16,0);
if (bytes_sent < 0) perror("send failed");
free(hdr);
free(payload);
return 0;
}
// TESTED
int handlemove(unsigned char d, int sock){
struct header *hdr = (struct header *) malloc(sizeof(int)); // Remember to free this
struct move * payload = (struct move *) malloc(sizeof(int)); // Remember to free this
int j;
hdr->version = 0x4;
hdr->len = htons(0x08);
hdr->msgtype = MOVE;
payload->direction = d;
unsigned char * payload_c = (unsigned char*) payload;
unsigned char * header_c = (unsigned char *) hdr;
unsigned char tosent[8];
for(j = 0; j < 8; j++){
if(j<4){
tosent[j] = header_c[j];
} else {
tosent[j] = payload_c[j-4];
}
}
int bytes_sent = send(sock,tosent,8,0);
if (bytes_sent < 0){
perror("send failed.\n");
}
// Freeing the pointers;
free(hdr);
free(payload);
return 0;
}
// Helper for attack
// Assuming that the input is valid
// @TODO: Test
int handleattack(char * victim, int sock){
int j = 0;
// Sending message
struct header *hdr = (struct header *) malloc(sizeof(int)); // remember to free this
struct attack * payload = (struct attack *) malloc(sizeof(int)*3); // remember to free this
hdr->version = 0x4;
hdr->len = htons(0x10);
hdr->msgtype = ATTACK;
// Copy the name
strcpy(payload->victimname,victim);
unsigned char * payload_c = (unsigned char*) payload;
unsigned char * header_c = (unsigned char *) hdr;
unsigned char tosent[16];
for(j = 0; j < 16; j++){
if(j<4){
tosent[j] = header_c[j];
} else {
tosent[j] = payload_c[j-4];
}
}
// Sending
int bytes_sent = send(sock,tosent,16,0);
if (bytes_sent < 0){
perror("send failed.\n");
return -1;
} else {
//printf("Send: %d bytes\n", bytes_sent);
}
free(hdr);
free(payload);
return 0;
}
// Speak
int handlespeak(char * m, int sock){
// Header
unsigned int payloadLength = strlen(m) + 1 + 4 - ((strlen(m)+1) % 4);
struct header *hdr = (struct header *) malloc(sizeof(int)); // remember to free this
//msg = (char *) malloc(sizeof(char)*(strlen(m)+1));
int j;
unsigned int totalMessageLength = payloadLength + 4;
hdr->version = 0x4;
hdr->len = htons(totalMessageLength+300);
hdr->msgtype = SPEAK+10;
unsigned char * header_c = (unsigned char *) hdr;
unsigned char tosent[totalMessageLength];
for(j = 0; j < totalMessageLength; j++){
if(j<4){
tosent[j] = header_c[j];
} else {
tosent[j] = *(m+j-4);
}
}
// Send a speak message to the server
int bytes_sent = send(sock, tosent,totalMessageLength,0);
if (bytes_sent < 0) {
perror("send failed");
} else {
//printf("Sent: %d bytes\n", bytes_sent);
}
free(hdr);
return 0;
}
int handlelogout(char * name,int sock){
struct header *hdr = (struct header *) malloc(sizeof(int));
int j;
hdr->version = 0x4;
hdr->len = htons(0x4);
hdr->msgtype = LOGOUT;
char * header_c = (char*) hdr;
unsigned char tosent[4];
for(j = 0; j < 4; j++){
tosent[j] = header_c[j];
}
// Sending the message;
int bytes_sent = send(sock,tosent,4,0);
if (bytes_sent < 0){
perror("send failed.\n");
free(hdr);
free(hdr);
}
}
int sendslrequest(char * name, int udpsock,struct sockaddr_in sin, int currentID){
struct storage_location_request * slr = (struct storage_location_request *) malloc(sizeof(struct storage_location_request));
slr->message_type = STORAGE_LOCATION_REQUEST;
slr->id = currentID;
strcpy(slr->name,name);
char * tosent = (char*) slr;
int sent_bytes = sendto(udpsock,slr,STORAGE_LOCATION_REQUEST_SIZE,0,&sin,sizeof(sin));
if(sent_bytes < 0) {
perror("sendslrequest: sendto failed.");
}
return 0;
}
// Sending the SERVER_AREA_REQUEST
int sendsarequest(int udpsock,struct sockaddr_in sin, int currentID){
struct storage_location_request * slr = (struct storage_location_request *) malloc(sizeof(struct storage_location_request));
slr->message_type = STORAGE_LOCATION_REQUEST;
slr->id = currentID;
strcpy(slr->name,name);
char * tosent = (char*) slr;
int sent_bytes = sendto(udpsock,slr,STORAGE_LOCATION_REQUEST_SIZE,0,&sin,sizeof(sin));
if(sent_bytes < 0) {
perror("sendslrequest: sendto failed.");
}
return 0;
}