iocalls.c 15.3 KB
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Este programa está nomeado como iocalls.c e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 * licinio@bb.com.br		(Licínio Luis Branco)
 * kraucer@bb.com.br		(Kraucer Fernandes Mazuco)
 *
 */

#include "private.h"
#include <sys/time.h>
#include <sys/types.h>
#include "xioc.h"
#include "telnetc.h"
#include "utilc.h"

#define MILLION			1000000L

#if defined(_WIN32)
	#define MAX_HA	256
#endif

/*---[ Standard calls ]-------------------------------------------------------------------------------------*/

// Timeout calls
static void       internal_remove_timeout(H3270 *session, void *timer);
static void		* internal_add_timeout(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session));

static void		* internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata );
static void		  internal_remove_poll(H3270 *session, void *id);

static int		  internal_wait(H3270 *session, int seconds);

static void		  internal_ring_bell(H3270 *session);

/*---[ Active callbacks ]-----------------------------------------------------------------------------------*/

 static void	* (*add_timeout)(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session))
					= internal_add_timeout;

 static void	  (*remove_timeout)(H3270 *session, void *timer)
					= internal_remove_timeout;

 static void	* (*add_poll)(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata)
					= internal_add_poll;

 static void	  (*remove_poll)(H3270 *session, void *id)
					= internal_remove_poll;

 static int	  	  (*wait)(H3270 *session, int seconds)
					= internal_wait;

 static int 	  (*event_dispatcher)(H3270 *session,int wait)
					= lib3270_default_event_dispatcher;

 static void	  (*ring_bell)(H3270 *)
					= internal_ring_bell;

/*---[ Typedefs ]-------------------------------------------------------------------------------------------*/

 #define TN	(timeout_t *)NULL

/*---[ Implement ]------------------------------------------------------------------------------------------*/


/* Timeouts */

#if defined(_WIN32)
static void ms_ts(unsigned long long *u)
{
	FILETIME t;

	/* Get the system time, in 100ns units. */
	GetSystemTimeAsFileTime(&t);
	memcpy(u, &t, sizeof(unsigned long long));

	/* Divide by 10,000 to get ms. */
	*u /= 10000ULL;
}
#endif

static void * internal_add_timeout(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session))
{
	timeout_t *t_new;
	timeout_t *t;
	timeout_t *prev = TN;

	trace("%s session=%p proc=%p interval=%ld",__FUNCTION__,session,proc,interval_ms);

	t_new = (timeout_t *) lib3270_malloc(sizeof(timeout_t));

	t_new->proc = proc;
	t_new->session = session;
	t_new->in_play = False;

#if defined(_WIN32)
	ms_ts(&t_new->ts);
	t_new->ts += interval_ms;
#else

	gettimeofday(&t_new->tv, NULL);
	t_new->tv.tv_sec += interval_ms / 1000L;
	t_new->tv.tv_usec += (interval_ms % 1000L) * 1000L;

	if (t_new->tv.tv_usec > MILLION)
	{
		t_new->tv.tv_sec += t_new->tv.tv_usec / MILLION;
		t_new->tv.tv_usec %= MILLION;
	}
#endif /*]*/

	/* Find where to insert this item. */
	for (t = session->timeouts; t != TN; t = t->next)
	{
#if defined(_WIN32)
		if (t->ts > t_new->ts)
#else
		if (t->tv.tv_sec > t_new->tv.tv_sec || (t->tv.tv_sec == t_new->tv.tv_sec && t->tv.tv_usec > t_new->tv.tv_usec))
#endif
		break;

		prev = t;
	}

	// Insert it.
	if (prev == TN)
	{	// Front.
		t_new->next = session->timeouts;
		session->timeouts = t_new;
	}
	else if (t == TN)
	{	// Rear.
		t_new->next = TN;
		prev->next = t_new;
	}
	else
	{	// Middle.
		t_new->next = t;
		prev->next = t_new;
	}

	trace("Timeout %p added with value %ld",t_new,interval_ms);

	return t_new;
}

static void internal_remove_timeout(H3270 *session, void * timer)
{
	timeout_t *st = (timeout_t *)timer;
	timeout_t *t;
	timeout_t *prev = TN;

	trace("Removing timeout: %p",st);

	if (st->in_play)
		return;

	for (t = session->timeouts; t != TN; t = t->next)
	{
		if (t == st)
		{
			if (prev != TN)
				prev->next = t->next;
			else
				session->timeouts = t->next;
			lib3270_free(t);
			return;
		}
		prev = t;
	}
}

