static int write_dev(HWAVEOUT handle, struct buffer *buffer) { MMRESULT error; buffer->wave_header.lpData = buffer->pcm_data; buffer->wave_header.dwBufferLength = buffer->pcm_length; buffer->wave_header.dwUser = (DWORD) buffer; buffer->wave_header.dwFlags = 0; error = waveOutPrepareHeader(handle, &buffer->wave_header, sizeof(buffer->wave_header)); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } error = waveOutWrite(handle, &buffer->wave_header, sizeof(buffer->wave_header)); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } buffer->playing = 1; return 0; }
static int set_pause(int flag) { static int paused; MMRESULT error; if (flag && !paused) { error = waveOutPause(wave_handle); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } } else if (!flag && paused) { error = waveOutRestart(wave_handle); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } } paused = flag; return 0; }
static void display_abort_message(an_error_code err_code) /* Display the reason that the runtime is aborting execution. */ { fprintf(stderr, "%s: %s\n", error_text(ec_abort_header), error_text(err_code)); } /* display_abort_message */
void get_reply_status( str *status, struct sip_msg *reply, int code ) { str phrase; status->s=0; if (reply==0) { LM_CRIT("called with 0 msg\n"); return; } if (reply==FAKED_REPLY) { phrase.s=error_text(code); phrase.len=strlen(phrase.s); } else { phrase=reply->first_line.u.reply.reason; } status->len=phrase.len+3/*code*/+1/*space*/; status->s=pkg_malloc(status->len+1/*ZT */); if (!status->s) { LM_ERR("no pkg mem\n"); return; } status->s[3]=' '; status->s[2]='0'+code % 10; code=code/10; status->s[1]='0'+code% 10; code=code/10; status->s[0]='0'+code % 10; memcpy(&status->s[4], phrase.s, phrase.len); status->s[status->len]=0; }
int acc_pvel_to_acc_param(struct sip_msg* rq, pv_elem_t* pv_el, struct acc_param* accp) { str buf; if(pv_printf_s(rq, pv_el, &buf) < 0) { LM_ERR("Cannot parse comment\n"); return 1; } accp->reason = buf; if (accp->reason.len>=3 && isdigit((int)buf.s[0]) && isdigit((int)buf.s[1]) && isdigit((int)buf.s[2]) ) { /* reply code is in the comment string */ accp->code = (buf.s[0]-'0')*100 + (buf.s[1]-'0')*10 + (buf.s[2]-'0'); accp->code_s.s = buf.s; accp->code_s.len = 3; accp->reason.s += 3; accp->reason.len -= 3; for( ; isspace((int)accp->reason.s[0]) ; accp->reason.s++,accp->reason.len-- ); } else { /* no reply code */ accp->code = 0; accp->code_s.s = NULL; accp->code_s.len = 0; } /*Default comment if none supplied*/ if (accp->reason.len <= 0) { accp->reason.s = error_text(accp->code); accp->reason.len = strlen(accp->reason.s); } return 0; }
static inline void env_set_code_status( int code, struct sip_msg *reply) { static char code_buf[INT2STR_MAX_LEN]; str reason = {"Reason", 6}; struct hdr_field *hf; acc_env.code = code; if (reply==FAKED_REPLY || reply==NULL) { /* code */ acc_env.code_s.s = int2bstr((unsigned long)code, code_buf, &acc_env.code_s.len); /* reason */ acc_env.reason.s = error_text(code); acc_env.reason.len = strlen(acc_env.reason.s); } else { acc_env.code_s = reply->first_line.u.reply.status; hf = NULL; if (reason_from_hf) { /* TODO: take reason from all Reason headers */ if(parse_headers(reply, HDR_EOH_F, 0) < 0) { LM_ERR("error parsing headers\n"); } else { for (hf=reply->headers; hf; hf=hf->next) { if (cmp_hdrname_str(&hf->name, &reason)==0) break; } } } if (hf == NULL) { acc_env.reason = reply->first_line.u.reply.reason; } else { acc_env.reason = hf->body; } } }
int pv_get_tm_reply_reason(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { struct cell *t; struct sip_msg *reply; int branch; if(msg==NULL || res==NULL) return -1; /* first get the transaction */ if (_tmx_tmb.t_check( msg , 0 )==-1) return -1; if ( (t=_tmx_tmb.t_gett())==0) { /* no T */ return pv_get_strempty(msg, param, res); } else { switch (get_route_type()) { case CORE_ONREPLY_ROUTE: /* t_check() above has the side effect of setting T and REFerencing T => we must unref and unset it for the main/core onreply_route. */ _tmx_tmb.t_unref(msg); /* no break */ case TM_ONREPLY_ROUTE: /* use the reason of the current reply */ res->rs.s = msg->first_line.u.reply.reason.s; res->rs.len = msg->first_line.u.reply.reason.len; break; case FAILURE_ROUTE: /* use the reason of the winning reply */ if ( (branch=_tmx_tmb.t_get_picked_branch())<0 ) { LM_CRIT("no picked branch (%d) for a final response" " in MODE_ONFAILURE\n", branch); return -1; } reply = t->uac[branch].reply; if (reply == FAKED_REPLY) { res->rs.s = error_text(t->uac[branch].last_received); res->rs.len = strlen(res->rs.s); } else { res->rs.s = reply->first_line.u.reply.reason.s; res->rs.len = reply->first_line.u.reply.reason.len; } break; default: LM_ERR("unsupported route_type %d\n", get_route_type()); return -1; } } LM_DBG("reply reason is [%.*s]\n", res->rs.len, res->rs.s); res->flags = PV_VAL_STR; return 0; }
static int read_block_by_offset(int fd, int blksize, uint64_t offset, char *buffer) { if (lseek(fd, offset, SEEK_SET) == -1) { /* Seek error. */ error_text("Error: Could not seek to %llu: %s!\n", (unsigned long long) offset); return -1; } misc_read(fd, buffer, blksize); return 0; }
static int close_dev(HWAVEOUT handle) { MMRESULT error; opened = 0; error = waveOutClose(handle); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } return 0; }
void MainWindow::handleCanceledFEA() { feaTerminated = true; QString error_text(feaProcess->readAllStandardError()); feaProcess->kill(); removeTmpFiles(); if (progress) { progress->done(QDialog::Accepted); delete progress; } if (!error_text.isEmpty()) { QMessageBox::critical(this, "FEA exited with error(s)", error_text); } statusBar()->showMessage(tr("Analysis aborted"), 2000); setEnabled(true); }
static inline void env_set_code_status( int code, struct sip_msg *reply) { static char code_buf[INT2STR_MAX_LEN]; acc_env.code = code; if (reply==FAKED_REPLY || reply==NULL) { /* code */ acc_env.code_s.s = int2bstr((unsigned long)code, code_buf, &acc_env.code_s.len); /* reason */ acc_env.reason.s = error_text(code); acc_env.reason.len = strlen(acc_env.reason.s); } else { acc_env.code_s = reply->first_line.u.reply.status; acc_env.reason = reply->first_line.u.reply.reason; } }
static int stop(struct audio_stop *stop) { int result = 0; MMRESULT error; if (stop->flush) { error = waveOutReset(wave_handle); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); result = -1; } output[bindex].pcm_length = 0; return result; } return set_pause(1); }
static int open_dev(HWAVEOUT *handle, unsigned int channels, unsigned int speed, unsigned int depth) { WAVEFORMATEX format; MMRESULT error; set_format(&format, channels, speed, depth); error = waveOutOpen(handle, WAVE_MAPPER, &format, (DWORD) callback, 0, CALLBACK_FUNCTION); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } opened = 1; return 0; }
ID3D10Blob *compile_hlsl(const char *code) { const bool is_ps=strstr(code,"SV_TARGET")!=0; const char *profile=is_ps?"ps_4_0_level_9_3":"vs_4_0_level_9_3"; ID3D10Blob *compiled=0, *error=0; D3DCompile(code,strlen(code),0,0,0,"main",profile,0,0,&compiled,&error); if(error) { fprintf(stderr,"Error: can`t compile shader with profile %s\n",profile); std::string error_text((const char *)error->GetBufferPointer(),error->GetBufferSize()); fprintf(stderr,"%s\n",error_text.c_str()); error->Release(); return 0; } if(!compiled) fprintf(stderr,"Error: compile error\n"); return compiled; }
/* t_uac callback */ static void rpc_uac_callback(struct cell* t, int type, struct tmcb_params* ps) { rpc_delayed_ctx_t* dctx; str text; rpc_t* rpc; void* c; int code; str* preason; dctx=(rpc_delayed_ctx_t*)*ps->param; *ps->param=0; if (dctx==0){ BUG("null delayed reply ctx\n"); return; } rpc=&dctx->rpc; c=dctx->reply_ctx; if (ps->rpl==FAKED_REPLY) { text.s=error_text(ps->code); text.len=strlen(text.s); code=ps->code; preason=&text; rpc->add(c, "dS", code, preason); rpc->add(c, "s", ""); /* request uri (rpc_print_uris)*/ rpc->add(c, "s", ""); /* next hop (rpc_print_uris) */ rpc->add(c, "s", ""); /* dialog routes (rpc_print_routes) */ rpc->add(c, "s", ""); /* rest of the reply */ }else{ code=ps->rpl->first_line.u.reply.statuscode; preason=&ps->rpl->first_line.u.reply.reason; rpc->add(c, "dS", code, preason); rpc_print_uris(rpc, c, ps->rpl); /* print all the reply (from the first header) */ rpc->add(c, "s", ps->rpl->headers->name.s); } rpc->delayed_ctx_close(dctx); ps->param=0; }
int gdsql_log_tail(int level) { FILE* f; f = get_stream(); if (f == 0) return level; if (level == LOG_FATAL || level == LOG_ERROR) { int errnum = errno; const char* errtxt = error_text(errnum); if (errtxt == 0) errtxt = "UNKNOWN"; fprintf(f, " (%d: %s)\n", errnum, errtxt); if (level == LOG_FATAL) stack_trace(level, 2); } else if (level != LOG_ALWAYS) fprintf(f, "\n"); fflush(f); return level; }
static int wait(struct buffer *buffer) { MMRESULT error; if (!buffer->playing) return 0; switch (WaitForSingleObject(buffer->event_handle, INFINITE)) { case WAIT_OBJECT_0: break; case WAIT_ABANDONED: audio_error = _("wait abandoned"); return -1; case WAIT_TIMEOUT: audio_error = _("wait timeout"); return -1; case WAIT_FAILED: default: audio_error = _("wait failed"); return -1; } buffer->playing = 0; error = waveOutUnprepareHeader(wave_handle, &buffer->wave_header, sizeof(buffer->wave_header)); if (error != MMSYSERR_NOERROR) { audio_error = error_text(error); return -1; } return 0; }
/* this is the code which decides what and when shall be relayed upstream; note well -- it assumes it is entered locked with REPLY_LOCK and it returns unlocked! */ enum rps relay_reply( struct cell *t, struct sip_msg *p_msg, int branch, unsigned int msg_status, branch_bm_t *cancel_bitmap ) { int relay; int save_clone; char *buf; /* length of outbound reply */ unsigned int res_len; int relayed_code; struct sip_msg *relayed_msg; struct bookmark bm; int totag_retr; enum rps reply_status; /* retransmission structure of outbound reply and request */ struct retr_buf *uas_rb; str cb_s; str text; /* keep compiler warnings about use of uninit vars silent */ res_len=0; buf=0; relayed_msg=0; relayed_code=0; totag_retr=0; /* remember, what was sent upstream to know whether we are * forwarding a first final reply or not */ /* *** store and relay message as needed *** */ reply_status = t_should_relay_response(t, msg_status, branch, &save_clone, &relay, cancel_bitmap, p_msg ); LM_DBG("branch=%d, save=%d, relay=%d\n", branch, save_clone, relay ); /* store the message if needed */ if (save_clone) /* save for later use, typically branch picking */ { if (!store_reply( t, branch, p_msg )) goto error01; } uas_rb = & t->uas.response; if (relay >= 0 ) { /* initialize sockets for outbound reply */ uas_rb->activ_type=msg_status; t->relaied_reply_branch = relay; /* try building the outbound reply from either the current * or a stored message */ relayed_msg = branch==relay ? p_msg : t->uac[relay].reply; if (relayed_msg==FAKED_REPLY) { relayed_code = branch==relay ? msg_status : t->uac[relay].last_received; text.s = error_text(relayed_code); text.len = strlen(text.s); /* FIXME - bogdan*/ if (relayed_code>=180 && t->uas.request->to && (get_to(t->uas.request)->tag_value.s==0 || get_to(t->uas.request)->tag_value.len==0)) { calc_crc_suffix( t->uas.request, tm_tag_suffix ); buf = build_res_buf_from_sip_req( relayed_code, &text, &tm_tag, t->uas.request, &res_len, &bm ); } else { buf = build_res_buf_from_sip_req( relayed_code, &text, 0/* no to-tag */, t->uas.request, &res_len, &bm ); } } else { /* run callbacks for all types of responses - * even if they are shmem-ed or not */ if (has_tran_tmcbs(t,TMCB_RESPONSE_FWDED) ) { run_trans_callbacks( TMCB_RESPONSE_FWDED, t, t->uas.request, relayed_msg, msg_status ); } relayed_code=relayed_msg->REPLY_STATUS; buf = build_res_buf_from_sip_res( relayed_msg, &res_len, uas_rb->dst.send_sock); /* remove all lumps which are not in shm * added either by build_res_buf_from_sip_res, or by * the callbacks that have been called with shmem-ed messages - vlad */ if (branch!=relay) { del_notflaged_lumps( &(relayed_msg->add_rm), LUMPFLAG_SHMEM); del_notflaged_lumps( &(relayed_msg->body_lumps), LUMPFLAG_SHMEM); } } if (!buf) { LM_ERR("no mem for outbound reply buffer\n"); goto error02; } /* attempt to copy the message to UAS's shmem: - copy to-tag for ACK matching as well - allocate little a bit more for provisional as larger messages are likely to follow and we will be able to reuse the memory frag */ uas_rb->buffer.s = (char*)shm_resize( uas_rb->buffer.s, res_len + (msg_status<200 ? REPLY_OVERBUFFER_LEN : 0)); if (!uas_rb->buffer.s) { LM_ERR("no more share memory\n"); goto error03; } uas_rb->buffer.len = res_len; memcpy( uas_rb->buffer.s, buf, res_len ); if (relayed_msg==FAKED_REPLY) { /* to-tags for local replies */ update_local_tags(t, &bm, uas_rb->buffer.s, buf); } stats_trans_rpl( relayed_code, (relayed_msg==FAKED_REPLY)?1:0 ); /* update the status ... */ t->uas.status = relayed_code; if (is_invite(t) && relayed_msg!=FAKED_REPLY && relayed_code>=200 && relayed_code < 300 && has_tran_tmcbs( t, TMCB_RESPONSE_OUT|TMCB_RESPONSE_PRE_OUT)) { totag_retr=update_totag_set(t, relayed_msg); } }; /* if relay ... */ UNLOCK_REPLIES( t ); /* Setup retransmission timer _before_ the reply is sent * to avoid race conditions */ if (reply_status == RPS_COMPLETED) { /* for auth related replies, we do not do retransmission (via set_final_timer()), but only wait for a final reply (put_on_wait() ) - see RFC 3261 (26.3.2.4 DoS Protection) */ if ((relayed_code != 401) && (relayed_code != 407)) set_final_timer(t); else put_on_wait(t); } /* send it now (from the private buffer) */ if (relay >= 0) { /* run the PRE sending out callback */ if (!totag_retr && has_tran_tmcbs(t, TMCB_RESPONSE_PRE_OUT) ) { cb_s.s = buf; cb_s.len = res_len; set_extra_tmcb_params( &cb_s, &uas_rb->dst); run_trans_callbacks_locked(TMCB_RESPONSE_PRE_OUT,t,t->uas.request, relayed_msg, relayed_code); } SEND_PR_BUFFER( uas_rb, buf, res_len ); LM_DBG("sent buf=%p: %.9s..., shmem=%p: %.9s\n", buf, buf, uas_rb->buffer.s, uas_rb->buffer.s ); /* run the POST sending out callback */ if (!totag_retr && has_tran_tmcbs(t, TMCB_RESPONSE_OUT) ) { cb_s.s = buf; cb_s.len = res_len; set_extra_tmcb_params( &cb_s, &uas_rb->dst); run_trans_callbacks_locked( TMCB_RESPONSE_OUT, t, t->uas.request, relayed_msg, relayed_code); } pkg_free( buf ); } /* success */ return reply_status; error03: pkg_free( buf ); error02: if (save_clone) { if (t->uac[branch].reply!=FAKED_REPLY) sip_msg_free( t->uac[branch].reply ); t->uac[branch].reply = NULL; } error01: text.s = "Reply processing error"; text.len = sizeof("Reply processing error")-1; t_reply_unsafe( t, t->uas.request, 500, &text ); UNLOCK_REPLIES(t); if (is_invite(t)) cancel_uacs( t, *cancel_bitmap ); /* a serious error occurred -- attempt to send an error reply; it will take care of clean-ups */ /* failure */ return RPS_ERROR; }
int main(int argc, char *argv[]) { auto log = std::unique_ptr<sammy::log>(new sammy::log()); log->info("Starting server."); unsigned int request_id = 1; addrinfo* res = create_addrinfo(); int sockfd = create_socket(res); set_socket_reusable(sockfd); set_socket_bind(sockfd, res); set_socket_non_blocking(sockfd); set_socket_listen(sockfd); int efd = create_epoll(); set_epoll_interface(efd, sockfd); // Init the domain config loading auto domain_storage = std::make_shared<sammy::domain_storage>(); if(domain_storage->errors()) { log->error("Error loading config."); exit(1); } // Init cache auto cache = std::make_shared<sammy::cache_storage>(); // Init events epoll_event* events = new epoll_event[MAX_EPOLL_EVENTS]; sammy::thread::pool thread_pool; std::map<int, std::string> client_ip_address; log->info("Server started"); while( true ) { const int event_count = epoll_wait(efd, events, MAX_EPOLL_EVENTS, -1); for(int i = 0; i < event_count; ++i) { const epoll_event& event = events[i]; if( (event.events & EPOLLERR) || (event.events & EPOLLHUP) || (!(event.events & EPOLLIN))) { std::cout << "Error in the file descriptor." << std::endl; close(event.data.fd); continue; } else if(sockfd == event.data.fd) { while( true ) { sockaddr_in cli_addr; socklen_t clilen{ sizeof(cli_addr) }; // Accept the the request int newsockfd = accept(sockfd, (sockaddr*)&cli_addr, &clilen); if(newsockfd == -1) { break; } set_socket_non_blocking(sockfd); set_epoll_interface(efd, newsockfd); std::string out; inet_ntop(AF_INET, &(cli_addr.sin_addr), &out[0], INET_ADDRSTRLEN); client_ip_address[newsockfd] = out; } continue; } else { int newsockfd = event.data.fd; std::string client_address = client_ip_address[newsockfd]; std::string request_str = ""; size_t read_result = 0; read_request(newsockfd, read_result, request_str); // If the read result isn't ok if(read_result == -1) { log->error("Error in read result, error #" + std::to_string(errno)); close(newsockfd); continue; } // The request string is empty if(request_str.empty()) { log->error("Request string empty."); close(newsockfd); continue; } // Parse request string auto client_request = std::make_shared<sammy::request>(request_id, request_str); if(client_request->errors()) { log->error("Request parse error: " + client_request->error_text()); close(newsockfd); continue; } request_id++; // Load domain configuration std::string response = ""; auto domain = domain_storage->get_domain(client_request->get_host()); // If this server does not serve the domain being asked for, we close the connection. if(!domain) { log->error(client_address + " : " + client_request->get_host() + " : " + client_request->get_method() + " : " + client_request->get_path() + " : Domain not served."); close(newsockfd); continue; } // If the client address is blacklisted, we close the connection. if(domain->is_blacklisted(client_address)) { log->error(client_address + " : " + client_request->get_host() + " : " + client_request->get_method() + " : " + client_request->get_path() + " : IP blacklisted."); close(newsockfd); continue; } // Add the task, to be processed by the thread pool when possible thread_pool.add_task(std::make_shared<sammy::thread::task>(newsockfd, domain, client_request, cache, client_address)); continue; } } } delete [] events; return 0; }