summaryrefslogtreecommitdiff
path: root/other/zylyx/src/zylyx.c
blob: 5196c0f7a4f1ce11d6aa942a33dcdcae4b7c16da (plain)
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
/* zylyx - file find ;-)
 *
 * by team teso
 */

#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <slang.h>
#include "common.h"
#include "screen.h"
#include "proxy.h"
#include "zylyx.h"

#define	MAX_PROC	16

int	win_y;

int
main (int argc, char **argv)
{
	proxy	**prx_list;
	int	proxy_count;

	if (argc != 2) {
		printf ("usage: %s <url>\n", argv[0]);
		exit (EXIT_FAILURE);
	}

	scr_init ();
	scr_prg_init ();

	prx_list = prx_load ("proxy-list", &proxy_count);
	if (prx_list == NULL) {
		scr_exit ();
		exit (EXIT_FAILURE);
	}

	zyl_assign_prx (prx_list);
	zyl_main (prx_list, proxy_count, argv[1]);

	scr_exit ();

	exit (EXIT_SUCCESS);
}


void
zyl_main (proxy **pl, int proxycount, char *file)
{
	int		n;
	struct timeval	last_conn = { 0, 0 };
	sem_t		permit_action,	/* permit a client response */
			client_action;	/* client responded */
	pthread_mutex_t	subcount_m;	/* subcounter mutex */
	int		subcount = 0;
	int		proxy_ptr = 0;
	pthread_mutex_t	result_m;	/* result data is locked hehe */
	result		result_r;	/* result data =) */

	sem_init (&permit_action, 0, 0);
	sem_init (&client_action, 0, 0);
	pthread_mutex_init (&subcount_m, NULL);
	pthread_mutex_init (&result_m, NULL);

	sem_post (&permit_action);

	/* fire clients and take events (event loop)
	 */

	while (proxy_ptr < proxycount) {
		pthread_mutex_lock (&subcount_m);
		if (subcount < MAX_PROC) {
			unsigned long int	tp;

			pl[proxy_ptr]->file = file;

			/* anti flood mechanism :)
			 */
			tp = t_passed (&last_conn);
			if (tp > 0 && tp < 500000) {
				/* race condition here (find it and you'll get a hug ;)
				 */
				usleep (500000 - tp);
			}
			gettimeofday (&last_conn, NULL);
			prx_fire (pl[proxy_ptr++], &result_r, &result_m, &permit_action, &client_action);
			subcount++;
		}
		pthread_mutex_unlock (&subcount_m);

#ifdef IPC_DEBUG
		printf ("zylyx.c:%d # sem_trywait (&client_action) = %d\n", __LINE__, sem_trywait (&client_action));
		printf ("zylyx.c:%d # subcount = %d\n", __LINE__, subcount);
#endif

		/* if we cannot fire out new clients because we reached the maximum number,
		 * or if a client wants our attention we enter the result processing
		 */

		n = 1;
		if (subcount >= MAX_PROC ||
			((n = sem_trywait (&client_action)) == 0))
		{

			/* wait for client action (queued)
			 */
			if (n == 1)
				sem_wait (&client_action);

			/* lock result data
			 */
			pthread_mutex_lock (&result_m);

			/* only be verbose if the file was found
			 */
			if (result_r.found == 1) {

				/* yeah, zylyx found the file, now tell the user <g>
				 */

				scr_rprint (1, win_y, ":01! - ");
				scr_rprintf (5, win_y++, "%s %hu\n",
					result_r.proxy_host, result_r.proxy_port);
			}

			/* unlock the result data for the clients to modify,
			 * then post the permission to act ;-)
			 */
			pthread_mutex_unlock (&result_m);
			sem_post (&permit_action);

			subcount--;	/* one client quitted */
		}
	}

	while (subcount > 0) {
		sem_wait (&client_action);
		pthread_mutex_lock (&result_m);

		if (result_r.found == 1) {
			/* yeah, zylyx found the file <g>
			 */

			scr_rprint (1, win_y, ":01! - ");
			scr_rprintf (5, win_y++, "%s %hu\n",
				result_r.proxy_host, result_r.proxy_port);
		}
		pthread_mutex_unlock (&result_m);
		sem_post (&permit_action);
		subcount--;	/* one client quitted */
	}

	return;
}


void
zyl_assign_prx (proxy **pl)
{
	int	x, y;
	int	n = 0;

	x = 1;
	y = 7;

	for (y = 7; y < SLtt_Screen_Rows - 2; ++y) {
		for (x = 1; x < SLtt_Screen_Cols - 1; ++x) {
			if (pl[n] == NULL) {
				scr_build_box (0, 6, SLtt_Screen_Cols - 1, y + 1);
				scr_build_box (0, y + 1, SLtt_Screen_Cols - 1, SLtt_Screen_Rows - 1);
				win_y = y + 2;
				SLsmg_refresh ();
				return;
			}
			pl[n]->x = x;
			pl[n]->y = y;
			scr_rprint (x, y, ":16.");
			n++;
		}
	}
 
	return;
}