void mcsdb_client_set (McSdbClient *ms, const char *key, const char *body, ut64 exptime __unused) { net_printf (ms->fd, "set %s 0 0 %d\r\n", key, strlen (body)); net_flush (ms->fd); net_printf (ms->fd, "%s\r\n", body); net_flush (ms->fd); free (net_readnl (ms->fd)); }
int send_data(const char *msg, Session *sn, Request *rq) { int len = msg != NULL?strlen(msg):0; int retVal = REQ_ABORTED; if(len > 0) { char buf[50]; buf[0] = '\0'; sprintf(buf, "%d", len); protocol_status(sn, rq, PROTOCOL_OK, NULL); param_free(pblock_remove("content-type", rq->srvhdrs)); pblock_nvinsert("content-type", "text/html", rq->srvhdrs); pblock_nvinsert("content-length", buf, rq->srvhdrs); /* Send the headers to the client*/ protocol_start_response(sn, rq); /* Write the output using net_write*/ if (IO_ERROR == net_write(sn->csd, (char *)msg, len)) { retVal = REQ_EXIT; } else { retVal = net_flush(sn->csd); } } return retVal; }
/** * NSAPI flush filter method. Calls OutputStream.flush in response to a * net_flush call. */ extern "C" int j2eefilter_method_flush(FilterLayer* layer) { J2EEFilterContext* context = (J2EEFilterContext*) layer->context->data; j2eefilter_migrate_response(layer->context->rq, context); if (!context->outputstream) { j2eefilter_flush_dirty(context); return net_flush(context->parent_sn->csd); } if (!context->throwable) { // Call OutputStream.flush context->env->CallVoidMethod(context->outputstream, _outputstream_flush); // Remember any OutputStream.flush exception j2eefilter_store_throwable(context); } if (context->throwable) { // Propagate exception information to NSAPI land JException jexc(context->env, context->throwable); NsprError::setErrorf(PR_IO_ERROR, "%s, %s", jexc.toString(), jexc.getMessage()); return -1; } return 0; }
static int client_mpvio_write_packet(struct st_plugin_vio *mpv, const uchar *pkt, size_t pkt_len) { int res; MCPVIO_EXT *mpvio= (MCPVIO_EXT*)mpv; if (mpvio->packets_written == 0) { if (mpvio->mysql_change_user) res= send_change_user_packet(mpvio, pkt, (int)pkt_len); else res= send_client_reply_packet(mpvio, pkt, (int)pkt_len); } else { NET *net= &mpvio->mysql->net; if (mpvio->mysql->thd) res= 1; /* no chit-chat in embedded */ else res= my_net_write(net, (char *)pkt, pkt_len) || net_flush(net); if (res) my_set_error(mpvio->mysql, CR_SERVER_LOST, SQLSTATE_UNKNOWN, ER(CR_SERVER_LOST_EXTENDED), "sending authentication information", errno); } mpvio->packets_written++; return res; }
void run_game(void) { //clmap = map_load_aos(fnmap); tcam.mpx = 256.5f; tcam.mpz = 256.5f; tcam.mpy = 32.0f-3.0f; //clmap->pillars[((int)tcam.mpz)*clmap->xlen+((int)tcam.mpy)][4+1]-2.0f; tcam.mxx = 1.0f; tcam.mxy = 0.0f; tcam.mxz = 0.0f; tcam.myx = 0.0f; tcam.myy = 1.0f; tcam.myz = 0.0f; tcam.mzx = 0.0f; tcam.mzy = 0.0f; tcam.mzz = 1.0f; int i; //render_vxl_redraw(&tcam, clmap); int quitflag = 0; usec_basetime = platform_get_time_usec(); while(!quitflag) { // update time sec_lasttime = sec_curtime; int64_t usec_curtime = platform_get_time_usec() - usec_basetime; sec_curtime = ((float)usec_curtime)/1000000.0f; // update client/server if(boot_mode & 1) quitflag = quitflag || update_client(); net_flush(); if(boot_mode & 2) quitflag = quitflag || update_server(); net_flush(); } map_free(clmap); clmap = NULL; }
int run_game_cont1(void) { int quitflag = update_client_cont1(); net_flush(); if(boot_mode & 2) quitflag = quitflag || update_server(); net_flush(); // update time sec_lasttime = sec_curtime; int64_t usec_curtime = platform_get_time_usec() - usec_basetime; sec_curtime = ((float)usec_curtime)/1000000.0f; // update client/server quitflag = quitflag || update_client_contpre1(); return quitflag; }
int receive_cmd(int sock, char *data, int max_len, int *p_cmd) { struct nestor_header header; int ret = 0, remaining; /* DEBUG("receive_cmd"); */ while (ret != HEADER_LEN) { ret = net_read(sock, (char *)&header, HEADER_LEN, -1 /* TIMEOUT_LONG */); if (ret <= 0) return ret; if (ret != HEADER_LEN) { ERROR("header read failed, ret: %d", ret); net_flush(sock); /* try to read again */ } } *p_cmd = header.cmd; //DEBUG("%s cmd %d len %d", __func__, header.cmd, header.len); if (header.len >= max_len) { ERROR("command too long, max length is: %d", max_len); net_flush(sock); return INVALID_DATA; } remaining = header.len; while (remaining > 0) { ret = net_read(sock, data, remaining, TIMEOUT_SHORT); if (ret <= 0) return ret; remaining -= ret; } /* set cmd terminate byte */ if (*p_cmd == CMD_XML) data[header.len] = '\0'; return header.len; }
int run_game_cont2(void) { int quitflag = 0; if(boot_mode & 2) quitflag = quitflag || update_server(); net_flush(); // update time sec_lasttime = sec_curtime; int64_t usec_curtime = platform_get_time_usec() - usec_basetime; sec_curtime = ((double)usec_curtime)/1000000.0; return quitflag; }
int net_printf (int fd, const char *fmt, ...) { int n; char buf[MCSDB_MAX_BUFFER]; va_list ap; va_start (ap, fmt); n = vsnprintf (buf, sizeof (buf)-1, fmt, ap); va_end (ap); if (n < 0) return 0; if (netbuflen+n>sizeof (netbuf)) net_flush (fd); strcpy (netbuf+netbuflen, buf); netbuflen += n; return n; }
/* modified version of apache2 */ static void sapi_nsapi_flush(void *server_context) { nsapi_request_context *rc = (nsapi_request_context *)server_context; TSRMLS_FETCH(); if (!rc) { /* we have no context, so no flushing needed. This fixes a SIGSEGV on shutdown */ return; } if (!SG(headers_sent)) { sapi_send_headers(TSRMLS_C); } /* flushing is only supported in iPlanet servers from version 6.1 on, make it conditional */ #if NSAPI_VERSION >= 302 if (net_flush(rc->sn->csd) < 0) { php_handle_aborted_connection(); } #endif }
char *mcsdb_client_decr (McSdbClient *ms, const char *key, ut64 val) { net_printf (ms->fd, "decr %s %lld\r\n", key, val); net_flush (ms->fd); return net_readnl (ms->fd); }
static int send_client_reply_packet(MCPVIO_EXT *mpvio, const uchar *data, int data_len) { MYSQL *mysql= mpvio->mysql; NET *net= &mysql->net; char *buff, *end; size_t conn_attr_len= (mysql->options.extension) ? mysql->options.extension->connect_attrs_len : 0; /* see end= buff+32 below, fixed size of the packet is 32 bytes */ buff= my_alloca(33 + USERNAME_LENGTH + data_len + NAME_LEN + NAME_LEN + conn_attr_len + 9); mysql->client_flag|= mysql->options.client_flag; mysql->client_flag|= CLIENT_CAPABILITIES; if (mysql->client_flag & CLIENT_MULTI_STATEMENTS) mysql->client_flag|= CLIENT_MULTI_RESULTS; #if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) if (mysql->options.ssl_key || mysql->options.ssl_cert || mysql->options.ssl_ca || mysql->options.ssl_capath || mysql->options.ssl_cipher) mysql->options.use_ssl= 1; if (mysql->options.use_ssl) mysql->client_flag|= CLIENT_SSL; /* if server doesn't support SSL and verification of server certificate was set to mandatory, we need to return an error */ if (mysql->options.use_ssl && !(mysql->server_capabilities & CLIENT_SSL)) { if ((mysql->client_flag & CLIENT_SSL_VERIFY_SERVER_CERT) || (mysql->options.extension && (mysql->options.extension->ssl_fp || mysql->options.extension->ssl_fp_list))) { my_set_error(mysql, CR_SSL_CONNECTION_ERROR, SQLSTATE_UNKNOWN, ER(CR_SSL_CONNECTION_ERROR), "Server doesn't support SSL"); goto error; } } #endif /* HAVE_OPENSSL && !EMBEDDED_LIBRARY*/ if (mpvio->db) mysql->client_flag|= CLIENT_CONNECT_WITH_DB; /* Remove options that server doesn't support */ mysql->client_flag= mysql->client_flag & (~(CLIENT_COMPRESS | CLIENT_SSL | CLIENT_PROTOCOL_41) | mysql->server_capabilities); #ifndef HAVE_COMPRESS mysql->client_flag&= ~CLIENT_COMPRESS; #endif if (mysql->client_flag & CLIENT_PROTOCOL_41) { /* 4.1 server and 4.1 client has a 32 byte option flag */ int4store(buff,mysql->client_flag); int4store(buff+4, net->max_packet_size); buff[8]= (char) mysql->charset->nr; bzero(buff+9, 32-9); end= buff+32; } else { int2store(buff, mysql->client_flag); int3store(buff+2, net->max_packet_size); end= buff+5; } #ifdef HAVE_OPENSSL if (mysql->options.ssl_key || mysql->options.ssl_cert || mysql->options.ssl_ca || mysql->options.ssl_capath || mysql->options.ssl_cipher #ifdef CRL_IMPLEMENTED || (mysql->options.extension && (mysql->options.extension->ssl_crl || mysql->options.extension->ssl_crlpath)) #endif ) mysql->options.use_ssl= 1; if (mysql->options.use_ssl && (mysql->client_flag & CLIENT_SSL)) { SSL *ssl; /* Send mysql->client_flag, max_packet_size - unencrypted otherwise the server does not know we want to do SSL */ if (my_net_write(net, (char*)buff, (size_t) (end-buff)) || net_flush(net)) { my_set_error(mysql, CR_SERVER_LOST, SQLSTATE_UNKNOWN, ER(CR_SERVER_LOST_EXTENDED), "sending connection information to server", errno); goto error; } /* Create SSL */ if (!(ssl= my_ssl_init(mysql))) goto error; /* Connect to the server */ if (my_ssl_connect(ssl)) { SSL_free(ssl); goto error; } if (mysql->options.extension && (mysql->options.extension->ssl_fp || mysql->options.extension->ssl_fp_list)) { if (ma_ssl_verify_fingerprint(ssl)) goto error; } if ((mysql->options.ssl_ca || mysql->options.ssl_capath) && (mysql->client_flag & CLIENT_SSL_VERIFY_SERVER_CERT) && my_ssl_verify_server_cert(ssl)) goto error; } #endif /* HAVE_OPENSSL */ DBUG_PRINT("info",("Server version = '%s' capabilites: %lu status: %u client_flag: %lu", mysql->server_version, mysql->server_capabilities, mysql->server_status, mysql->client_flag)); compile_time_assert(MYSQL_USERNAME_LENGTH == USERNAME_LENGTH); /* This needs to be changed as it's not useful with big packets */ if (mysql->user[0]) strmake(end, mysql->user, USERNAME_LENGTH); else read_user_name(end); /* We have to handle different version of handshake here */ DBUG_PRINT("info",("user: %s",end)); end= strend(end) + 1; if (data_len) { if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION) { *end++= data_len; memcpy(end, data, data_len); end+= data_len; } else { DBUG_ASSERT(data_len == SCRAMBLE_LENGTH_323 + 1); /* incl. \0 at the end */ memcpy(end, data, data_len); end+= data_len; } } else *end++= 0; /* Add database if needed */ if (mpvio->db && (mysql->server_capabilities & CLIENT_CONNECT_WITH_DB)) { end= strmake(end, mpvio->db, NAME_LEN) + 1; mysql->db= my_strdup(mpvio->db, MYF(MY_WME)); } if (mysql->server_capabilities & CLIENT_PLUGIN_AUTH) end= strmake(end, mpvio->plugin->name, NAME_LEN) + 1; end= ma_send_connect_attr(mysql, end); /* Write authentication package */ if (my_net_write(net, buff, (size_t) (end-buff)) || net_flush(net)) { my_set_error(mysql, CR_SERVER_LOST, SQLSTATE_UNKNOWN, ER(CR_SERVER_LOST_EXTENDED), "sending authentication information", errno); goto error; } my_afree(buff); return 0; error: my_afree(buff); return 1; }
/* {{{ mysql_handle_local_infile */ my_bool mysql_handle_local_infile(MYSQL *conn, const char *filename) { unsigned int buflen= 4096; unsigned int bufread; unsigned char *buf= NULL; void *info= NULL; my_bool result= 1; DBUG_ENTER("mysql_handle_local_infile"); if (!(conn->options.client_flag & CLIENT_LOCAL_FILES)) { my_set_error(conn, CR_UNKNOWN_ERROR, SQLSTATE_UNKNOWN, "Load data local infile forbidden"); /* write empty packet to server */ my_net_write(&conn->net, "", 0); net_flush(&conn->net); goto infile_error; } /* check if all callback functions exist */ if (!conn->options.local_infile_init || !conn->options.local_infile_end || !conn->options.local_infile_read || !conn->options.local_infile_error) mysql_set_local_infile_default(conn); /* allocate buffer for reading data */ buf = (uchar *)my_malloc(buflen, MYF(0)); /* init handler: allocate read buffer and open file */ if (conn->options.local_infile_init(&info, filename, conn->options.local_infile_userdata)) { char tmp_buf[MYSQL_ERRMSG_SIZE]; int tmp_errno; tmp_errno= conn->options.local_infile_error(info, tmp_buf, sizeof(tmp_buf)); my_set_error(conn, tmp_errno, SQLSTATE_UNKNOWN, tmp_buf); my_net_write(&conn->net, "", 0); net_flush(&conn->net); goto infile_error; } /* read data */ while ((bufread= conn->options.local_infile_read(info, (char *)buf, buflen)) > 0) { if (my_net_write(&conn->net, (char *)buf, bufread)) { my_set_error(conn, CR_SERVER_LOST, SQLSTATE_UNKNOWN, NULL); goto infile_error; } } /* send empty packet for eof */ if (my_net_write(&conn->net, "", 0) || net_flush(&conn->net)) { my_set_error(conn, CR_SERVER_LOST, SQLSTATE_UNKNOWN, NULL); goto infile_error; } /* error during read occured */ if (bufread < 0) { char tmp_buf[MYSQL_ERRMSG_SIZE]; int tmp_errno= conn->options.local_infile_error(info, tmp_buf, sizeof(tmp_buf)); my_set_error(conn, tmp_errno, SQLSTATE_UNKNOWN, tmp_buf); goto infile_error; } result = 0; infile_error: conn->options.local_infile_end(info); my_free((char *)buf, MYF(0)); DBUG_RETURN(result); }
/* We need this */ int main(int argc, char *argv[]) { int optc, show_help, show_version, show_usage; char *local_file, *cmd_listen_port, *cmd_pid_file; int inetd_mode, no_daemon; /* Set up some globals */ progname = argv[0]; listen_port = x_strdup(DEFAULT_LISTEN_PORT); pid_file = (DEFAULT_PID_FILE ? x_strdup(DEFAULT_PID_FILE) : 0); #ifndef DEBUG no_daemon = 0; #else /* DEBUG */ no_daemon = 1; #endif /* DEBUG */ local_file = cmd_listen_port = cmd_pid_file = 0; show_help = show_version = show_usage = inetd_mode = 0; while ((optc = getopt_long(argc, argv, GETOPTIONS, long_opts, NULL)) != -1) { switch (optc) { case 'h': show_help = 1; break; case 'v': show_version = 1; break; case 'D': #ifndef DEBUG no_daemon = 1; #else /* DEBUG */ no_daemon = 0; #endif /* DEBUG */ break; case 'I': inetd_mode = 1; break; case 'P': free(cmd_listen_port); cmd_listen_port = x_strdup(optarg); break; case 'p': free(cmd_pid_file); cmd_pid_file = x_strdup(optarg); break; case 'f': free(local_file); local_file = x_strdup(optarg); break; default: show_usage = 1; break; } } if (show_usage || (optind < argc)) { _print_usage(); return 1; } if (show_version) { _print_version(); if (!show_help) return 0; } if (show_help) { _print_help(); return 0; } /* If no -f was specified use the home directory */ if (!local_file && !inetd_mode) { struct stat statinfo; struct passwd *pw; pw = getpwuid(geteuid()); if (pw && pw->pw_dir) { local_file = x_sprintf("%s/%s", pw->pw_dir, USER_CONFIG_FILENAME); debug("Local config file: %s", local_file); if (!stat(local_file, &statinfo) && (statinfo.st_mode & 0077)) { fprintf(stderr, "%s: Permissions of %s must be 0700 or " "more restrictive\n", progname, local_file); free(local_file); return 2; } if (cfg_read(local_file, &listen_port, &pid_file, &g)) { /* If the local one didn't exist, set to 0 so we open global one */ free(local_file); local_file = 0; } else { config_file = x_strdup(local_file); } } } else if (local_file) { if (cfg_read(local_file, &listen_port, &pid_file, &g)) { /* This is fatal! */ fprintf(stderr, "%s: Couldn't read configuration from %s: %s\n", progname, local_file, strerror(errno)); free(local_file); return 2; } else { config_file = x_strdup(local_file); } } /* Read global config file if local one not found */ if (!local_file) { char *global_file; /* Not fatal if it doesn't exist */ global_file = x_sprintf("%s/%s", SYSCONFDIR, GLOBAL_CONFIG_FILENAME); debug("Global config file: %s", global_file); cfg_read(global_file, &listen_port, &pid_file, &g); config_file = x_strdup(global_file); free(global_file); } else { free(local_file); } /* Check we got some connection classes */ if (!connclasses) { fprintf(stderr, "%s: No connection classes have been defined.\n", progname); return 2; } /* -P overrides config file */ if (cmd_listen_port) { free(listen_port); listen_port = cmd_listen_port; } /* -p overrides pid file */ if (cmd_pid_file) { free(pid_file); pid_file = cmd_pid_file; } /* Set signal handlers */ signal(SIGTERM, _sig_term); signal(SIGINT, _sig_term); signal(SIGHUP, _sig_hup); signal(SIGCHLD, _sig_child); #ifdef DEBUG_MEMORY signal(SIGUSR1, _sig_usr); signal(SIGUSR2, _sig_usr); #endif /* DEBUG_MEMORY */ /* Broken Pipe? This means that someone disconnected while we were sending stuff. Naughty! */ signal(SIGPIPE, SIG_IGN); if (!inetd_mode) { debug("Ordinary console dodge-monkey mode"); /* Make listening socket before we fork */ if (ircnet_listen(listen_port)) { fprintf(stderr, "%s: Unable to establish listen port\n", progname); return 3; } /* go daemon here */ if (!no_daemon) { switch (go_daemon()) { case -1: return -1; case 0: break; default: return 0; } } } else { /* running under inetd means we are backgrounded right *now* */ in_background = 1; debug("Inetd SuperTed mode!"); /* Hook STDIN into a new proxy */ ircnet_hooksocket(STDIN_FILENO); } /* Open a connection to syslog if we're in the background */ if (in_background) openlog(PACKAGE, LOG_PID, LOG_USER); if (pid_file) { FILE *pidfile; pidfile = fopen(pid_file, "w"); if (pidfile) { fchmod(fileno(pidfile), 0600); fprintf(pidfile, "%d\n", getpid()); fclose(pidfile); } else { syscall_fail("fopen", pid_file, 0); } } /* Main loop! */ while (!stop_poll) { int ns, nt, status; pid_t pid; ircnet_expunge_proxies(); dccnet_expunge_proxies(); ns = net_poll(); nt = timer_poll(); /* Reap any children */ while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { debug("Reaped process %d, exit status %d", pid, status); /* Handle any DNS children */ dns_endrequest(pid, status); } /* Reload the configuration file? */ if (reload_config) { _reload_config(); reload_config = 0; } if (!ns && !nt) break; } if (pid_file) { unlink(pid_file); } /* Free up stuff */ ircnet_flush(); dccnet_flush(); dns_flush(); timer_flush(); /* Do a lingering close on all sockets */ net_closeall(); net_flush(); /* Close down and free up memory */ if (!inetd_mode && !no_daemon) closelog(); free(listen_port); free(pid_file); free(config_file); #ifdef DEBUG_MEMORY mem_report("termination"); #endif /* DEBUG_MEMORY */ return 0; }