예제 #1
0
파일: server.c 프로젝트: aeppert/hawkeye
static void add_client(struct server *s, int sock, struct sockaddr_storage *addr, struct frame_buffers *fbs) {
    struct client *c;
    char cbuf[INET6_ADDRSTRLEN]; // general purpose buffer for various string conversions in this function

    c = malloc(sizeof(struct client));
    memset(c, 0, sizeof(struct client));

    c->sock = sock;
    memcpy(&c->addr, addr, sizeof(struct sockaddr_storage));
    c->last_communication = gettime();
    c->current_frame = 0;
    c->current_frame_pos = 0;
    c->request_header_size = 0;
    c->request = REQUEST_INCOMPLETE;
    c->resp = NULL;
    c->resp_pos = 0;
    c->resp_len = 0;
    c->fb = NULL;
    c->static_file = NULL;

    if (s->ssl_ctx != NULL) {
        c->ssl = SSL_new(s->ssl_ctx);
        SSL_set_fd(c->ssl, c->sock);

        if (SSL_accept(c->ssl) < 1) {
            log_itf(LOG_ERROR, "Error occured doing the SSL handshake: %s", ERR_error_string(ERR_get_error(), NULL));
            free(c);
            return;
        }
    }

    s->clients[c->sock] = c;

    log_itf(LOG_INFO, "Client connected from %s.", ntop(&c->addr, cbuf, sizeof(cbuf)));
}
예제 #2
0
파일: nutscan-ip.c 프로젝트: AlexLov/nut
/* return the next IP
return NULL if there is no more IP
*/
char * nutscan_ip_iter_inc(nutscan_ip_iter_t * ip)
{
	char host[SMALLBUF];

	if( ip->type == IPv4 ) {
		/* Check if this is the last address to scan */
		if(ip->start.s_addr == ip->stop.s_addr) {
			return NULL;
		}
		/* increment the address (need to pass address in host
		   byte order, then pass back in network byte order */
		ip->start.s_addr = htonl((ntohl(ip->start.s_addr)+1));

		if( ntop(&ip->start, host, sizeof(host)) != 0 ) {
			return NULL;
		}
	
		return strdup(host);
	}
	else {
		/* Check if this is the last address to scan */
		if( memcmp(&ip->start6.s6_addr, &ip->stop6.s6_addr,
				sizeof(ip->start6.s6_addr)) == 0 ) {
			return NULL;
		}

		increment_IPv6(&ip->start6);
		if( ntop6(&ip->start6, host, sizeof(host)) != 0 ) {
			return NULL;
		}

		return strdup(host);
	}
}
예제 #3
0
char *ntop(const char * sastr)
{
    const struct sockaddr * sa = (const struct sockaddr *)sastr;

    assert(sa);
    if(!sa)
    {
        return xstrdup("DNX Error:  Address Uknown or Corrupt! ");
    }
    char * buf = NULL;

    switch(sa->sa_family)
    {
        case AF_INET:
            buf = (char *)xcalloc(INET_ADDRSTRLEN +1,sizeof(char));
            if(buf)
            {
                inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),buf, INET_ADDRSTRLEN);
            }
        break;

        case AF_INET6:
            buf = (char *)xcalloc(INET6_ADDRSTRLEN +1,sizeof(char));
            if(buf)
            {
                inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),buf, INET6_ADDRSTRLEN);
            }
        break;

        default:
            buf = xstrdup("127.0.0.1");
        break;
    }

    if(!buf)
    {
        dnxLog("ntop: out of memory, sleeping for 1 second before  trying again");
        sleep(1);
        return(ntop((char *)sa));
    }else{
        return buf;
    }
}
예제 #4
0
파일: server.c 프로젝트: aeppert/hawkeye
static void remove_client(struct server *s, struct client *c) {
    char cbuf[INET6_ADDRSTRLEN];
    log_itf(LOG_INFO, "Disconneting client from %s.", ntop(&c->addr, cbuf, sizeof(cbuf)));
    
    s->clients[c->sock] = NULL;
    close(c->sock);
    
    if (c->static_file != NULL) {
        fclose(c->static_file);
    }

    if (c->resp != NULL) {
        free(c->resp);
    }
    
    if (c->ssl) {
        SSL_free(c->ssl);
    }

    free(c);
}
예제 #5
0
int main (int argc, char **argv) {
    struct hwa_info *hwa, *hwahead;
    struct sockaddr *sa;
    unsigned char *ptr;
    int i, prflag;

    for (hwahead = hwa = get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) {

        printf("%s :%s", hwa->if_name, ((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n");

        if ( (sa = hwa->ip_addr) != NULL)
            printf("         IP addr = %s\n", ntop(sa));

        prflag = 0;
        i = 0;
        do {
            if (hwa->if_haddr[i] != '\0') {
                prflag = 1;
                break;
            }
        } while (++i < IFHWADDRLEN);

        if (prflag) {
            printf("         HW addr = ");
            ptr = hwa->if_haddr;
            i = IFHWADDRLEN;
            do {
                printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
            } while (--i > 0);
        }

        printf("\n         interface index = %d\n\n", hwa->if_index);
    }

    free_hwa_info(hwahead);
    exit(0);
}
예제 #6
0
파일: server.c 프로젝트: aeppert/hawkeye
static void handle_request(struct server *s, struct client *c, struct frame_buffers *fbs) {
    int index;
    char cbuf[INET6_ADDRSTRLEN]; // general purpose buffer for various string conversions in this function
    char tmp_filename[PATH_MAX + 1], filename[PATH_MAX + 1]; // Need 2 of these for realpath()
    char resp_head[sizeof(HTTP_STATIC_FILE_HEADERS_TMPL) + 256];
    struct http_request req;

    parse_request(c->request_headers, &req);
    if (req.protocol_version == NULL) {
        set_client_response(c, REQUEST_BAD, HTTP_BAD_REQUEST);
        return;
    }

    log_itf(LOG_INFO, "Client at %s requested %s %s%s%s.", 
        ntop(&c->addr, cbuf, sizeof(cbuf)),
        req.method,
        req.path,
        *req.query_string != '\0' ? "?" : "",
        req.query_string
    );
    
    if (strcmp(req.method, "GET") != 0 || strcmp(req.protocol, "HTTP") != 0) {
        return set_client_response(c, REQUEST_BAD, HTTP_BAD_REQUEST);
    }

    if (strncmp(req.path, "/img/", 5) != 0) {
        if (!check_http_auth(req.authorization, s->auth)) {
            return set_client_response(c, REQUEST_AUTH_REQUIRED, HTTP_AUTH_REQUIRED);
        }
    }

    // /stream/
    if (strncmp(req.path, "/stream/", strlen("/stream/")) == 0) {
        if (strcmp(req.path, "/stream/info") == 0) {
            set_client_response(c, REQUEST_STREAM_INFO, s->stream_info);
        }
        else {
            // /stream/0, /stream/1, etc
            index = atoi(&req.path[strlen("/stream/")]);

            if (index < 0 || index >= fbs->count) {
                set_client_response(c, REQUEST_NOT_FOUND, HTTP_NOT_FOUND);
            }
            else {
                set_client_response(c, REQUEST_STREAM, STREAM_HEADER);
                
                c->fb = &fbs->buffers[index];
                c->current_frame = c->fb->current_frame;
                c->current_frame_pos = 0;
            }
        }
    }
    else {

        if (!strlen(s->static_root)) {
            return set_client_response(c, REQUEST_NOT_FOUND, HTTP_NOT_FOUND);
        }
                
        // Serving static file
        strncpy(tmp_filename, s->static_root, PATH_MAX);
        strncat(tmp_filename, req.path, PATH_MAX);
        if (tmp_filename[strlen(tmp_filename) - 1] == '/') {
            strncat(tmp_filename, INDEX_FILE_NAME, PATH_MAX);
        }

        // This also checks if the file exists
        if (NULL == realpath(tmp_filename, filename) ||
            strncmp(filename, s->static_root, strlen(s->static_root)) != 0) {
                return set_client_response(c, REQUEST_NOT_FOUND, HTTP_NOT_FOUND);

        }

        // Fill in response template
        snprintf(resp_head, sizeof(resp_head), HTTP_STATIC_FILE_HEADERS_TMPL,
            (long) file_size(filename),
            get_mime_type(filename)
        );

        set_client_response(c, REQUEST_STATIC_FILE, resp_head);
        c->static_file = fopen(filename, "rb");
    }
    
}
예제 #7
0
파일: nutscan-ip.c 프로젝트: AlexLov/nut
/* Return the first ip or NULL if error */
char * nutscan_ip_iter_init(nutscan_ip_iter_t * ip, const char * startIP, const char * stopIP)
{
	int addr;
	int i;
	struct addrinfo hints;
	struct addrinfo *res;
	struct sockaddr_in * s_in;
	struct sockaddr_in6 * s_in6;
	char host[SMALLBUF];

	if( startIP == NULL ) {
		return NULL;
	}

	if(stopIP == NULL ) {
		stopIP = startIP;
	}

	memset(&hints,0,sizeof(struct addrinfo));
	hints.ai_family = AF_INET;

	ip->type = IPv4;
	/* Detecting IPv4 vs IPv6 */
	if(getaddrinfo(startIP,NULL,&hints,&res) != 0) {
		/*Try IPv6 detection */
		ip->type = IPv6;
		hints.ai_family = AF_INET6;
		if(getaddrinfo(startIP,NULL,&hints,&res) != 0) {
			fprintf(stderr,"Invalid address : %s\n",startIP);
			return NULL;
		}
		
		s_in6 = (struct sockaddr_in6 *)res->ai_addr;
		memcpy(&ip->start6,&s_in6->sin6_addr,sizeof(struct in6_addr));
		freeaddrinfo(res);
	}
	else {
		s_in = (struct sockaddr_in *)res->ai_addr;
		ip->start = s_in->sin_addr;
		freeaddrinfo(res);
	}

	/* Compute stop IP */
        if( ip->type == IPv4 ) {
		hints.ai_family = AF_INET;
		if(getaddrinfo(stopIP,NULL,&hints,&res) != 0) {
                        fprintf(stderr,"Invalid address : %s\n",stopIP);
                        return NULL;
                }

		s_in = (struct sockaddr_in *)res->ai_addr;
		ip->stop = s_in->sin_addr;
		freeaddrinfo(res);
        }
        else {
		hints.ai_family = AF_INET6;
		if(getaddrinfo(stopIP,NULL,&hints,&res) != 0) {
                        fprintf(stderr,"Invalid address : %s\n",stopIP);
                        return NULL;
                }
		s_in6 = (struct sockaddr_in6 *)res->ai_addr;
		memcpy(&ip->stop6,&s_in6->sin6_addr,sizeof(struct in6_addr));
		freeaddrinfo(res);
        }

        /* Make sure start IP is lesser than stop IP */
        if( ip->type == IPv4 ) {
                if( ntohl(ip->start.s_addr) > ntohl(ip->stop.s_addr) ) {
                        addr = ip->start.s_addr;
                        ip->start.s_addr = ip->stop.s_addr;
                        ip->stop.s_addr = addr;
                }

		if( ntop(&ip->start, host, sizeof(host)) != 0 ) {
			return NULL;
		}

		return strdup(host);
        }
        else { /* IPv6 */
                for( i=0; i<16; i++ ) {
                        if( ip->start6.s6_addr[i] !=ip->stop6.s6_addr[i] ) {
                                if(ip->start6.s6_addr[i]>ip->stop6.s6_addr[i]){
                                        invert_IPv6(&ip->start6,&ip->stop6);
                                }
                                break;
                        }
                }

		if( ntop6(&ip->start6, host, sizeof(host)) != 0 ) {
			return NULL;
		}

		return strdup(host);
        }


}
예제 #8
0
int gen_op ( )
{
    Matrix	a;
    Matrix	b;
    void       *ptr;
    descriptor *d;
    descriptor *v;
    descriptor *var;
    descriptor *index;
    descriptor *vector;
    descriptor	temp;
    double	value;
    Address	increment;
    Array	arr;
    int		fail;
    unsigned	offset;
    unsigned	i;
    unsigned	c;
    unsigned	r;


    index = ntop (0);
    vector = ntop (1);
    var = ntop (2);

    offset = fetch (pc ++).ival;


    if (D_Type (index) == T_Double) {
        if (!assignable (var)) {
            TypeError ("cannot assign to", NULL, var, NULL, F_False);
            return 1;
        }

        d = &temp;
        D_Type    (d) = T_Null;
        D_Temp    (d) = F_False;
        D_Trapped (d) = F_False;
        D_Pointer (d) = NULL;

        v = CoerceData (vector, T_Double);
        AssignData (d, &v);
        RecycleData (v);
        D_Temp (d) = F_False;

        d_printf ("d = %s %p\n", D_TypeName (d), D_Pointer (d));

        switch (D_Type (d)) {
        case T_Double:
        case T_Matrix:
        case T_Array:
        case T_Null:
            break;


        default:
            TypeError ("cannot index", NULL, d, NULL, F_False);
            return 1;
        }

        *vector = *d;

        D_Type (index) = T_Row;
        D_Row (index) = 0;
    }

    d_printf ("vector = %s %p\n", D_TypeName (vector), D_Pointer (vector));
    var = deref (var);
    fail = F_False;


    switch (D_Type (vector)) {
    case T_Double:
        if (D_Row (index) ++ == 0)
            AssignData (var, &vector);
        else
            fail = F_True;
        break;


    case T_Matrix:
        a = D_Matrix (vector);
        d = &temp;
        D_Temp	  (d) = F_False;
        D_Trapped (d) = F_False;

        if (Mrows (a) == 1) {
            if (++ D_Row (index) <= Mcols (a)) {
                D_Type   (d) = T_Double;
                D_Double (d) = &value;
                value = mdata (a, 1, D_Row (index));
                AssignData (var, &d);
            } else
                fail = F_True;

        } else if (Mcols (a) == 1) {
            if (++ D_Row (index) <= Mrows (a)) {
                D_Type   (d) = T_Double;
                D_Double (d) = &value;
                value = mdata (a, D_Row (index), 1);
                AssignData (var, &d);
            } else
                fail = F_True;

        } else {
            if (++ D_Row (index) <= Mcols (a)) {
                d_printf ("indexing matrix\n");
                r = Mrows (a);
                c = D_Row (index);

                FreeData (var);
                CreateData (var, NULL, NULL, T_Matrix, r, 1);
                D_Temp (var) = F_False;
                b = D_Matrix (var);

                for (i = 1; i <= r; i ++)
                    sdata (b, i, 1) = mdata (a, i, c);
            } else
                fail = F_True;
        }
        break;


    case T_Array:
        arr = D_Array (vector);
        d = &temp;

        if (++ D_Row (index) <= arr -> length) {
            increment = D_Row (index) * arr -> elt_size;
            ptr = (void *) ((char *) arr -> ptr + increment);

            D_Type    (d) = arr -> type;
            D_Temp    (d) = F_False;
            D_Trapped (d) = F_False;
            D_Pointer (d) = ptr;
            AssignData (var, &d);
        } else
            fail = F_True;
        break;


    case T_Null:
        fail = F_True;
        break;
    }


    /* After assignment the variable is certainly not temporary.  Its trapped
       status remains as before: if it was trapped then AssignData() called
       the trap handler which didn't change the status.  If it wasn't then
       AssignData() left the status alone. */

    D_Temp (var) = F_False;

    if (fail == F_True) {
        pop ( );
        FreeData (pop ( ));		/* free the privately owned vector */
        pop ( );

        d = push ( );
        D_Type	  (d) = T_Null;
        D_Temp	  (d) = F_False;
        D_Trapped (d) = F_False;
        D_Pointer (d) = NULL;

        pc += offset;
        d_printf ("failing\n");
    }

    return 0;
}