static int register_specified_morph(cha_lat_t *lat, cha_seg_t *seg) { mrph_t *mrph; da_lex_t lex_data[DIC_BUFSIZ]; /* XXX */ int dic_no, i, nlex, len; long index; int is_found = 0; unsigned char *text = lat->text + lat->offset; for (dic_no = 0; dic_no < Da_ndicfile; dic_no++) { if ((index = da_exact_lookup(Da_dicfile[dic_no], text, seg->len)) < 0) continue; nlex = da_get_lex(Da_dicfile[dic_no], index, lex_data, &len); for (i = 0; i < nlex; i++) { if (lex_data[i].posid == seg->posid && lex_data[i].inf_type == seg->inf_type && lex_data[i].inf_form == seg->inf_form) { mrph = new_mrph(); mrph->headword = text; mrph->headword_len = seg->len; mrph->is_undef = 0; mrph->darts = Da_dicfile[dic_no]; memcpy(mrph, lex_data + i, sizeof(da_lex_t)); mrph->weight = 0; /* ??? */ is_found = 1; check_connect(lat, mrph_last_idx()); } } } if (!is_found) { mrph = new_mrph(); mrph->headword = text; mrph->headword_len = seg->len; mrph->posid = seg->posid; mrph->is_undef = 0; mrph->inf_type = seg->inf_type; mrph->inf_form = seg->inf_form; mrph->con_tbl = cha_check_table_for_undef(seg->posid); /* ??? */ mrph->weight = 0; mrph->darts = NULL; check_connect(lat, mrph_last_idx()); } return mrph_last_idx(); }
void main_loop(void) { int i, n, timeout = 250; struct connection *conn; ufds = must_calloc(thread_limit, sizeof(struct pollfd)); for (i = 0; i < thread_limit; ++i) ufds[i].fd = -1; while (head || outstanding) { start_next_comic(); n = poll(ufds, thread_limit, timeout); if (n < 0) { my_perror("poll"); continue; } if (n == 0) { timeout_connections(); if (!start_next_comic()) /* Once we have all the comics * started, increase the timeout * period. */ timeout = 1000; continue; } for (conn = comics; conn; conn = conn->next) if (!conn->poll) continue; else if (conn->poll->revents & POLLOUT) { if (!conn->connected) check_connect(conn); else { time(&conn->access); write_request(conn); } } else if (conn->poll->revents & POLLIN) { /* This check is needed for openssl */ if (!conn->connected) check_connect(conn); else read_conn(conn); } } free(ufds); }
void check_servers(void) { /* For each server, check the following, in order: * * - Connection status. Skip the rest if unresolved * - Ping timeout. Skip the rest detected * - Reconnect attempt. Skip the rest if successful * - Socket input. Consume all input * */ /* TODO: there's probably a better order to check these */ server *s; if ((s = server_head) == NULL) return; time_t t = time(NULL); do { if (check_connect(s)) continue; if (check_latency(s, t)) continue; if (check_reconnect(s, t)) continue; check_socket(s, t); } while ((s = s->next) != server_head); }
int main(int argc, char *argv[]) { int i; init(); /* Init some variables (like malloc timestamp string, encrypt text string, etc.) */ check_par(argc, argv); /* Check command arguments number */ open_config(argv); /* Open config file and check if it failed */ open_log(argv); /* Open log file and check if it failed */ get_ipaddr(); /* Get server IP address */ create_socket(); /* Create a socket */ bind_socket(); /* Bind the socket */ listen_socket(); /* Listen at the socket */ print_server_info(); /* Print server information */ while (TRUE) { /* Read until the end of file */ if (read_flag) { if (fscanf(fcfg, "%s", enc_txt) == EOF) { finish_flag = 1; break; } else { fscanf(fcfg, "%s", dec_txt); } } read_flag = 0; init_select(); /* Select function */ if (select_func() == -1) break; for (i = 0; i < max_fds + 1; i++) { if (FD_ISSET(i, &rfds)) { if (i == sockfd) { /* If have a new client connect */ if (accept_new_cli() == -1) break; /* Try to accept new client */ if (check_connect() == -1) break; /* Check connect message from client */ if (print_client_info() == -1) break; /* Print the information of client side */ store_client_ip(); /* Store the client ip address */ break; } else { /* If have new message from client side */ client_ip = get_host_by_sockfd(i); /* Get the client ip address by socket */ recv_socket_msg(i, recv_mark); /* Get the message from socket */ handle_client_msg(i); /* Handle client message (SUCCESS_MSG, FAILURE_MSG, DISPATCH_MSG, etc.) */ break; } } if (main_flag == EXIT_FAILURE) break; } if (main_flag == EXIT_FAILURE) break; } remained_cli = ask_clients_quit(); /* Ask clients quit and count the remain clients number */ wait_clients_quit(); /* Wait for all clients quit */ quit_server(); /* Clean up and quit server, also print the message to log */ return main_flag; }
nsapi_error_t EasyCellularConnection::connect() { nsapi_error_t err = check_connect(); if (err) { return err; } #if USE_APN_LOOKUP if (!_credentials_set) { _target_state = CellularConnectionFSM::STATE_SIM_PIN; err = _cellularConnectionFSM->continue_to_state(_target_state); if (err == NSAPI_ERROR_OK) { int sim_wait = _cellularSemaphore.wait(60 * 1000); // reserve 60 seconds to access to SIM if (sim_wait != 1) { tr_error("NO SIM ACCESS"); err = NSAPI_ERROR_NO_CONNECTION; } else { char imsi[MAX_IMSI_LENGTH + 1]; wait(1); // need to wait to access SIM in some modems err = _cellularConnectionFSM->get_sim()->get_imsi(imsi); if (err == NSAPI_ERROR_OK) { const char *apn_config = apnconfig(imsi); if (apn_config) { const char* apn = _APN_GET(apn_config); const char* uname = _APN_GET(apn_config); const char* pwd = _APN_GET(apn_config); tr_info("Looked up APN %s", apn); err = _cellularConnectionFSM->get_network()->set_credentials(apn, uname, pwd); } } } } if (err) { tr_error("APN lookup failed"); return err; } } #endif // USE_APN_LOOKUP _target_state = CellularConnectionFSM::STATE_CONNECTED; err = _cellularConnectionFSM->continue_to_state(_target_state); if (err == NSAPI_ERROR_OK) { int ret_wait = _cellularSemaphore.wait(10 * 60 * 1000); // cellular network searching may take several minutes if (ret_wait != 1) { tr_info("No cellular connection"); err = NSAPI_ERROR_NO_CONNECTION; } } return err; }
int cha_parse_eos(cha_lat_t *lat) { int last_idx; collect_mrphs_for_pos(lat); last_idx = register_bos_eos(); if (check_connect(lat, last_idx) == FALSE) { fprintf(stderr, "Error: Too many morphs\n"); return -1; } lat->len = lat->offset; return lat->offset; }
/** * @brief GetHostIdByName * 根据主机名查找对应的ID * @Param: hostname 主机名 * @Param: hostid 找到的id放入该参数 * * Returns: -1 查找错误 0 查找正确 */ Uint32 GetHostIdByName(char* hostname, Uint32 & hostid) { if (hostname == NULL) { LOG_ERROR("hostname is NULL\n"); return -1; } check_connect(); char sql[96]; sprintf(sql, "select HostAdd from HostInfo where HostName='%s'", hostname); LOG_DEBUG("GetHostIdByName sql=%s\n", sql); int ret = mysql_query(&m_mysqlfd, sql); if (ret != 0) { LOG_ERROR("mysql_query failed select error:%s\n ", mysql_error(&m_mysqlfd)); return -1; } MYSQL_RES *pres = mysql_use_result(&m_mysqlfd);//返回执行结果,适用于数据量较大时 if (pres == NULL) { LOG_ERROR("mysql_use_result failed\n "); return -1; } hostid = 0; Uint32 index = 0; MYSQL_ROW mysqlrow; while ((mysqlrow = mysql_fetch_row(pres)) != NULL) { //后继的调用正常返回的内容 if (mysqlrow[0]) { hostid = (Uint32)atoi(mysqlrow[0]); } index++; LOG_DEBUG("select success fieldcoun=%d index=%d hostid=%08x\n", mysql_field_count(&m_mysqlfd), index, hostid); } mysql_free_result(pres); if (index > 1) return -1; return 0; }
/** * @brief db_update_query * 更新查询 * @Param: setstr 设置字符串 * @Param: wherestr 条件字符串 * @Param: tablename 表名 * * Returns: */ static int db_update_query(const string &setstr, const string &wherestr, const string &tablename) { if (setstr.empty() || wherestr.empty() || tablename.empty()) return -1; check_connect(); string sql = "update "; sql += tablename; sql += " set "; sql += setstr; sql += " where "; sql += wherestr; LOG_DEBUG("UpdateQuery len=%ld sql=%s\n", sql.length(), sql.c_str()); return mysql_query(&m_mysqlfd, sql.c_str()); }
/** * @brief GetHostCount * 拿到 连接上来的主机个数 * Returns: 主机个数 */ int GetHostCount() { check_connect(); char sql[64]; sprintf(sql, "select count(*) from HostInfo"); int ret = mysql_query(&m_mysqlfd, sql); if (ret != 0) { LOG_ERROR("mysql_query failed select error:%s\n ", mysql_error(&m_mysqlfd)); return -1; } MYSQL_RES *pres = mysql_use_result(&m_mysqlfd);//返回执行结果,适用于数据量较大时 if (pres == NULL) { LOG_ERROR("mysql_use_result failed\n "); return -1; } MYSQL_ROW mysqlrow; Uint32 fieldcoun = mysql_field_count(&m_mysqlfd);//返回查询结果中的列数(column数) Uint32 index = 0; int count = 0; while ((mysqlrow = mysql_fetch_row(pres)) != NULL) {//后继的调用正常返回的内容 index = 0; while (index < fieldcoun) { if (mysqlrow[index]) { count = atoi(mysqlrow[index]); } index++; } LOG_DEBUG("select success fieldcoun=%d count=%d\n", fieldcoun, count); } mysql_free_result(pres); return count; }
static int register_mrphs(cha_lat_t *lat, darts_t* da, char *strings, long index) { mrph_t *new_mrph; da_lex_t lex_data[DIC_BUFSIZ]; /* XXX */ int nlex, i, len; nlex = da_get_lex(da, index, lex_data, &len); for (i = 0; i < nlex; i++) { new_mrph = new_mrph(); new_mrph->headword = strings; new_mrph->headword_len = len; new_mrph->is_undef = 0; new_mrph->darts = da; memcpy(new_mrph, lex_data + i, sizeof(da_lex_t)); check_connect(lat, mrph_last_idx()); } return mrph_last_idx(); }
static int register_undef_mrph(cha_lat_t *lat, char *string, int len, int no) { mrph_t *mrph = new_mrph(); mrph->posid = Cha_undef_info[no].hinsi; mrph->inf_type = 0; mrph->inf_form = 0; mrph->weight = MRPH_DEFAULT_WEIGHT; mrph->con_tbl = Cha_undef_info[no].con_tbl; mrph->headword = string; mrph->headword_len = len; mrph->is_undef = no + 1; mrph->darts = NULL; check_connect(lat, mrph_last_idx()); return mrph_last_idx(); }
/** * @brief DupicateKeyUpdateQuery * 更新、插入查询:存在则更新不存在则插入 * @Param: colstr 列 * @Param: valstr 值 * @Param: updatestr 要更新的列 * @Param: tablename 表名 * * Returns: */ static int DupicateKeyUpdateQuery(const string &colstr, const string &valstr, const string &updatestr, const string &tablename) { if (colstr.empty() || valstr.empty() || updatestr.empty() || tablename.empty()) return -1; check_connect(); string sql = "INSERT INTO "; sql += tablename; sql += "("; sql += colstr; sql += ") "; sql += "VALUES("; sql += valstr; sql += ") on DUPLICATE KEY UPDATE "; sql += updatestr; LOG_DEBUG("DupicateKeyUpdateQuery len=%ld sql=%s\n", sql.length(), sql.c_str()); return mysql_query(&m_mysqlfd, sql.c_str()); }
/** * @brief select_query * 查找操作 * @Param: sql 查找sql 命令 * @Param: retstr 返回列字段,以逗号隔开 * * Returns: */ static int select_query(const string & sql, string &retstr) { check_connect(); int nRet = mysql_query(&m_mysqlfd, sql.c_str()); if (nRet != 0) { LOG_ERROR("mysql_query failed select error:%s\n ", mysql_error(&m_mysqlfd)); return -1; } MYSQL_RES *pres = mysql_use_result(&m_mysqlfd);//返回执行结果,适用于数据量较大时 if (pres == NULL) { LOG_ERROR("mysql_use_result failed\n "); return -1; } Uint32 fieldcoun = mysql_field_count(&m_mysqlfd);//返回查询结果中的列数(column数) Uint32 index = 0; MYSQL_ROW mysqlrow; while ((mysqlrow = mysql_fetch_row(pres)) != NULL) {//后继的调用正常返回的内容 index = 0; while (index < fieldcoun) { if (mysqlrow[index]) { retstr.append(mysqlrow[index]); retstr.append(","); } index++; } LOG_DEBUG("select success fieldcoun=%d len=%ld ret=%s\n", fieldcoun, retstr.length(), retstr.c_str()); } mysql_free_result(pres); return retstr.empty()?1:0; }
/** * @brief findHostname 查找对应的主机名 * * @Param: hostname 主机名 * * Returns: 返回值 -1 错误 0 没有找到对应的 >0 找到的主机个数 */ int findHostname(char* hostname) { if (hostname == NULL) { LOG_ERROR("hostname is null..\n"); return -1; } check_connect(); char sql_cmd[96]; sprintf(sql_cmd, "select 1 from HostInfo where HostName='%s' limit 1", hostname); int ret = mysql_query(&m_mysqlfd, sql_cmd); if (ret != 0) { LOG_ERROR("query error:%s\n ",mysql_error(&m_mysqlfd)); return -1; } MYSQL_RES *pres = mysql_use_result(&m_mysqlfd);//返回执行结果,适用于数据量较大时 if (pres == NULL) { LOG_ERROR("mysql_use_result failed\n"); return -1; } MYSQL_ROW mysqlrow; int count = 0; while ((mysqlrow = mysql_fetch_row(pres)) != NULL) { count++; } //Int32 count = mysql_num_rows(pres);//返回上面函数返回结果的行数,对select有效 mysql_free_result(pres); return count; }
/** * @brief db_insert_query * 数据库插入 * @Param: colstr 列字段 * @Param: valstr 值字段 * @Param: tablename 数据表名 * * Returns: */ static int db_insert_query(const string &colstr, const string &valstr, const string &tablename) { /*插入前 检查数据连接 */ check_connect(); if(strcmp(tablename.c_str(),"") == 0 ) { LOG_ERROR("no such table.\n"); return -1; } string sql_cmd = "INSERT INTO "; sql_cmd += tablename; sql_cmd += "("; sql_cmd += colstr; sql_cmd += ") "; sql_cmd += "VALUES("; sql_cmd += valstr; sql_cmd += ")"; LOG_DEBUG("db_insert_query len=%ld sql_cmd=%s\n", sql_cmd.length(), sql_cmd.c_str()); return insert_mysql(sql_cmd); }
/** * @brief delete_db_host * 删除主机设备 * @Param: hostname * * Returns: */ int delete_db_host(char* hostname) { check_connect(); char sql[1024]; int len = 0; int ret = 0; for (int i = 1; i < MAX_DB_TYPE; i++) { len = 0; len += sprintf(sql + len, "DELETE %s.* ", DB_TABLE_NAMELIST[i]); len += sprintf(sql + len, "FROM %s ", DB_TABLE_NAMELIST[i]); len += sprintf(sql + len, "LEFT JOIN %s ON %s.HostAdd = %s.HostAdd ", DB_TABLE_HOSTINFO, DB_TABLE_NAMELIST[i], DB_TABLE_HOSTINFO); len += sprintf(sql + len, "WHERE %s.HostName = '%s'", DB_TABLE_HOSTINFO, hostname); sql[len] = 0; LOG_DEBUG("DeleteHost sql=%s\n", sql); ret = mysql_query(&m_mysqlfd, sql); if (ret != 0) { LOG_ERROR("mysql_query failed select error:%s\n ", mysql_error(&m_mysqlfd)); return -1; } } len = 0; len += sprintf(sql + len, "DELETE FROM %s WHERE HostName='%s' ", DB_TABLE_HOSTINFO, hostname); sql[len] = 0; LOG_DEBUG("DeleteHost sql=%s\n", sql); ret = mysql_query(&m_mysqlfd, sql); if (ret != 0) { LOG_ERROR("mysql_query failed select error:%s\n ", mysql_error(&m_mysqlfd)); return -1; } return 0; }
void rdp_connect(GtkButton *connect, gpointer erdp) { /*get the string info*/ GtkEntry *rip = (GtkEntry *) find_child(erdp, "address"); GtkEntry *ruser = (GtkEntry *) find_child(erdp, "user"); GtkEntry *rpass = (GtkEntry *) find_child(erdp, "pass"); GtkEntry *arguments = (GtkEntry *) find_child(erdp, "arguments"); GtkCheckButton *fullscreen = (GtkCheckButton *) find_child(erdp, "fullscreen"); GtkCheckButton *decorations = (GtkCheckButton *) find_child(erdp, "decorations"); GtkCheckButton *smartscaling = (GtkCheckButton *) find_child(erdp, "smartscaling"); GtkCheckButton *sound = (GtkCheckButton *) find_child(erdp, "sound"); GtkCheckButton *clipboard = (GtkCheckButton *) find_child(erdp, "clipboard"); GtkCheckButton *homedir = (GtkCheckButton *) find_child(erdp, "homedir"); char *xfreerdp = find_executable("xfreerdp"); printf("Xfreerdp path: %s\n", xfreerdp); /*format my strings correctly*/ char *fip = g_strconcat("/v:", gtk_entry_get_text(rip), NULL); char *fuser = g_strconcat("/u:", gtk_entry_get_text(ruser), NULL); char *fpass = g_strconcat("/p:", gtk_entry_get_text(rpass), NULL); /*check to see if the connection is valid*/ if(check_connect(fip, fuser, fpass) != TRUE) { return; } /*check what options to add to rdp from the options tickboxes*/ char **opts = malloc(sizeof(char *)); opts[0] = NULL; opts = add_opt(&opts, xfreerdp); opts = add_opt(&opts, "/cert-ignore"); opts = add_opt(&opts, "/auto-reconnect"); opts = add_opt(&opts, fip); opts = add_opt(&opts, fuser); opts = add_opt(&opts, fpass); if(gtk_toggle_button_get_active((GtkToggleButton*)fullscreen) == TRUE) { opts = add_opt(&opts, "/f"); } if(gtk_toggle_button_get_active((GtkToggleButton*)decorations) == TRUE) { opts = add_opt(&opts, "/disp"); opts = add_opt(&opts, "/fonts"); opts = add_opt(&opts, "/aero"); opts = add_opt(&opts, "/window-drag"); opts = add_opt(&opts, "/menu-anims"); } if(gtk_toggle_button_get_active((GtkToggleButton*)smartscaling) == TRUE) { GdkRectangle *workarea = g_new(GdkRectangle, 1); GdkScreen *screen = gdk_screen_get_default(); gint monitor = gdk_screen_get_primary_monitor(screen); gdk_screen_get_monitor_workarea(screen, monitor, workarea); int width = workarea->width; int height = workarea->height; char sizebuff[32]; snprintf(sizebuff, 31, "/size:%dx%d", width, height); opts = add_opt(&opts, sizebuff); snprintf(sizebuff, 31, "/smart-sizing:%dx%d", width, height); opts = add_opt(&opts, sizebuff); } if(gtk_toggle_button_get_active((GtkToggleButton*)sound) == TRUE) { opts = add_opt(&opts, "/sound"); } if(gtk_toggle_button_get_active((GtkToggleButton*)clipboard) == TRUE) { opts = add_opt(&opts, "/clipboard"); } if(gtk_toggle_button_get_active((GtkToggleButton*)homedir) == TRUE) { opts = add_opt(&opts, "/home-drive"); } /* add the user specified options. We do no error checking */ char *argtext = strdup(gtk_entry_get_text(arguments)); char *argbuff = strtok(argtext, " "); while(argbuff != NULL) { opts = add_opt(&opts, argbuff); strtok(NULL, " "); } /*and call xfreerdp*/ int i; printf("Calling: "); for(i=0; opts[i] != NULL; i++) { if(strncmp(opts[i], "/p:", 3) == 0) { printf("/p:**** "); continue; } printf("%s ", opts[i]); } printf("\n"); execv(xfreerdp, opts); /*code never gets here*/ return; }
int main(int argc, char *argv[]) { char *env; int i, n, timeout = 250; struct connection *conn; method = "HEAD"; while ((i = getopt(argc, argv, "hp:t:vT:")) != -1) switch ((char)i) { case 'h': usage(0); case 'p': set_proxy(optarg); break; case 't': thread_limit = strtol(optarg, NULL, 0); break; case 'v': verbose++; break; case 'T': read_timeout = strtol(optarg, NULL, 0); break; default: usage(1); } if (optind < argc) while (optind < argc) read_link_file(argv[optind++]); else read_urls(stdin); /* set_proxy will not use this if proxy already set */ env = getenv("COMICS_PROXY"); if (env) set_proxy(env); if (thread_limit == 0) { printf("You must allow at least one thread\n"); exit(1); } if (thread_limit > n_comics) thread_limit = n_comics; #ifdef _WIN32 win32_init(); #else signal(SIGTERM, dump_outstanding); signal(SIGHUP, dump_outstanding); #endif npoll = thread_limit + 1; /* add one for stdin */ ufds = must_calloc(npoll, sizeof(struct pollfd)); for (i = 0; i < npoll; ++i) ufds[i].fd = -1; while (head || outstanding) { start_next_comic(); n = poll(ufds, npoll, timeout); if (n < 0) { my_perror("poll"); continue; } if (n == 0) { timeout_connections(); if (!start_next_comic()) /* Once we have all the comics * started, increase the timeout * period. */ timeout = 1000; continue; } for (conn = comics; conn; conn = conn->next) if (!conn->poll) continue; else if (conn->poll->revents & POLLOUT) { if (!conn->connected) check_connect(conn); else { time(&conn->access); write_request(conn); } } else if (conn->poll->revents & POLLIN) { /* This check is needed for openssl */ if (!conn->connected) check_connect(conn); else read_conn(conn); } } out_results(comics, 0); return n_comics != gotit; }
/** * @brief delete_db_rtu * 删除设备 接口 不包括删除主机 * @Param: pRtuinfo * * Returns: -1 删除出错 0 删除成功 或者 没有删除项 */ int delete_db_rtu(RtuRemoveInfo* pRtuinfo) { if(pRtuinfo == NULL) { LOG_ERROR("pRtuinfo is NULL\n"); return -1; } RtuRemoveInfo rtuinfo = *pRtuinfo; Uint8 type =rtuinfo.rtutype; LOG_DEBUG("wjj test type=%u\n",type); /* 拿到 表 名 */ string tablename=get_table_name(type); if(strcmp(tablename.c_str(),"")==0) { LOG_ERROR("no such table...\n"); return -1; } check_connect(); char sql[256]; Uint64 index = rtuinfo.hostadd; index = index << 32; index += rtuinfo.rtuadd; sprintf(sql, "delete from %s where UniqueIndex=%ld", tablename.c_str(), index); LOG_DEBUG("delete sql=%s\n",sql); int ret = mysql_query(&m_mysqlfd, sql); if (ret != 0) { LOG_ERROR("mysql_query failed delete error:%s\n ", mysql_error(&m_mysqlfd)); return -1; } /* 如果是安防设备 ,则删除另外的表 */ if(strcmp(tablename.c_str(),DB_TABLE_NAMELIST[6]) == 0 ) { tablename=DB_TABLE_NAMELIST[7]; memset(sql,0,256); sprintf(sql, "delete from %s where UniqueIndex=%ld", tablename.c_str(), index); if(mysql_query(&m_mysqlfd, sql)!=0) { return -1; } tablename=DB_TABLE_NAMELIST[8]; memset(sql,0,256); sprintf(sql, "delete from %s where UniqueIndex=%ld", tablename.c_str(), index); if(mysql_query(&m_mysqlfd, sql)!=0) { return -1; } tablename=DB_TABLE_NAMELIST[9]; memset(sql,0,256); sprintf(sql, "delete from %s where UniqueIndex=%ld", tablename.c_str(), index); if(mysql_query(&m_mysqlfd, sql)!=0) { return -1; } } /* 如果是传感器设备 ,则删除另外的表 */ else if (strcmp(tablename.c_str(),DB_TABLE_NAMELIST[11]) == 0 ) { tablename=DB_TABLE_NAMELIST[12]; memset(sql,0,256); sprintf(sql, "delete from %s where UniqueIndex=%ld", tablename.c_str(), index); if(mysql_query(&m_mysqlfd, sql)!=0) { return -1; } } return 0; }
int _read(SSL_CTX *ctx, int sockfd) { char buf[2000]; struct sockaddr_in6 src; int len, ifindex, i; char addr[INET6_ADDRSTRLEN]; char port[6]; socklen_t sz = sizeof(struct sockaddr_in6); #ifdef WITH_DTLS SSL *ssl; int err; #endif /* Retrieve remote address and interface index as well as the first few bytes of the message to demultiplex protocols. */ memset(&src, 0, sizeof(struct sockaddr_in6)); len = check_connect(sockfd, buf, 4, (struct sockaddr *)&src, &ifindex); if (len < 0) /* error */ return len; #ifndef NDEBUG fprintf(stderr,"received packet"); if (getnameinfo((struct sockaddr *)&src, sizeof(src), addr, sizeof(addr), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) == 0) fprintf(stderr," from [%s]:%s", addr, port); fprintf(stderr," on interface %d\n", ifindex); #endif switch (demux_protocol(buf, len)) { #ifdef WITH_DTLS case DTLS : ssl = get_ssl(ctx, sockfd, (struct sockaddr *)&src, ifindex); if (!ssl) { fprintf(stderr, "cannot create new SSL object\n"); /* return recv(sockfd, buf, sizeof(buf), MSG_DONTWAIT);*/ len = recvfrom(sockfd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&src, &sz); getnameinfo((struct sockaddr *)&src, sz, addr, sizeof(addr), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); printf("discarded %d bytes from [%s]:%s\n", len, addr, port); return len; } len = SSL_read(ssl, buf, sizeof(buf)); break; #endif case UNKNOWN: default : len = recv(sockfd, buf, sizeof(buf), MSG_DONTWAIT); } if (len > 0) { printf("here is the data:\n"); for (i=0; i<len; i++) printf("%c",buf[i]); } if (len == 0) { /* session closed? */ #ifdef WITH_DTLS if (check_close(ssl) <= 0) { fprintf(stderr, "not closed\n"); } #endif } else { #ifdef WITH_DTLS err = SSL_get_error(ssl,len); switch (err) { case SSL_ERROR_WANT_READ: fprintf(stderr, "SSL_ERROR_WANT_READ\n"); return 0; case SSL_ERROR_WANT_WRITE: fprintf(stderr, "SSL_ERROR_WANT_WRITE\n"); return 0; default: fprintf(stderr, "read: SSL error %d: %s\n", err, ERR_error_string(err, NULL)); return 0; } #else perror("recv"); #endif } return len; }