-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
180 lines (153 loc) · 5.34 KB
/
client.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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "sql_queries.h"
#include "messages.h"
#include "utils.h"
#include "game_client.h"
#define MSG_MAX_LEN 4096
#define MULTICAST_GROUP "239.0.0.1"
#define MULTICAST_PORT 12345
void authenticate(int sockfd) {
int authenticated = 0;
char message[MSG_MAX_LEN];
while (!authenticated) {
char hello[] = "HELLO";
send_util(sockfd, hello);
read_util(sockfd, message, MSG_MAX_LEN);
printf("%s", message);
char username[USERNAME_LEN + 1];
scanf("%256s", username);
send_util(sockfd, username);
read_util(sockfd, message, MSG_MAX_LEN);
if (strncmp(message, invalid_username, strlen(invalid_username)) == 0) {
printf("%s", message);
continue;
}
if (strcmp(message, password_prompt) == 0) { // user exists
char password[PASSWORD_LEN + 1];
printf("%s", message);
scanf("%256s", password);
send_util(sockfd, password);
read_util(sockfd, message, MSG_MAX_LEN);
if (strncmp(message, invalid_password, strlen(invalid_password)) == 0) { // invalid password
printf("%s", message);
continue;
} else if (strncmp(message, auth_success, strlen(auth_success)) == 0) { // password correct
authenticated = 1;
send_util(sockfd, "OK");
continue;
} else {
printf("Error, unrecognized message: %s", message);
}
} else if (strncmp(message, no_such_user, strlen(no_such_user)) == 0) { // user doesn't exist, register
printf("%s", message);
char password[PASSWORD_LEN + 1];
scanf("%256s", password);
send_util(sockfd, password);
read_util(sockfd, message, MSG_MAX_LEN);
if (strncmp(message, user_added, strlen(user_added)) == 0) { // registered successfully
printf("%s", message);
authenticated = 1;
continue;
} else if (strncmp(message, user_add_failed, strlen(user_add_failed)) == 0) { // user registration failed
printf("%s", message);
continue;
}
}
}
}
void* receive_multicast(void* arg) {
int multicast_fd;
struct sockaddr_in multicast_addr;
struct ip_mreq mreq;
char msgbuf[MSG_MAX_LEN];
int nbytes;
socklen_t addrlen;
if ((multicast_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
u_int yes = 1;
if (setsockopt(multicast_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(INADDR_ANY);
multicast_addr.sin_port = htons(MULTICAST_PORT);
if (bind(multicast_fd, (struct sockaddr*) &multicast_addr, sizeof(multicast_addr)) < 0) {
perror("bind");
exit(1);
}
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_GROUP);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(multicast_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
perror("setsockopt");
exit(1);
}
while (1) {
addrlen = sizeof(multicast_addr);
if ((nbytes = recvfrom(multicast_fd, msgbuf, MSG_MAX_LEN, 0, (struct sockaddr*) &multicast_addr, &addrlen)) < 0) {
perror("recvfrom");
exit(1);
}
msgbuf[nbytes] = '\0';
printf("\n------------------------------------\n");
printf("Received multicast message: %s", msgbuf);
printf("------------------------------------\n");
}
close(multicast_fd);
return NULL;
}
int main(int argc, char* argv[]) {
int sockfd, err;
struct sockaddr_in servaddr;
char servip[16] = "172.104.225.148";
setvbuf(stdout, NULL, _IONBF, 0);
if (argc > 1) {
strncpy(servip, argv[1], 16);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
fprintf(stderr, "socket error: %s", strerror(errno));
return 1;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(1337);
if ((err = inet_pton(AF_INET, servip, &servaddr.sin_addr) <= 0)) {
if (err == 0) fprintf(stderr, "inet_pton error for %s", servip);
else fprintf(stderr, "inet_pton error for %s: %s", servip, strerror(errno));
return 1;
}
if (connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0) {
fprintf(stderr, "connect error: %s", strerror(errno));
return 1;
}
authenticate(sockfd);
pthread_t multicast_thread;
if (pthread_create(&multicast_thread, NULL, receive_multicast, NULL) != 0) {
perror("pthread_create");
return 1;
}
set_all_ships(sockfd);
play_game(sockfd);
pthread_cancel(multicast_thread);
pthread_join(multicast_thread, NULL);
close(sockfd);
return 0;
}