summaryrefslogtreecommitdiff
path: root/other/zylyx/src/proxy.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/zylyx/src/proxy.c')
-rw-r--r--other/zylyx/src/proxy.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/other/zylyx/src/proxy.c b/other/zylyx/src/proxy.c
new file mode 100644
index 0000000..5f2fde1
--- /dev/null
+++ b/other/zylyx/src/proxy.c
@@ -0,0 +1,206 @@
1/* zylyx - file find
2 *
3 * proxy routines
4 *
5 * by team teso
6 */
7
8#include <unistd.h>
9#include <pthread.h>
10#include <semaphore.h>
11#include <string.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include "common.h"
15#include "network.h"
16#include "screen.h"
17#include "proxy.h"
18#include "zylyx.h"
19
20
21void
22prx_fire (proxy *mp, result *result_r, pthread_mutex_t *result_m,
23 sem_t *permit_action, sem_t *client_action)
24{
25 pthread_t tid;
26 scan_t *new = xcalloc (1, sizeof (scan_t));
27 int n;
28
29 new->proxy = mp;
30 new->result_r = result_r;
31 new->result_m = result_m;
32 new->permit_action = permit_action;
33 new->client_action = client_action;
34
35 n = pthread_create (&tid, NULL, (void *) prx_scan, (void *) new);
36
37 return;
38}
39
40
41int
42prx_findfile (scan_t *sc)
43{
44 int n, m, linecount;
45 int prx_fd;
46 struct sockaddr_in csa;
47 char *readline;
48
49 prx_fd = net_connect (&csa, sc->proxy->host, sc->proxy->port, NULL, 0, 20);
50 if (prx_fd == -1) {
51 scr_rprint (sc->proxy->x, sc->proxy->y, ":04-");
52 return (-1);
53 }
54
55 scr_rprint (sc->proxy->x, sc->proxy->y, ":02c");
56 net_write (prx_fd, "GET %s HTTP/1.0\n\n", sc->proxy->file);
57
58 scr_rprint (sc->proxy->x, sc->proxy->y, ":02g");
59
60 for (linecount = 0 ;
61 (((n = net_rlineta (prx_fd, &readline, 30)) > 0) &&
62 linecount < 10) ;
63 linecount++)
64 {
65 int p;
66
67 scr_rprint (sc->proxy->x, sc->proxy->y, ":02r");
68
69 m = sscanf (readline, "HTTP/1.0 %d", &p);
70 free (readline);
71 if (m != 1) {
72 scr_rprint (sc->proxy->x, sc->proxy->y, ":03j");
73 } else if (p < 200 || p >= 300) {
74 scr_rprint (sc->proxy->x, sc->proxy->y, ":04f");
75 close (prx_fd);
76 } else {
77 close (prx_fd);
78 return (1);
79 }
80 }
81
82 if (n <= 0)
83 scr_rprint (sc->proxy->x, sc->proxy->y, ":04t");
84 return (0);
85}
86
87
88void *
89prx_scan (scan_t *sc)
90{
91 int n;
92
93 /* don't mess with the system resources, else we get stuck after like
94 * 1050's proxy fires =)
95 */
96 pthread_detach (pthread_self ());
97 scr_rprint (sc->proxy->x, sc->proxy->y, ":05o");
98
99 /* scan the proxy for the file, oh yeah
100 */
101 n = prx_findfile (sc);
102
103 /* post the result to the main thread using shared process memory
104 * and semaphores for activation
105 */
106 sem_wait (sc->permit_action);
107 pthread_mutex_lock (sc->result_m);
108 switch (n) {
109 case (-1):
110 case (0):
111 /* file not found or connection / proxy error
112 */
113
114 sc->result_r->found = 0;
115 break;
116 case (1):
117 /* file successfully found
118 */
119
120 sc->result_r->found = 1;
121 sc->result_r->proxy_host = sc->proxy->host;
122 sc->result_r->proxy_port = sc->proxy->port;
123 sc->result_r->file = sc->proxy->file;
124 scr_rprint (sc->proxy->x, sc->proxy->y, ":01!");
125 break;
126
127 default:
128 break;
129 }
130 sem_post (sc->client_action);
131 pthread_mutex_unlock (sc->result_m);
132
133 free (sc);
134
135 pthread_exit (NULL);
136
137 return (NULL); /* gcc eat that ;*/
138}
139
140
141proxy **
142prx_load (char *filename, int *pc)
143{
144 FILE *fp;
145 proxy **pl;
146 long int n, c;
147
148 *pc = n = prx_count (filename);
149 pl = xcalloc (n + 2, sizeof (proxy *));
150 pl[n] = NULL; /* EOA */
151
152 fp = fopen (filename, "r");
153 if (fp == NULL)
154 exit (EXIT_FAILURE);
155
156 for (c = 0; c < n; ++c) {
157 pl[c] = prx_read (fp);
158 if (pl[c] == NULL) {
159 c--;
160 n--;
161 }
162// printf ("%s:%hu\n", pl[c]->host, pl[c]->port);
163 }
164
165 return (pl);
166}
167
168
169proxy *
170prx_read (FILE *fp)
171{
172 proxy *prx = xcalloc (1, sizeof (proxy));
173 char buf[1024];
174 int n;
175
176 fgets (buf, sizeof (buf) - 1, fp);
177 n = net_parseip (buf, &prx->host, &prx->port);
178 if (n == 0) {
179 free (prx);
180 prx = NULL;
181 }
182
183 return (prx);
184}
185
186
187long int
188prx_count (char *filename)
189{
190 FILE *fp;
191 long int n;
192 char buf[1024];
193
194 fp = fopen (filename, "r");
195 if (fp == NULL)
196 exit (EXIT_FAILURE);
197
198 for (n = 0; fgets (buf, sizeof (buf), fp) != NULL; ++n)
199 ;
200
201 fclose (fp);
202
203 return (n);
204}
205
206