/* I/O events. */

static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*call)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata )
{
	input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));

	ip->session					= session;
	ip->fd						= fd;
	ip->flag					= flag;
	ip->userdata				= userdata;
	ip->call					= call;

	session->inputs 			= ip;
	session->inputs_changed 	= 1;

	return ip;
}

static void internal_remove_poll(H3270 *session, void *id)
{
	input_t *ip;
	input_t *prev = (input_t *)NULL;

	for (ip = session->inputs; ip != (input_t *)NULL; ip = ip->next)
	{
		if (ip == (input_t *)id)
			break;

		prev = ip;
	}

	if (ip == (input_t *)NULL)
	{
		lib3270_write_log(session,"lib3270","Invalid call to (%s): Input %p wasnt found in the list",__FUNCTION__,id);
		return;
	}

	if (prev != (input_t *)NULL)
		prev->next = ip->next;
	else
		session->inputs = ip->next;

	lib3270_free(ip);
	session->inputs_changed = 1;
}

LIB3270_EXPORT void	 lib3270_remove_poll(H3270 *session, void *id) {
	debug("%s %p",__FUNCTION__,id);
	remove_poll(session, id);
}

LIB3270_EXPORT void	 lib3270_remove_poll_fd(H3270 *session, int fd)
{

	input_t *ip;

	for (ip = session->inputs; ip != (input_t *)NULL; ip = ip->next)
	{
		if(ip->fd == fd)
		{
			remove_poll(session, ip);
			return;
		}
	}

	lib3270_write_log(NULL,"iocalls","Invalid or unexpected FD on %s(%d)",__FUNCTION__,fd);

}

LIB3270_EXPORT void	 lib3270_update_poll_fd(H3270 *session, int fd, LIB3270_IO_FLAG flag)
{

	input_t *ip;

	for (ip = session->inputs; ip != (input_t *)NULL; ip = ip->next)
	{
		if(ip->fd == fd)
		{
			ip->flag = flag;
			return;
		}
	}

	lib3270_write_log(session,"iocalls","Invalid or unexpected FD on %s(%d)",__FUNCTION__,fd);

}

LIB3270_EXPORT void	 * lib3270_add_poll_fd(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*call)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata ) {
	return add_poll(session,fd,flag,call,userdata);
}

