void* chat_read(void* arg) { LOGGER(LOG_CRITICAL,"Entered into chat_read function"); int ret = 0, sd = 0; char buffer[MAX_DATA_LEN]; struct cnode *ptr = (struct cnode*)arg; sd = ptr->sd; //Initializing structure for signal handling struct sigaction sa; sa.sa_handler = &handle_signal; sa.sa_flags = SA_RESTART; sigfillset(&sa.sa_mask); if(-1 == sigaction(SIGINT, &sa, NULL)) { ERROR_LOGGER(LOG_CRITICAL, "SIGNAL handling failed for SIGINT"); perror("Error"); exit(EXIT_FAILURE); } if(-1 == sigaction(SIGTSTP, &sa, NULL)) { ERROR_LOGGER(LOG_CRITICAL, "SIGNAL handling failed for SIGTSTP"); perror("Error"); exit(EXIT_FAILURE); } while(1) { ret = read(sd, buffer, MAX_DATA_LEN); buffer[ret] = '\0'; if(-1 == ret) { ERROR_LOGGER(LOG_CRITICAL,"Write error"); perror("Read error"); exit(EXIT_FAILURE); } if(0 == ret) { close(sd); LOGGER(LOG_CRITICAL,"Exit from chat_read function"); printf("Quitting..\n"); exit(EXIT_SUCCESS); // pthread_exit(NULL); } if(0 < ret) { printf("%s\n", buffer); } bzero(buffer, MAX_DATA_LEN); } LOGGER(LOG_CRITICAL,"Exit from chat_read function"); }
void send_group_number(int sd, int num) { LOGGER(LOG_CRITICAL,"Entered into send_group_number function"); int inum = 0, ret = 0; inum = htons(num); ret = write(sd, &inum, sizeof(int)); if(-1 == ret) { ERROR_LOGGER(LOG_CRITICAL,"Write error"); perror("Write error"); exit(EXIT_FAILURE); } LOGGER(LOG_CRITICAL,"Exit from send_group_number function"); }
void receive_flag(int sd, int *flag) { LOGGER(LOG_CRITICAL,"Entered into receive_flag function"); int iflag = 0, ret = 0; ret = read(sd, &iflag, sizeof(int)); if(-1 == ret) { ERROR_LOGGER(LOG_CRITICAL,"Read error"); perror("Read error"); exit(EXIT_FAILURE); } *flag = ntohs(iflag); LOGGER(LOG_CRITICAL,"Exit from receive_flag function"); }
void send_flag(int sd, int flag) { LOGGER(LOG_CRITICAL,"Entered into send_flag function"); int iflag = 0, ret = 0; iflag = htons(flag); ret = write(sd, &iflag, sizeof(int)); if(-1 == ret) { ERROR_LOGGER(LOG_CRITICAL,"Write error"); perror("Write error"); exit(EXIT_FAILURE); } LOGGER(LOG_CRITICAL,"Exit from send_flag function"); }
void handle_signal(int signal) { LOGGER(LOG_CRITICAL,"Entered into handle_signal function"); int ret = 0; char* msg = "Type in 'quit' to exit\n\n"; ret = write(1, msg, strlen(msg)); if(-1 == ret) { ERROR_LOGGER(LOG_CRITICAL,"Write error"); perror("Write error"); exit(EXIT_FAILURE); } LOGGER(LOG_CRITICAL,"Exit from handle_signal function"); }
void* chat_write(void* arg) { LOGGER(LOG_CRITICAL,"Entered into chat_write function"); int ret = 0, sd = 0; char buffer[MAX_DATA_LEN]; int g_number = 0; struct cnode *ptr = (struct cnode*)arg; sd = ptr->sd; g_number = ptr->g_number; //Sending the group number to server send_group_number(sd, g_number); printf("Chat session (enter 'quit' to exit) :"); while(1) { printf("\n"); fgets(buffer, MAX_DATA_LEN, stdin); buffer[strlen(buffer) - 1] = '\0'; if(strlen(buffer) > (MAX_DATA_LEN - 1)) { printf("Buffer size full,\t enter within %d characters\n", MAX_DATA_LEN); bzero(buffer, MAX_DATA_LEN); __fpurge(stdin); } ret = write(sd ,buffer, strlen(buffer)); if(-1 == ret) { ERROR_LOGGER(LOG_CRITICAL, "Write error"); perror("Write error"); exit(EXIT_FAILURE); } if(0 == strcmp(buffer, "quit")) { close(sd); LOGGER(LOG_CRITICAL,"Exit from chat_write function"); printf("Quitting..\n"); // pthread_exit(NULL); exit(EXIT_SUCCESS); } bzero(buffer, MAX_DATA_LEN); }//while ends LOGGER(LOG_CRITICAL,"Exit from chat_write function"); }
/* connection event handler */ void conn_event_handler(int event_fd, short event, void *arg) { int error = 0; socklen_t len = sizeof(int); CONN *conn = (CONN *)arg; if(conn) { if(event_fd == conn->fd) { if(conn->status == CONN_STATUS_READY) { if(getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 || error != 0) { ERROR_LOGGER(conn->logger, "socket %d connecting failed, %s", strerror(errno)); conn->status = CONN_STATUS_CLOSED; CONN_TERMINATE(conn); return ; } conn->status = CONN_STATUS_CONNECTED; } if(event & E_CLOSE) { //DEBUG_LOGGER(conn->logger, "E_CLOSE:%d on %d START ", E_CLOSE, event_fd); CONN_TERMINATE(conn); //conn->push_message(conn, MESSAGE_QUIT); //DEBUG_LOGGER(conn->logger, "E_CLOSE:%d on %d OVER ", E_CLOSE, event_fd); } if(event & E_READ) { //DEBUG_LOGGER(conn->logger, "E_READ:%d on %d START", E_READ, event_fd); conn->read_handler(conn); //conn->push_message(conn, MESSAGE_INPUT); //DEBUG_LOGGER(conn->logger, "E_READ:%d on %d OVER ", E_READ, event_fd); } if(event & E_WRITE) { //conn->push_message(conn, MESSAGE_OUTPUT); //DEBUG_LOGGER(conn->logger, "E_WRITE:%d on %d START", E_WRITE, event_fd); conn->write_handler(conn); //DEBUG_LOGGER(conn->logger, "E_WRITE:%d on %d OVER", E_WRITE, event_fd); } } } }
int hispider_packet_handler(CONN *conn, CB_DATA *packet) { char *p = NULL, *end = NULL, *ip = NULL, *host = NULL, *path = NULL; HTTP_RESPONSE http_resp = {0}; int taskid = 0, n = 0; int c_id = 0, port = 0; struct hostent *hp = NULL; if(conn && tasklist && (c_id = conn->c_id) >= 0 && c_id < ntask) { p = packet->data; end = packet->data + packet->ndata; /* http handler */ if(conn == tasklist[c_id].c_conn) { if(p == NULL || http_response_parse(p, end, &http_resp) == -1) { conn->over_cstate(conn); conn->over(conn); return http_download_error(c_id); } if(http_resp.respid == RESP_OK && http_resp.headers[HEAD_ENT_CONTENT_TYPE] && strncasecmp(http_resp.headers[HEAD_ENT_CONTENT_TYPE], "text", 4) == 0) { conn->save_cache(conn, &http_resp, sizeof(HTTP_RESPONSE)); if((p = http_resp.headers[HEAD_ENT_CONTENT_LENGTH]) && (n = atol(p)) > 0) { conn->recv_chunk(conn, n); } else { conn->set_timeout(conn, HTTP_DOWNLOAD_TIMEOUT); conn->recv_chunk(conn, HTML_MAX_SIZE); } } else { conn->over_cstate(conn); conn->close(conn); return http_download_error(c_id); } } /* task handler */ else if(conn == tasklist[c_id].s_conn) { if(p == NULL || http_response_parse(p, end, &http_resp) == -1) { conn->over_cstate(conn); conn->over(conn); QUEUE_PUSH(taskqueue, int, &c_id); tasklist[c_id].s_conn = NULL; return -1; } if(http_resp.respid == RESP_OK) { host = http_resp.headers[HEAD_REQ_HOST]; ip = http_resp.headers[HEAD_RESP_SERVER]; port = (http_resp.headers[HEAD_REQ_REFERER]) ? atoi(http_resp.headers[HEAD_REQ_REFERER]) : 0; path = http_resp.headers[HEAD_RESP_LOCATION]; taskid = tasklist[c_id].taskid = (http_resp.headers[HEAD_REQ_FROM]) ? atoi(http_resp.headers[HEAD_REQ_FROM]) : 0; //fprintf(stdout, "%s::%d OK host:%s ip:%s port:%d path:%s taskid:%d \n", // __FILE__, __LINE__, host, ip, port, path, taskid); if(host == NULL || ip == NULL || path == NULL || port == 0 || taskid < 0) goto restart_task; if(strcmp(ip, "255.255.255.255") == 0) { if((hp = gethostbyname(host))) { tasklist[c_id].is_new_host = 1; strcpy(tasklist[c_id].host, host); sprintf(tasklist[c_id].ip, "%s", inet_ntoa(*((struct in_addr *)(hp->h_addr)))); ip = tasklist[c_id].ip; } else { DEBUG_LOGGER(logger, "Resolving name[%s] failed, %s", host, strerror(h_errno)); goto restart_task; } } if((tasklist[c_id].c_conn = service->newconn(service, -1, -1, ip, port, NULL))) { tasklist[c_id].nrequest = sprintf(tasklist[c_id].request, "GET %s HTTP/1.0\r\nHost: %s\r\n" "User-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\n" "Accept-Encoding: %s\r\nAccept-Charset: %s\r\nConnection: close\r\n\r\n", //"Accept-Charset: %s\r\nConnection: close\r\n\r\n", path, host, USER_AGENT, ACCEPT_TYPE, ACCEPT_LANGUAGE, ACCEPT_ENCODING, ACCEPT_CHARSET); tasklist[c_id].c_conn->c_id = c_id; tasklist[c_id].c_conn->start_cstate(tasklist[c_id].c_conn); return service->newtransaction(service, tasklist[c_id].c_conn, c_id); } else { ERROR_LOGGER(logger, "Connect to [%s][%s:%d] failed, %s\n", host, ip, port, strerror(errno)); } } restart_task: return http_download_error(c_id); }