void http_accept_request_socket(http_conf_t * conf, int fd) { int confd ; struct sockaddr addr; struct sockaddr_in addrIn; socklen_t addLen = sizeof(struct sockaddr ); while( (confd = accept(fd, &addr, &addLen)) && confd > 0) { pool_t *p = (pool_t *)pool_create(); http_connect_t * con; epoll_extra_data_t *data_ptr; addrIn = *((struct sockaddr_in *) &addr); data_ptr = (epoll_extra_data_t *)palloc(p, sizeof(epoll_extra_data_t)); con = (http_connect_t *) palloc(p, sizeof(http_connect_t));//换成初始化函数, con->p= p; con->fd = confd; con->in = (request *)request_init(p); con->out = (response *)response_init(p); /*char *ip = NULL; if(addrIn.sin_addr.s_addr) { ip = inet_ntoa(addrIn.sin_addr); } if(ip) { //con->in->clientIp = (string *) string_init_from_str(p, ip, strlen(ip)); }*/ con->next_handle = accept_handler; data_ptr->type = SOCKFD; data_ptr->ptr = (void *) con; epoll_add_fd(conf->epfd, confd, EPOLL_R, (void *)data_ptr);//对epoll data结构指向的结构体重新封装,分websit //data struct , connect data struct , file data struct , } }
static gint collect_success(VmonRequest *req) { int j = 0; VmonResponse res; response_init(&res); VmChecks checks; checks.disk_usage_perc = req->ctx->conf.disk_usage_perc; for (j = 0; j < req->records_num; j++) { VmInfo vm; vminfo_init(&vm); vminfo_parse(&vm, req->records[j]); /* FIXME */ response_open(&res); vminfo_send_events(&vm, &checks, res.out); if (!req->ctx->conf.events_only) { response_begin(&res, req->sr.uuid); vminfo_print_json(&vm, res.out); response_finish(&res); } response_close(&res, req->ctx->out); vminfo_free(&vm); } virDomainStatsRecordListFree(req->records); return 0; }
void make_service(uint8_t symbol){ if(symbol == EdConf.clean_char_symb){ if(resp_ptr > 0){ cursor_shift(LEFT); lcd_putc(' '); cursor_shift(LEFT); if(btn_old && !is_service_symbol(symbol))return; //if btn not pushed in response yet response_rm_char(); } if(resp_ptr >= disp_line_length){ lcd_init_first(); for(uint8_t i=(resp_ptr-disp_line_length); i<resp_ptr; i++){ lcd_putc(response[i]); } } exit_status=ExitContinue; } if(symbol == EdConf.exit_symb_ok){ turn_off_cursor(); response_push('\0'); exit_status=ExitOk; } if(symbol == EdConf.exit_symb_discard){ turn_off_cursor(); response_init(EdConf.old_response, EdConf.resp_size); exit_status=ExitDiscard; } }
void init_editor(EditorConf config){ press_counter=0; resp_ptr=0; btn_old=0; duration_old=0; timer_id=0; exit_status=ExitContinue; EdConf = config; lcd_init_first(); turn_on_cursor(); response_init(EdConf.old_response, EdConf.resp_size); lcd_init_resp(EdConf.old_response); }
int accept_handler(http_conf_t *conf, http_connect_t *con, struct epoll_event *ev) { con->in = (request *)request_init(con->p); if(read_header(con) == 0) { if(con->in->header == NULL || con->in->header->used <= 0) { send_bad_request(con->fd); epoll_del_fd(conf->epfd, ev); close(con->fd); pool_destroy(con->p); return -1; } parse_header(con); //list_buffer_to_lower(con->in->header); con->next_handle = authorized_handle; virtual_port_match(conf, con); epoll_edit_fd(conf->epfd, ev, EPOLL_W); /*是否需要登录 *提交方式 *主页匹配 *压缩 *发送数据 */ //http_connect_t *con; //con = ev[count].data.ptr; con->out = (response *)response_init(con->p); while(con->next_handle != NULL && con->next_handle(conf, con) == 0){ } epoll_del_fd(conf->epfd, ev); } return 0; }
void run (int worker_id, int sock_fd, struct sockaddr addr, socklen_t addr_len) { headers_parse (sock_fd); if (headers_is_error (headers_get_state ()) || !headers_has_request()) { goto hangup; } char *name = get_request (1); char *strip = name; char response_buffer[255]; while (*strip == '.' || *strip == '/') strip++; if (!*strip) { fprintf (stderr, "%s: No such file or directory\n", name); goto _404; } int len = strlen (strip); int file_fd; char *file = malloc (2 + len + 1); file[0] = '.'; file[1] = '/'; memcpy (file + 2, strip, len); file[len + 2] = 0; file_fd = open (file, O_RDONLY); free (file); struct stat info; if (file_fd == -1 || fstat (file_fd, &info)) { if (file_fd > 0) { close (file_fd); } file_fd = 0; perror (name); goto _404; } response_init (200); char content_length[25]; sprintf (content_length, "%d", info.st_size); response_add ("Content-Length", content_length); if (write_headers (sock_fd)) { close (file_fd); goto _404; } len = read (file_fd, response_buffer, 255); while (len > 0) { if (write (sock_fd, response_buffer, len) < 0) { perror ("write"); break; } len = read (file_fd, response_buffer, 255); } if (file_fd > 0) { close (file_fd); } goto hangup; _404: response_init (404); write_headers (sock_fd); snprintf (response_buffer, 255, "File not found: '%s'\n", name); if (write (sock_fd, response_buffer, strlen (response_buffer)) < 0) { goto hangup; } goto hangup; hangup: headers_cleanup (); hangup (sock_fd); }
int start_accept(http_conf *g) { int count; int confd ; struct epoll_event ev[MAX_EVENT]; struct epoll_event *evfd ; epoll_extra_data_t *epoll_data; struct sockaddr addr; struct sockaddr_in addrIn; socklen_t addLen = sizeof(struct sockaddr ); start_web_server(g); printf("--------------- start server\n--------------"); while(1){ count = epoll_wait(g->epfd, ev, MAX_EVENT, -1); evfd = ev; while( (confd = accept(g->fd, &addr, &addLen)) && confd > 0) { pool_t *p = (pool_t *)pool_create(); http_connect_t * con; epoll_extra_data_t *data_ptr; addrIn = *((struct sockaddr_in *) &addr); data_ptr = (epoll_extra_data_t *)palloc(p, sizeof(epoll_extra_data_t)); con = (http_connect_t *) palloc(p, sizeof(http_connect_t));//换成初始化函数, con->p= p; con->fd = confd; con->in = (request *)request_init(p); con->out = (response *)response_init(p); char *ip = NULL; if(addrIn.sin_addr.s_addr) { ip = inet_ntoa(addrIn.sin_addr); } if(ip) { con->in->clientIp = (string *) string_init_from_str(p, ip, strlen(ip)); } con->next_handle = accept_handler; data_ptr->type = SOCKFD; data_ptr->ptr = (void *) con; epoll_add_fd(g->epfd, confd, EPOLL_R, (void *)data_ptr);//对epoll data结构指向的结构体重新封装,分websit //data struct , connect data struct , file data struct , } while((evfd->events & EPOLLIN)){ if((evfd->events & EPOLLIN)) { http_connect_t * con; epoll_data = (epoll_data_t *)evfd->data.ptr; con = (http_connect_t *) epoll_data->ptr; switch(epoll_data->type) { case SOCKFD: if(con->in == NULL) { //accept_handler(g, con, evfd); epoll_edit_fd(g->epfd, evfd, EPOLL_W); //epoll_del_fd(g->epfd, evfd); } while(con->next_handle != NULL) { int ret = con->next_handle(con); if(ret == -1) { con->next_handle = NULL; epoll_del_fd(g->epfd, evfd); close(con->fd); ds_log(con, " [END] ", LOG_LEVEL_DEFAULT); pool_destroy(con->p); }else if(ret == 1) { break; } /*if(con->next_handle(con) == -1) { epoll_del_fd(g->epfd, evfd); close(con->fd); pool_destroy(con->p); }*/ } break; case CGIFD: { epoll_cgi_t *cgi = (epoll_cgi_t *)epoll_data->ptr; break; } } } else if(evfd->events & EPOLLOUT) { } evfd++; } } }
int main(int argc, char**argv){ //log files logs = fopen("/var/log/weperf/sensor/sensor.log", "a+"); int clientSocket; char buffer[1024]; char recv_buff[1024]; char send_buff[1024]; struct sockaddr_in serverAddr; socklen_t addr_size; //parse configurations from file if (ini_parse("/etc/weperf/sensor-config.ini", handler, &config) < 0) { client_log("Error", "Can't load 'config.ini'\n"); return 1; } //casue zombies to be reaped automatically if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { perror(0); exit(1); } /*---- Create the socket. The three arguments are: ----*/ /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */ clientSocket = socket(PF_INET, SOCK_STREAM, 0); /*---- Configure settings of the server address struct ----*/ /* Address family = Internet */ serverAddr.sin_family = AF_INET; /* Set port number, using htons function to use proper byte order */ serverAddr.sin_port = htons(config.server_port); /* Set IP address to localhost */ serverAddr.sin_addr.s_addr = inet_addr(config.server_addr); /* Set all bits of the padding field to 0 */ memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero); /*---- Connect the socket to the server using the address struct ----*/ addr_size = sizeof serverAddr; if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)){ client_log("Error", "Connecting to server - %s", strerror(errno)); return 0; } client_log("Info", "Connected to server"); //get IP of interface - add interface to config file struct ifreq ifr; strncpy(ifr.ifr_name, config.interface, IFNAMSIZ-1); ioctl(clientSocket, SIOCGIFADDR, &ifr); struct in_addr local_ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; printf("Local IP %s\n", inet_ntoa(local_ip)); //timeout struct timeval tv; int bytes = 1; //receive loop while(bytes){ FILE *fp; fd_set rfds; FD_ZERO(&rfds); FD_SET (clientSocket, &rfds); tv.tv_sec = 60; tv.tv_usec = 0; int ready = select(clientSocket + 1, &rfds, NULL, NULL, &tv); if(ready == -1 ){ }else if(ready){ bytes = recv(clientSocket, recv_buff, sizeof(recv_buff),0); struct srrp_request * request = (struct srrp_request *) recv_buff; if(request->type == SRRP_HB){ //heatbeat request printf("Received hb request - %d bytes\n", bytes); //build response response_init((struct srrp_response *) send_buff, request->type, SRRP_SCES, request->dst_id, request->m_id); send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); }else if(request->type == SRRP_ETHER){ //ethernet request printf("Received ether request - %d bytes\n", bytes); //build response response_init((struct srrp_response *) send_buff, request->type, SRRP_SCES, request->dst_id, request->m_id); //bit of a hack to break from srrp to send MAC address memcpy(&((struct srrp_response *)send_buff)->results[0], config.ether, strlen(config.ether) + 1); //also for the ip memcpy(&((struct srrp_response *)send_buff)->results[3], &local_ip, sizeof(local_ip)); send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff) /*header*/ + 100 /*mac*/ + sizeof(local_ip) /*ip*/, 0); }else if(request->type == SRRP_BW){ client_log("Info", "Recevived iperf request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * cmd_fmt = "iperf -c %s -p %d -t %d -y C"; char cmd[100]; //default params int dur = 10; //get parameters int i; for(i = 0; i < request->length; i++){ if(request->params[i].param == SRRP_DUR){ dur = request->params[i].value; }else{ client_log("Error", "Invalid iperf parameter"); } } //build command sprintf(cmd, cmd_fmt, inet_ntoa(request->dst_ip), config.tcp_iperf_port, dur); printf("%s\n", cmd); fp = popen(cmd, "r"); if(fp == NULL){ client_log("Error", "Failed to run command '%s'", cmd); _exit(1); } //get otuput - single line because of -y C flage char result[100]; while(fgets(result, sizeof(result)-1, fp) != NULL){} int exit_status = pclose(fp); if(exit_status != 0){ client_log("Error", "iperf failed exit status %d", exit_status); //should send resonpse with failed success code _exit(1); }else{ //build response if(parse_iperf(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, result)){ client_log("Error", "Failed to parse iperf response"); _exit(0); } client_log("Info", "Sending iperf results"); send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); } _exit(0); } }else if(request->type == SRRP_RTT){ client_log("Info", "Received ping request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * command_fmt = "ping -c %d %s"; char command[100]; //default params int itterations = 5; //load in params int i; for(i = 0; i < request->length; i++){ if(request->params[i].param == SRRP_ITTR){ itterations = request->params[i].value; }else{ client_log("Error", "Invalid ping parameter"); } } //build command sprintf(command, command_fmt, itterations, inet_ntoa(request->dst_ip)); printf("%s\n", command); fp = popen(command , "r"); if(fp == NULL){ client_log("Error", "Failed to run command %s", command); _exit(1); } //get otuput - single line because of -y C flage char result[100]; while(fgets(result, sizeof(result)-1, fp) != NULL){} int exit_status = pclose(fp); if(exit_status != 0){ client_log("Error", "command failed exit status %d", exit_status); //should respond }else{ if(parse_ping(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, result)){ client_log("Error", "Failed to parse ping response"); _exit(0); } send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); _exit(0); } } }else if(request->type == SRRP_UDP){ client_log("Info", "Received UDP iperf request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * cmd_fmt = "iperf -c %s -u -p %d -b %dK -l %d -t %d -S %d -y C"; char cmd[100]; //default params int speed = 1; int size = 1470; int duration = 10; int dscp = 0; //load in params int i; for(i = 0; i < request->length; i++){ if(request->params[i].param == SRRP_SPEED){ speed = request->params[i].value; }else if(request->params[i].param == SRRP_SIZE){ size = request->params[i].value; }else if(request->params[i].param == SRRP_DUR){ duration = request->params[i].value; }else if(request->params[i].param == SRRP_DSCP){ dscp = request->params[i].value; }else{ client_log("Error", "Invalid udp parameter"); } } sprintf(cmd, cmd_fmt, inet_ntoa(request->dst_ip), config.udp_iperf_port, speed, size, duration, dscp); printf("%s\n", cmd); fp = popen(cmd , "r"); if(fp == NULL){ client_log("Error", "Failed to run command %s", cmd); _exit(1); } //get otuput - single line because of -y C flage char result[200]; while(fgets(result, sizeof(result)-1, fp) != NULL) printf("%s\n", result); int exit_status = pclose(fp); if(exit_status != 0){ client_log("Error", "udp iperf failed exit status %d", exit_status); //should send resonpse with failed success code //parse_failure() _exit(1); }else{ if(parse_udp(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, result, speed, dscp)){ client_log("Error", "Failed to parse udp response"); _exit(0); } send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); client_log("Info", "Sending udp iperf results"); _exit(0); } } }else if(request->type == SRRP_DNS){ client_log("Info", "Received dns request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * cmd_fmt = "nslookup %s %s"; char cmd[100]; char * domain_name = NULL; char * server = NULL; int i = 0; while(i < request->length){ if(request->params[i].param == SRRP_DN){ i = get_param_string(&domain_name, request, i); }else if(request->params[i].param == SRRP_SERVER){ i = get_param_string(&server, request, i); }else{ client_log("Error", "Invalid param"); } } if(!server || strcmp(server, "default") == 0) server = ""; if(!domain_name || strcmp(server, "default") == 0) domain_name = (char *)config.nslookup_addr; sprintf(cmd, cmd_fmt, domain_name, server); printf("%s\n", cmd); struct timeval start, end; float mtime, secs, usecs; gettimeofday(&start, NULL); fp = popen(cmd , "r"); if(fp == NULL){ client_log("Error", "Failed to run command %s", cmd); _exit(1); } //get otuput char result[200]; while(fgets(result, sizeof(result)-1, fp) != NULL){} //work out resolution time gettimeofday(&end, NULL); secs = end.tv_sec - start.tv_sec; usecs = end.tv_usec - start.tv_usec; mtime = ((secs) * 1000 + usecs/1000.0) + 0.5; int exit_status = pclose(fp); if(exit_status != 0){ client_log("Info", "DNS status failure - exit status %d", exit_status); parse_failure(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff); }else{ client_log("Info", "DNS status sucess - exit status %d", exit_status); parse_dns(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, mtime); } send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); client_log("Info", "Sending dns response"); _exit(0); } }else{ //unrecognised data client_log("Error", "Recevied unrecognised data - %d - %d bytes", request->type, bytes); } }else{ //timeout //dont care about timeout --- keep running client_log("Error", "Timeout"); break; } } client_log("Info", "Recevied empty data, closing connection"); fclose(logs); close(clientSocket); return 0; }