/* Event dispatcher. */
int lib3270_default_event_dispatcher(H3270 *hSession, int block)
{
#if defined(_WIN32)
	unsigned long long now;
//	int i;
	int maxSock;
	DWORD tmo;
#else
	int ns;
	struct timeval now, twait, *tp;
	int events;
#endif

	fd_set rfds, wfds, xfds;

	input_t *ip;
	int processed_any = 0;

retry:

	hSession->inputs_changed = 0;

	// If we've processed any input, then don't block again.
	if(processed_any)
		block = 0;

#if defined(_WIN32)

	maxSock = 0;

	FD_ZERO(&rfds);
	FD_ZERO(&wfds);
	FD_ZERO(&xfds);

	for (ip = hSession->inputs; ip != (input_t *)NULL; ip = ip->next)
	{
		if(ip->flag & LIB3270_IO_FLAG_READ)
		{
			FD_SET(ip->fd, &rfds);
			maxSock = max(ip->fd,maxSock);
		}

		if(ip->flag & LIB3270_IO_FLAG_WRITE)
		{
			FD_SET(ip->fd, &wfds);
			maxSock = max(ip->fd,maxSock);
		}

		if(ip->flag & LIB3270_IO_FLAG_EXCEPTION)
		{
			FD_SET(ip->fd, &xfds);
			maxSock = max(ip->fd,maxSock);
		}
	}

	if (block)
	{
		if (hSession->timeouts != TN)
		{
			ms_ts(&now);
			if (now > hSession->timeouts->ts)
				tmo = 0;
			else
				tmo = hSession->timeouts->ts - now;
		}
		else
		{
			// Block for 1 second (at maximal)
			tmo = 1000;
		}
	}
	else
	{
		tmo = 1000;
	}

	if(maxSock)
	{
		struct timeval tm;

		tm.tv_sec 	= 0;
		tm.tv_usec	= tmo;

		int ns = select(maxSock+1, &rfds, &wfds, &xfds, &tm);

		if (ns < 0 && errno != EINTR)
		{
			lib3270_popup_dialog(	hSession,
									LIB3270_NOTIFY_ERROR,
									_( "Network error" ),
									_( "Select() failed when processing for events." ),
									lib3270_win32_strerror(WSAGetLastError()));
		}
		else
		{
			for (ip = hSession->inputs; ip != (input_t *) NULL; ip = ip->next)
			{
				if((ip->flag & LIB3270_IO_FLAG_READ) && FD_ISSET(ip->fd, &rfds))
				{
					(*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_READ,ip->userdata);
					processed_any = True;
					if (hSession->inputs_changed)
						goto retry;
				}

				if ((ip->flag & LIB3270_IO_FLAG_WRITE) && FD_ISSET(ip->fd, &wfds))
				{
					(*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_WRITE,ip->userdata);
					processed_any = True;
					if (hSession->inputs_changed)
						goto retry;
				}

				if ((ip->flag & LIB3270_IO_FLAG_EXCEPTION) && FD_ISSET(ip->fd, &xfds))
				{
					(*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_EXCEPTION,ip->userdata);
					processed_any = True;
					if (hSession->inputs_changed)
						goto retry;
				}
			}
		}
	}
	else if(block)
	{
		Sleep(tmo);
	}


#else

	events = 0;

	FD_ZERO(&rfds);
	FD_ZERO(&wfds);
	FD_ZERO(&xfds);

	for (ip = hSession->inputs; ip != (input_t *)NULL; ip = ip->next)
	{
		if(ip->flag & LIB3270_IO_FLAG_READ)
		{
			FD_SET(ip->fd, &rfds);
			events++;
		}

		if(ip->flag & LIB3270_IO_FLAG_WRITE)
		{
			FD_SET(ip->fd, &wfds);
			events++;
		}

		if(ip->flag & LIB3270_IO_FLAG_EXCEPTION)
		{
			FD_SET(ip->fd, &xfds);
			events++;
		}
	}

	if (block)
	{
		if (hSession->timeouts != TN)
		{
			(void) gettimeofday(&now, (void *)NULL);
			twait.tv_sec = hSession->timeouts->tv.tv_sec - now.tv_sec;
			twait.tv_usec = hSession->timeouts->tv.tv_usec - now.tv_usec;
			if (twait.tv_usec < 0L) {
				twait.tv_sec--;
				twait.tv_usec += MILLION;
			}
			if (twait.tv_sec < 0L)
				twait.tv_sec = twait.tv_usec = 0L;
			tp = &twait;
		}
		else
		{
			twait.tv_sec = 1;
			twait.tv_usec = 0L;
			tp = &twait;
		}
	}
	else
	{
		twait.tv_sec  = 1;
		twait.tv_usec = 0L;
		tp = &twait;

		if(!events)
			return processed_any;
	}

	ns = select(FD_SETSIZE, &rfds, &wfds, &xfds, tp);

	if (ns < 0 && errno != EINTR)
	{
		lib3270_popup_dialog(	hSession,
								LIB3270_NOTIFY_ERROR,
								_( "Network error" ),
								_( "Select() failed when processing for events." ),
								"%s",
								strerror(errno));
	}
	else
	{
		for (ip = hSession->inputs; ip != (input_t *) NULL; ip = ip->next)
		{
			if((ip->flag & LIB3270_IO_FLAG_READ) && FD_ISSET(ip->fd, &rfds))
			{
				(*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_READ,ip->userdata);
				processed_any = True;
				if (hSession->inputs_changed)
					goto retry;
			}

			if((ip->flag & LIB3270_IO_FLAG_WRITE) && FD_ISSET(ip->fd, &wfds))
			{
				(*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_WRITE,ip->userdata);
				processed_any = True;
				if (hSession->inputs_changed)
					goto retry;
			}

			if((ip->flag & LIB3270_IO_FLAG_EXCEPTION) && FD_ISSET(ip->fd, &xfds))
			{
				(*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_EXCEPTION,ip->userdata);
				processed_any = True;
				if (hSession->inputs_changed)
					goto retry;
			}
		}
	}

#endif

	// See what's expired.
	if (hSession->timeouts != TN)
	{
#if defined(_WIN32)
		struct timeout *t;
		ms_ts(&now);
#else
		struct timeout *t;
		(void) gettimeofday(&now, (void *)NULL);
#endif

		while ((t = hSession->timeouts) != TN)
		{
#if defined(_WIN32)
			if (t->ts <= now)
#else
			if (t->tv.tv_sec < now.tv_sec ||(t->tv.tv_sec == now.tv_sec && t->tv.tv_usec < now.tv_usec))
#endif
			{
				hSession->timeouts = t->next;
				t->in_play = True;
				(*t->proc)(t->session);
				processed_any = True;
				lib3270_free(t);
			} else
				break;
		}
	}

	if (hSession->inputs_changed)
		goto retry;

	return processed_any;

}

