summaryrefslogtreecommitdiff
blob: 0895729833942e21ef3cb3f758274c76747cc662 (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
/*
* Copyright (C) 2010 Robin H.Johnson, Ovechko Kostyantyn <fastinetserver@gmail.com>.
*
* Project: IDFetch.
* Developer: Ovechko Kostyantyn Olexandrovich (Kharkiv State Technical University of Construction and Architecture, Ukraine).
* Mentor: Robin H. Johnson (Gentoo Linux: Developer, Trustee & Infrastructure Lead).
* Mentoring organization: Gentoo Linux.
* Sponsored by GSOC 2010.
*
* This file is part of Segget.
*
* Segget is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Segget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Segget; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "proxyfetcher.h"

void *run_proxy_fetcher_server(void * ){
	try{
		int server_sockfd, client_sockfd;
		socklen_t server_len, client_len;
		struct sockaddr_in server_address;
		struct sockaddr_in client_address;
		int result;
		fd_set readfds, testfds;

		// Create and name a socket for the server:
		server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

		int on=1;
		setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

		server_address.sin_family = AF_INET;
		//server_address.sin_addr.s_addr = htonl(INADDR_ANY);
		server_address.sin_addr.s_addr = inet_addr(settings.provide_proxy_fetcher_ip.c_str());
		server_address.sin_port = htons(settings.provide_proxy_fetcher_port);
		server_len = sizeof(server_address);

		bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

		//Create a connection queue and initialize readfds to handle input from server_sockfd:
		listen(server_sockfd, 5);

		FD_ZERO(&readfds);
		FD_SET(server_sockfd, &readfds);

		//Now wait for clients and requests. Because you have passed a null pointer as the timeout parameter, no timeout will occur. The program will exit and report an error if select returns a value less than 1:
		while(1) {
			int fd;
			int nread;
			testfds = readfds;

			debug("proxyfether is waiting for connections");
			result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, (struct timeval *) 0);
//			debug("proxyfether done waiting");


			if(result < 1) {
				error_log("Error in proxyfetcher.cpp : run");
				exit(1);
			}

			//Once you know you’ve got activity, you can find which descriptor it’s on by checking each in turn using FD_ISSET:
			for(fd = 0; fd < FD_SETSIZE; fd++) {
				if(FD_ISSET(fd,&testfds)) {

				//If the activity is on server_sockfd, it must be a request for a new connection, and you add the associated client_sockfd to the descriptor set:

					if(fd == server_sockfd) {
						debug("new client - read");
						client_len = sizeof(client_address);
						client_sockfd = accept(server_sockfd,
						(struct sockaddr *)&client_address, &client_len);
						FD_SET(client_sockfd, &readfds);
						debug("adding client on fd:"+toString(client_sockfd));

						//If it isn’t the server, it must be client activity. If close is received, the client has gone away, and you remove it from the descriptor set. Otherwise, you “serve” the client as in the previous examples.
					}else{
//						debug("old client - read");
						ioctl(fd, FIONREAD, &nread);
						if(nread == 0) {
							close(fd);
							FD_CLR(fd, &readfds);
							debug("removing client on fd:"+toString(fd));
						}else{
							char buffer[100000]="";
							if (nread!=read(fd, &buffer, nread)){
								error_log("Error in proxyfetcher.cpp : run_proxy_fetcher_server(): Not all data has been read from proxy-fetcher-client");
							}
//							debug("serving client - read");
//							debug("serving client on fd"+toString(fd));
							string recv_msg=buffer;
							error_log("Received a msg from the client:"+recv_msg);
							string send_response;
//							char send_buffer[10]="";

							json_object* json_obj_distfile=json_tokener_parse(buffer);
							string distfile_name=json_object_get_string(json_object_object_get(json_obj_distfile,"name"));
							int result=request_server_pkg.find_distfile(distfile_name);
							switch (result){
								case R_PF_NOT_REQUESTED_YET:
								case R_PF_ERROR_ADDING_TO_PROXY_QUEUE: // if error - try with proxy_fetcher
								{
									result=proxy_fetcher_pkg.find_distfile(distfile_name);
									switch (result){
										case R_PF_NOT_REQUESTED_YET:{
											if (is_symlink_restricted(distfile_name)!=-1){
												debug("PROXY_FETCHER: distfile: "+distfile_name+" restricted (name matches restriting pattern");
												result=R_PF_REJECTED;
											}else{
												result=proxy_fetcher_pkg.push_back_distfile(json_obj_distfile);
											break;
											}
										}
										default: break;
									}
									break;
								}
								default: break;
							}
							send_response=toString(result);
//							if (write(sockfd, send_buffer, strlen(send_buffer))!=(int)msg.length()){
							if (write(fd, send_response.c_str(), send_response.length())!=(int)send_response.length()){
								error_log("Error in proxyfetcher.cpp: run_proxy_fetcher_server(): response msg size and sent data size are different.");
							};
						}
					}
				}
			}
		}
	}catch(...){
		error_log("Error in proxyfetcher.cpp: run_proxy_fetcher_server()");
		return (void*)1;
	}
}