1 /**
2  * libqcproto: Vypress/QChat protocol interface library
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 /*
20  * QCS: qChat 1.6/VypressChat link interface
21  *
22  *	link module
23  *
24  * (c) Saulius Menkevicius 2001-2004
25  */
26 
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 
33 #ifdef _WIN32
34 /* Win32 system */
35 #include <windows.h>
36 #include <winsock2.h>
37 #include <ws2tcpip.h>
38 #else
39 /* UNIX system */
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45 #include <netdb.h>
46 #endif
47 
48 #include "qcproto.h"
49 #include "link.h"
50 #include "supp.h"
51 #include "p_vypress.h"
52 #include "p_qchat.h"
53 
54 #define VALID_ID(id)	(id!=NULL)
55 #define ERRRET(err)	if(1){errno=(err);return(0);}
56 #define ACTIVE_LINK(l)	((l)->rx >=0)
57 #define INVALIDATE(l)	((l)->rx=-1)
58 
59 /* static variables */
60 static int link_count = 0;
61 
62 /* internal routines */
63 
64 /* link_bind_rx:
65  *	binds link->rx to link->port
66  * returns:
67  *	non-0 on error
68  */
69 static int
link_bind_rx(link_data * link)70 link_bind_rx(link_data * link)
71 {
72 	struct sockaddr_in sa;
73 
74 	sa.sin_family = PF_INET;
75 	sa.sin_addr.s_addr = htonl(INADDR_ANY);
76 	sa.sin_port = htons(link->port);
77 
78 	return bind(link->rx, (struct sockaddr*)&sa, sizeof(sa))==-1;
79 }
80 
81 /* link_setup_broadcast:
82  *	setups link->tx to broadcast mode
83  * returns:
84  *	non-0 on error
85  */
86 static int
link_setup_broadcast(link_data * link)87 link_setup_broadcast(link_data * link)
88 {
89 	const int int_true = 1;
90 
91         return setsockopt(link->tx, SOL_SOCKET, SO_BROADCAST,
92 			(void*)&int_true, sizeof(int_true)
93 		) == -1;
94 }
95 
96 /* link_setup_multicast:
97  *	setups link->tx to multicast mode
98  * returns:
99  *	non-0 on error
100  */
101 static int
link_setup_multicast(link_data * link)102 link_setup_multicast(link_data * link)
103 {
104 	struct ip_mreq mreq;
105 	unsigned char opt;
106 
107 	/* set IP_MULTICAST_LOOP to 1, so our host receives
108 	 * the messages we send
109 	 */
110    	opt = 1;
111 	if(setsockopt(	link->tx, IPPROTO_IP, IP_MULTICAST_LOOP,
112 			(void*)&opt, sizeof(opt)) != 0)
113 		return 1;
114 
115 	/* set IP_MULTICAST_TTL to 32, that is the packets
116 	 * will go through 32 routers before getting scrapped
117 	 */
118 	opt = 32;
119 	if(setsockopt(	link->tx, IPPROTO_IP, IP_MULTICAST_TTL,
120 			(void*)&opt, sizeof(opt)) != 0)
121 		return 1;
122 
123 
124 	/* set our group membership */
125 	mreq.imr_multiaddr.s_addr = htonl(link->broadcast_addr);
126 	mreq.imr_interface.s_addr = INADDR_ANY;
127 	if(setsockopt(	link->rx, IPPROTO_IP, IP_ADD_MEMBERSHIP,
128 			(void*)&mreq, sizeof(mreq)) != 0)
129 		return 1;
130 
131 	return 0;	/* multicast setup */
132 }
133 
134 /** API implementation			*/
qcs_open(enum qcs_proto proto,enum qcs_proto_opt proto_opt,unsigned long broadcast_addr,unsigned short port)135 qcs_link qcs_open(
136 	enum qcs_proto proto,
137 	enum qcs_proto_opt proto_opt,
138 	unsigned long broadcast_addr,
139 	unsigned short port)
140 {
141 	const int int_true = 1;
142 	int error;
143 	link_data * link;
144 
145 	/* check params */
146 	if(proto >= QCS_PROTO_NUM)
147 		ERRRET(ENOSYS);
148 
149 	/* alloc and setup `link_data' struct */
150 	link = malloc(sizeof(link_data));
151 	if(link==NULL)
152 		ERRRET(ENOMEM);
153 
154 	link->proto = proto;
155 	link->port = port ? port: 8167;
156 	link->broadcast_addr = broadcast_addr;
157 
158 	/* alloc sockets */
159 	link->tx = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
160 	if(link->tx < 0) {
161 		free(link);
162 #ifdef WIN32
163 		errno = EINVAL;
164 #endif
165 		return NULL;
166 	}
167 
168 	/* setup rx */
169 	link->rx = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
170 	if(link->rx < 1) {
171 		close(link->tx);
172 		free(link);
173 #ifdef WIN32
174 		errno = EINVAL;
175 #endif
176 		return NULL;
177 	}
178 
179 #ifdef WIN32
180 	/* on win32, we can setup the rx socket so, that we can use
181 	 * multiple clients on the same pc simultaneously
182 	 */
183 	setsockopt(link->rx, SOL_SOCKET, SO_REUSEADDR,
184 		(void*)&int_true, sizeof(int_true));
185 #endif
186 	/* bind the rx socket to the port specified
187 	 */
188 	if(link_bind_rx(link)) {
189 		close(link->rx);
190 		close(link->tx);
191 		free(link);
192 #ifdef WIN32
193 		errno = EINVAL;
194 #endif
195 		return NULL;
196 	}
197 
198 	/* setup link to multicast/broadcast mode, as specified
199 	 */
200 	if(proto_opt & QCS_PROTO_OPT_MULTICAST)
201 		error = link_setup_multicast(link);
202 	else	error = link_setup_broadcast(link);
203 	if(error) {
204 		close(link->tx);
205 		close(link->rx);
206 		free(link);
207 #ifdef WIN32
208 		errno = ENOSYS;
209 #endif
210 		return NULL;
211 	}
212 
213 	/* increase link count */
214 	link_count ++;
215 
216 	/* return success */
217 	return (qcs_link)link;
218 }
219 
qcs_close(qcs_link link_id)220 int qcs_close(qcs_link link_id)
221 {
222 	link_data * link = (link_data *)link_id;
223 
224 	/* check if link is valid */
225 	if(!VALID_ID(link_id)) ERRRET(EINVAL);
226 	if(!ACTIVE_LINK(link)) ERRRET(EINVAL);
227 
228 	/* shutdown sockets and free the struct */
229 	close(link->rx);
230 	close(link->tx);
231 	free(link);
232 
233 	link_count--;
234 	if( link_count==0 ) {
235 		/* clean up duplicate buffer */
236 		qcs__cleanup_dup();
237 	}
238 
239 	return 1;
240 }
241 
qcs_rxsocket(qcs_link link_id,int * p_rxsocket)242 int qcs_rxsocket(
243 	qcs_link link_id,
244 	int * p_rxsocket )
245 {
246 	link_data * link = (link_data*)link_id;
247 
248 	if(!VALID_ID(link_id)) ERRRET(EINVAL);
249 	if(!ACTIVE_LINK(link)) ERRRET(EINVAL);
250 
251 	*p_rxsocket = link->rx;
252 	return 1;
253 }
254 
qcs_waitinput(qcs_link link_id,int timeout_ms)255 int qcs_waitinput(
256 	qcs_link link_id,
257 	int timeout_ms )
258 {
259 	link_data * link = (link_data *) link_id;
260 	struct timeval tv;
261 	fd_set fds;
262 
263 	// check if link is valid
264 	if(!VALID_ID(link_id)) ERRRET(EINVAL);
265 	if(!ACTIVE_LINK(link)) ERRRET(EINVAL);
266 
267 	// fill structs & select()
268 	if(timeout_ms < 0) {
269 		tv.tv_sec = 0;
270 		tv.tv_usec = 0;
271 	} else {
272 		tv.tv_sec = timeout_ms / 1000;
273 		tv.tv_usec = (timeout_ms % 1000) * 1000;
274 	}
275 
276 	FD_ZERO(&fds);
277 	FD_SET(link->rx, &fds);
278 
279 	return select(FD_SETSIZE, &fds, NULL, NULL, &tv);
280 }
281 
qcs_send(qcs_link link_id,const qcs_msg * msg)282 int qcs_send(
283 	qcs_link link_id,
284 	const qcs_msg * msg )
285 {
286 	char * datagram;
287 	ssize_t datagram_len, datagram_sent;
288 	struct sockaddr_in sab;
289 	link_data * link = (link_data *)link_id;
290 
291 	// check link
292 	if(!VALID_ID(link_id)) ERRRET(EINVAL);
293 	if(!ACTIVE_LINK(link)) ERRRET(EINVAL);
294 
295 	// build proto message
296 	switch(link->proto) {
297 	case QCS_PROTO_VYPRESS:
298 		datagram = qcs__make_vypress_msg(msg, &datagram_len);
299 		break;
300 	case QCS_PROTO_QCHAT:
301 		datagram = qcs__make_qchat_msg(msg, &datagram_len);
302 		break;
303 	default:
304 		ERRRET(ENOSYS);
305 		break;
306 	}
307 	if(datagram==NULL)
308 		ERRRET(EINVAL);
309 
310 	sab.sin_family = PF_INET;
311 	sab.sin_port = htons(link->port);
312 	sab.sin_addr.s_addr = htonl(link->broadcast_addr);
313 	datagram_sent = sendto(
314 		link->tx, datagram, datagram_len, 0,
315 		(struct sockaddr*)&sab, sizeof(sab));
316 
317 	free(datagram);
318 
319 	if(datagram_sent != datagram_len)
320 		ERRRET(ENETUNREACH);
321 
322 	return 1;
323 }
324 
qcs_recv(qcs_link link_id,qcs_msg * msg)325 int qcs_recv(
326 	qcs_link link_id,
327 	qcs_msg * msg )
328 {
329 	link_data * link = (link_data *)link_id;
330 	char * buff;
331 
332 	struct sockaddr_in sa;
333 	socklen_t sa_len;
334 	ssize_t dgram_size;
335 	int parse_ok;
336 
337 	if(msg==NULL) ERRRET(EINVAL);
338 
339 	if(!VALID_ID(link_id)) ERRRET(EINVAL);
340 	if(!ACTIVE_LINK(link)) ERRRET(EINVAL);
341 
342 	buff = (char*) malloc(QCP_MAXUDPSIZE+0x10);
343 	if(buff==NULL) ERRRET(ENOMEM);
344 
345 	/* receive data of the message */
346 	sa_len = sizeof(sa);
347 	dgram_size = recvfrom(
348 		link->rx, (void*)buff, QCP_MAXUDPSIZE, 0,
349 		(struct sockaddr*)&sa, &sa_len
350 	);
351 
352 	/* fill in message source address */
353 	msg->src_ip = ntohl(sa.sin_addr.s_addr);
354 
355 	/* check if msg is too long for us to process:
356 	 * just strip it down. we guess this was the last
357 	 * ascii field, in proto msg, that was soo long.
358 	 */
359 	if(dgram_size > QCP_MAXUDPSIZE)
360 		*(char*)(buff+QCP_MAXUDPSIZE-1) = '\0';
361 
362 	/* failure */
363 	if(dgram_size < 0) {
364 		free(buff);
365 		return 0;
366 	}
367 
368 	/* parse the message */
369 	switch(link->proto) {
370 	case QCS_PROTO_QCHAT:
371 		parse_ok = qcs__parse_qchat_msg(buff, dgram_size, msg);
372 		break;
373 	case QCS_PROTO_VYPRESS:
374 		parse_ok = qcs__parse_vypress_msg(buff, dgram_size, msg);
375 		break;
376 	default: break;
377 	}
378 
379 	/* cleanup */
380 	free((void*)buff);
381 	return parse_ok;
382 }
383 
qcs_newmsg()384 qcs_msg * qcs_newmsg()
385 {
386 	qcs_msg * msg = malloc(sizeof(qcs_msg));
387 	if(msg==NULL)
388 		return NULL;
389 
390 	msg->msg = QCS_MSG_INVALID;
391 	msg->umode = QCS_UMODE_INVALID;
392 	msg->uactive = 1;
393 	msg->src = msg->dst = msg->text = msg->supp = msg->chan = NULL;
394 
395 	return msg;
396 }
397 
qcs_deletemsg(qcs_msg * msg)398 void qcs_deletemsg( qcs_msg * msg )
399 {
400 	if(msg==NULL) return;
401 
402 	if( msg->src ) free( msg->src );
403 	if( msg->dst ) free( msg->dst );
404 	if( msg->text ) free( msg->text );
405 	if( msg->supp ) free( msg->supp );
406 	if( msg->chan ) free( msg->chan );
407 
408 	free( msg );
409 }
410 
qcs_msgset(qcs_msg * msg,enum qcs_textid which,const char * new_text)411 int qcs_msgset(
412 	qcs_msg * msg,
413 	enum qcs_textid which,
414 	const char * new_text )
415 {
416 	char ** ptext, * new_p;
417 	size_t slen;
418 
419 	if(!msg) {
420 		errno = EINVAL;
421 		return 0;
422 	}
423 
424 	switch(which) {
425 	case QCS_SRC: ptext = &msg->src; break;
426 	case QCS_DST: ptext = &msg->dst; break;
427 	case QCS_TEXT: ptext = &msg->text; break;
428 	case QCS_SUPP: ptext = &msg->supp; break;
429 	case QCS_CHAN: ptext = &msg->chan; break;
430 	default:
431 		errno = EINVAL;
432 		return 0;
433 	}
434 
435 	if(*ptext==NULL && new_text==NULL) {
436 		/* we have nothing to set here */
437 		return 1;
438 	}
439 
440 	/* try to allocate new space, if we have to
441 	 * before we damage something
442 	 */
443 	if(new_text) {
444 		slen = strlen(new_text);
445 		new_p = malloc(slen + 1);
446 		if(new_p==NULL) {
447 			/* return -- out of mem */
448 			errno = ENOMEM;
449 			return 0;
450 		}
451 		memcpy((void*)new_p, (void*)new_text, slen + 1);
452 	}
453 
454 	/* clear previous text */
455 	if(*ptext!=NULL) {
456 		free((void*)*ptext);
457 		*ptext = NULL;
458 
459 		/* return if we have nothing to set */
460 		if(new_text==NULL) {
461 			return 1;
462 		}
463 	}
464 
465 	/* set new text */
466 	*ptext = new_p;
467 
468 	return 1;
469 }
470