static int internal_wait(H3270 *hSession, int seconds)
{
	time_t end = time(0) + seconds;

	while(time(0) < end)
	{
		lib3270_main_iterate(hSession,1);
	}

	return 0;
}

static void internal_ring_bell(H3270 *session)
{
	return;
}

/* External entry points */

void * AddTimeOut(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session))
{
	void *timer;
	CHECK_SESSION_HANDLE(session);
	timer = add_timeout(session,interval_ms,proc);
	trace("Timeout %p created with %ld ms",timer,interval_ms);
	return timer;
}

void RemoveTimeOut(H3270 *session, void * timer)
{
	if(!timer)
		return;

	trace("Removing timeout %p",timer);
	return remove_timeout(session, timer);
}

void x_except_on(H3270 *h)
{
	int reading = (h->ns_read_id != NULL);

	debug("%s",__FUNCTION__);
	if(h->ns_exception_id)
		return;

	if(reading)
		lib3270_remove_poll(h,h->ns_read_id);

	h->ns_exception_id = lib3270_add_poll_fd(h,h->sock,LIB3270_IO_FLAG_EXCEPTION,net_exception,0);

	if(reading)
		h->ns_read_id = lib3270_add_poll_fd(h,h->sock,LIB3270_IO_FLAG_READ,net_input,0);
	debug("%s",__FUNCTION__);

}

void remove_input_calls(H3270 *session)
{
	if(session->ns_read_id)
	{
		lib3270_remove_poll(session,session->ns_read_id);
		session->ns_read_id	= NULL;
	}
	if(session->ns_exception_id)
	{
		lib3270_remove_poll(session,session->ns_exception_id);
		session->ns_exception_id = NULL;
	}
	if(session->ns_write_id)
	{
		lib3270_remove_poll(session,session->ns_write_id);
		session->ns_write_id = NULL;
	}
}

LIB3270_EXPORT void lib3270_register_time_handlers(void * (*add)(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session)), void (*rm)(H3270 *session, void *timer))
{
	if(add)
		add_timeout = add;

	if(rm)
		remove_timeout = rm;

}

LIB3270_EXPORT void lib3270_register_fd_handlers(void * (*add)(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata), void (*rm)(H3270 *, void *id)) {
	if(add)
		add_poll = add;

	if(rm)
		remove_poll = rm;
}

LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk)
{
	if(!cbk)
		return EINVAL;

	if(cbk->sz != sizeof(struct lib3270_callbacks))
		return EINVAL;

	lib3270_register_time_handlers(cbk->AddTimeOut,cbk->RemoveTimeOut);
	lib3270_register_fd_handlers(cbk->add_poll,cbk->remove_poll);

	if(cbk->Wait)
		wait = cbk->Wait;

	if(cbk->event_dispatcher)
		event_dispatcher = cbk->event_dispatcher;

	if(cbk->ring_bell)
		ring_bell = cbk->ring_bell;

	return 0;

}

LIB3270_EXPORT void lib3270_main_iterate(H3270 *hSession, int block)
{
	CHECK_SESSION_HANDLE(hSession);
	event_dispatcher(hSession,block);
}

LIB3270_EXPORT int lib3270_wait(H3270 *hSession, int seconds)
{
	wait(hSession,seconds);
	return 0;
}

LIB3270_EXPORT int lib3270_wait_for_ready(H3270 *hSession, int seconds)
{
	time_t	end = time(0)+seconds;

	event_dispatcher(hSession,0);

	if(!lib3270_lock_status(hSession))
		return 0;

	while(time(0) < end)
	{
		event_dispatcher(hSession,1);

		if(!lib3270_lock_status(hSession))
			return 0;

		if(!lib3270_connected(hSession))
			return ENOTCONN;

	}

	return ETIMEDOUT;
}


LIB3270_EXPORT void lib3270_ring_bell(H3270 *session)
{
	CHECK_SESSION_HANDLE(session);
	if(lib3270_get_toggle(session,LIB3270_TOGGLE_BEEP))
		ring_bell(session);
}