-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchat-lib.c
277 lines (234 loc) · 5.36 KB
/
chat-lib.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
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include "chat-lib.h"
#include "chat.h"
#define BUF_SIZE 1000 // buffer size for read
static int chat_loop_impl(void *arg);
/*
*
* Built in function for printing chat client info
*
*/
static void print_chat_client_info(chat_client *cc)
{
fprintf(stderr, "\n\nChat client\n");
fprintf(stderr, "* cc: %p\n", (void*)cc);
fprintf(stderr, "* host name: %s\n", cc->host_name);
fprintf(stderr, "* port: %d\n", cc->port);
}
/*
*
* Built in function for used feedback
*
*/
static int print_msg(char *msg, chat_client *cc)
{
if (cc==NULL)
{
return -1;
}
return fprintf(stdout, "%s\n", msg);
}
/*
*
* See API
*
*/
void chat_set_feedback_fun(chat_client *cc, input_handler fun)
{
cc->feedback = fun;
}
/*
*
* See API
*
*/
void*
chat_init(char *hostname, int port)
{
chat_client* cc =
calloc(sizeof(chat_client), 1);
if (cc==NULL || hostname==NULL)
{
return NULL;
}
cc->host_name = strdup(hostname);
cc->port = port ; //port;
fprintf(stderr, "host: %s:%d (%p)\n",
hostname, port, cc);
chat_set_feedback_fun(cc, (input_handler)print_msg);
return cc;
}
/*
*
* internal function. opens and sets up a socket
*
*/
static int open_socket(chat_client* cc)
{
if (cc==NULL)
{
return CHAT_CLIENT_SETUP_FAILED;
}
/* Create the socket (store in cc) */
cc->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (cc->sockfd < 0)
{
fprintf(stderr, "Could not open socket");
print_chat_client_info(cc);
return CHAT_CLIENT_COULD_NOT_OPEN_CHANNEL;
}
/* Find DNS entry for server */
cc->server = gethostbyname(cc->host_name);
if (cc->server == NULL)
{
fprintf(stderr, "Could not find host as %s\n", cc->host_name);
print_chat_client_info(cc);
return CHAT_CLIENT_COULD_NOT_OPEN_CHANNEL;
}
/* build address */
bzero((char *) &cc->serveraddr, sizeof(cc->serveraddr));
cc->serveraddr.sin_family = AF_INET;
bcopy((char *)cc->server->h_addr,
(char *)&(cc->serveraddr.sin_addr.s_addr),
(size_t)cc->server->h_length);
cc->serveraddr.sin_port = htons((short unsigned int)cc->port);
/* connect to server */
if (connect(cc->sockfd,
(struct sockaddr *)&(cc->serveraddr),
sizeof(cc->serveraddr)) < 0)
{
fprintf(stderr, "Could not connect to server: %s:%d\n", cc->host_name, cc->port);
print_chat_client_info(cc);
return CHAT_CLIENT_COULD_NOT_OPEN_CHANNEL;
}
FD_ZERO(&(cc->read_fds));
cc->nfds = (unsigned int)cc->sockfd +2;
return CHAT_CLIENT_OK;
}
int
chat_loop(chat_client *cc)
{
int ret;
cc->running = 1;
ret = chat_loop_impl(cc);
return ret;
}
/*
*
* See API
*
*/
static int
chat_loop_impl(void *arg)
{
int bytes;
char buf[BUF_SIZE];
int ret;
chat_client *cc = (chat_client *) arg;
if (cc==NULL)
{
return CHAT_CLIENT_BAD_ARG;
}
ret = open_socket(cc);
if (ret!=CHAT_CLIENT_OK)
{
fprintf(stderr, "Failed opening socket: %d\n", ret);
cc->running=0;
return CHAT_CLIENT_COULD_NOT_OPEN_CHANNEL;
}
while (cc->running)
{
fprintf(stdout, "type>");
fflush(stdout);
FD_SET(STDIN_FILENO,&(cc->read_fds));
FD_SET(cc->sockfd,&(cc->read_fds));
if (select((int)cc->nfds,&(cc->read_fds),NULL,NULL,NULL) == -1){
fprintf(stderr, "select failed....");
return CHAT_CLIENT_COULD_NOT_OPEN_CHANNEL;
}
if (FD_ISSET(cc->sockfd, &(cc->read_fds)))
{
/* read from server */
bzero(buf, BUF_SIZE);
bytes = (int)read(cc->sockfd, buf, BUF_SIZE);
if (bytes < 0)
{
fprintf(stderr, "ERROR reading from socket");
}
if (bytes == 0)
{
/* inform listener */
cc->feedback("Leaving since user typed 'bye'", cc);
cc->running = 0;
return CHAT_CLIENT_LEAVE;
}
printf("feedbacking on %p\n", (unsigned char *)((void*)cc->feedback));
printf("feedbacking on %p\n", (void*)cc->feedback);
cc->feedback(buf, cc);
}
else
{
/* read from stdin */
bzero(buf, BUF_SIZE);
fgets(buf, BUF_SIZE, stdin);
ret = chat_handle_input(cc, buf);
if (ret==CHAT_CLIENT_LEAVE)
{
return CHAT_CLIENT_LEAVE;
}
}
}
return CHAT_CLIENT_LEAVE;
}
/*
* internal macro for checking strings
*/
#define COMP_STR(a,b) strncmp(a, b, strlen(a))
/*
*
* See API
*
*/
int chat_handle_input(chat_client *cc, char *msg)
{
int written;
if (cc==NULL || msg==NULL)
{
return CHAT_CLIENT_BAD_ARG;
}
if (COMP_STR(".quit", msg)==0)
{
cc->feedback("Leaving since user typed '.quit'", cc);
cc->running = 0;
return CHAT_CLIENT_LEAVE;
}
written = (int)write(cc->sockfd, msg, strlen(msg));
if (written < 0)
{
fprintf(stderr,
"Failed writing to socket: '%s'\n",
msg);
return CHAT_CLIENT_ERROR;
}
return CHAT_CLIENT_OK;
}
/*
*
* See API
*
*/
void chat_close(chat_client *cc)
{
cc->running=0;
close(cc->sockfd);
if (cc!=NULL) {
if( cc->host_name!=NULL) {
free(cc->host_name);
}
free(cc);
}
}