Ejemplo n.º 1
0
static void addBlacklist(t_original_network_id onid, t_transport_stream_id tsid, t_service_id sid)
{
	t_channel_id channel_id =
		CREATE_CHANNEL_ID(sid, onid, tsid);
	t_channel_id mask =
		CREATE_CHANNEL_ID(
				(sid ? 0xFFFF : 0), (onid ? 0xFFFF : 0), (tsid ? 0xFFFF : 0)
				);
	if (!checkBlacklist(channel_id))
	{
		xprintf("Add Channel Blacklist for channel 0x%012" PRIx64 ", mask 0x%012" PRIx64 "\n", channel_id, mask);
		ChannelBlacklist *node = new ChannelBlacklist;
		node->chan = channel_id;
		node->mask = mask;
		node->next = CurrentBlacklist;
		CurrentBlacklist = node;
	}
}
Ejemplo n.º 2
0
void* connection_handler(void* socket_desc) {
    
    while(1) {  // main accept() loop
        
        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            continue;
        }
        
        inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
        printf("server: got connection from %s\n", s);
        
        if (!fork()) { // this is the child process
            close(sockfd); // child doesn't need the listener
            
            // recieve message from client
            if ((numBytes = recv(new_fd, HTTP_REQUEST, MAXDATASIZE-1, 0)) == -1) {
                perror("recv");
                exit(1);
            }
            
            // parse GET request -------------------------------------------------------------------------
            
            printf("\n%s\n", HTTP_REQUEST);
            
            char request_type[4];
            char filename[1024];
            const char ch[] = ":";
            
            char *portTest;
            char *strptr;
            char *port;      // optional. default to 80
            
            strptr = strtok(HTTP_REQUEST, " ");
            strcpy(request_type, strptr);
            
            strptr = strtok(NULL, "//");
            
            strptr = strtok(NULL, "/");
            strcpy(host, strptr);
            
            strptr = strtok(NULL, " ");
            strcpy(filename, strptr);
            
            portTest = strpbrk(filename, ch);
            
            if (portTest) {
                newFile = strtok(filename, ":");
                printf("filename without port: %s\n", newFile);
                port = strtok(NULL, " ");
                printf("port: %s\n", port);
                
            } else {
                newFile = filename;
                port = "80";
            }
            
            // --------------------------------------------------------------------------------------------
            
            // send a 405 if request is not a GET
            if (strcmp(request_type, "GET") != 0) {
                if (send(new_fd, "405: Please send a GET request only\n", 37, 0) == -1) {
                    perror("send");
                }
                close(new_fd);
                exit(1);
            }
            
            printf("Host: %s Filename: %s \n", host, filename);
            
            // send a 403 if URI is blacklisted
            
            if (checkBlacklist(blacklist, host, filename) != 0) {
                if (send(new_fd, "403: Your requested URI is blacklisted\n", 40, 0) == -1) {
                    perror("send");
                }
                close(new_fd);
                exit(1);
            }
            
            
            // reset getaddinfo()
            if ((gr = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
                exit(1);
            }
            
            
            // open a socket and get a connection
            for (p = servinfo; p != NULL; p = p->ai_next) {
                if ((getR_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
                    perror("client: socket");
                    continue;
                }
                
                if (connect(getR_fd, p->ai_addr, p->ai_addrlen) == -1) {
                    close(getR_fd);
                    perror("client: connect");
                    continue;
                }
                
                break;
            }
            
            // error check
            if (p == NULL) {
                fprintf(stderr, "proxy: failed to connect\n");
                 exit(1);
            }
            freeaddrinfo(servinfo); // done with this structure
            
            // set up the request
            strncpy(getRequest, "GET /", 5);
            strncat(getRequest, newFile, strlen(newFile));
            strncat(getRequest, " HTTP/1.1\r\nHost: ", strlen(" HTTP/1.1\r\nHost: "));
            strncat(getRequest, host, strlen(host));
            strncat(getRequest, "\r\nConnection: close\r\n\r\n", strlen("\r\nConnection: close\r\n\r\n"));
            
            // send the request
            if (send(getR_fd, getRequest, strlen(getRequest), 0) == -1) {
                perror("send");
            }
            
            char stream[1000];
            memset(httpPage, 0, sizeof(httpPage));
            memset(stream, 0, sizeof(stream));
            int headerCheck = 1;
            
            while (1) {
                if ((numBytes = recv(getR_fd, stream, 1000, 0)) == -1) {
                    perror("recv");
                    exit(1);
                }
                // check for server error
                if (headerCheck == 1) {
                    if (stream[9] == '5') {
                        send(new_fd, "Unable to connect to host due to a Server Error.\n", 51, 0);
                        close(new_fd);
                        exit(1);
                    }
                }
                headerCheck = 0;
                
                if (numBytes == 0) { break; }
                
                strcat(httpPage, stream);
                memset(stream, 0, sizeof(stream));
                
            }
            memset(stream, 0, sizeof(stream));
            
            // print resonse to console
            printf("%s", httpPage);
            
            // send response to client
            if (send(new_fd, httpPage, sizeof(httpPage), 0) == -1) {
                perror("send");
            }
            
            close(getR_fd);
            close(new_fd);
            
            exit(0);
        }
        close(new_fd);
    }
}