Exemple #1
0
void clock_get( struct evhttp_request* req, void* obj )
{
	assert(req);
	assert(_av.clock_get );

	switch(req->type )
		{
		case EVHTTP_REQ_GET:
			{			 
				uint64_t t = (*_av.clock_get)( obj ); 
				uint64_t sec = t / 1e6;
				uint64_t usec = t - (sec*1e6);
				char buf[128];
				snprintf( buf, 128, "\"time\" : %llu.%llu", sec, usec );
				reply_success( req, HTTP_OK, "OK", buf );
			} break;
		case EVHTTP_REQ_HEAD:						
			reply_success( req, HTTP_OK, "OK", NULL);			
			break;		 
		case EVHTTP_REQ_POST:
			reply_error( req, HTTP_NOTMODIFIED, "clock POST not implemented" );
			break;
		default:
			reply_error( req, HTTP_NOTMODIFIED, "unrecognized request type" );
		}
}
Exemple #2
0
static void s1_ready_async(ErlDrvData drv_data, ErlDrvThreadData thread_data)
{
    descriptor_t        *desc = (descriptor_t *) drv_data;
    callstate_t *c = (callstate_t *) thread_data;
    int                 bytes, offset, i;
    char                *p = NULL;
    unsigned long       index = 0;

    edtk_debug("%s: cmd = %d", __FUNCTION__, c->cmd);
    if (c == NULL) {
        edtk_debug("%s: c == NULL", __FUNCTION__);
        return;
    }
    switch (c->cmd) {
    case S1_NULL:
        reply_ok(desc);
        break;
    case S1_OPEN:
        if (! c->o.__expect) {
            reply_error(desc, c->o.__expect_errval);
            break;
        }
        if (find_unused_fd_index(desc, &index) < 0) {
            reply_error(desc, ENOMEM);
        } else {
            desc->valmap_fd[index] = c->o.ret_int;
            reply_ok_valmap(desc, am_valmap_fd, index);
        }
        break;
    case S1_GETFD:
        reply_ok_num(desc, c->o.ret_int);
        break;
    case S1_SENDFD:
        if (c->o.__expect) {
            reply_ok_num(desc, c->o.ret_int_t);
        } else {
            reply_error(desc, c->o.__expect_errval);
        }
        break;
    case S1_RECEIVEFD:
        if (c->o.__expect) {
            reply_ok_num(desc, c->o.ret_int_t);
        } else {
            reply_error(desc, c->o.__expect_errval);
        }
        break;
    case S1_CLOSE:
        if (! c->o.__expect) {
            reply_error(desc, c->o.__expect_errval);
            break;
        }
        cleanup_valmap_fd_index(desc, c->i.__valmap_fd_index, 0);
        reply_ok_num(desc, c->o.ret_int);
        break;
    default:
        edtk_debug("%s: bogus command, should never happen", __FUNCTION__);
        break;
    }
    sys_free(c);
}
Exemple #3
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
static void
wrap_zmq_socket(zmq_drv_t *drv, const uint8_t* bytes, size_t size)
{
    int type = *bytes;

    assert(sizeof(uint8_t) == size);

    zmqdrv_fprintf("socket (type: %d)\r\n", type);

    ErlDrvTermData caller = driver_caller(drv->port);

    if (drv->terminating)
    {
        reply_error(drv->port, caller, ETERM);
        return;
    }

    assert(NULL == drv->get_socket_info(caller));

    // Runtime validation as well in case zmq_drv.erl is used directly rather
    // than through zmq_socket.erl gen_server.
    if (NULL != drv->get_socket_info(caller))
    {
        reply_error(drv->port, caller, EBUSY);
        return;
    }

    void* s = zmq_socket(drv->zmq_context, type);

    if (!s)
    {
        reply_error(drv->port, caller, zmq_errno());
        return;
    }

    //TODO: Support Windows 'SOCKET' type?
    int fd; size_t fd_size = sizeof(fd);
    if (0 != zmq_getsockopt(s, ZMQ_FD, &fd, &fd_size))
    {
        reply_error(drv->port, caller, zmq_errno());
        zmq_close(s);
        return;
    }

    zmq_sock_info* si = new zmq_sock_info(s, (ErlDrvEvent)fd);

    if (!si)
    {
        driver_failure_posix(drv->port, ENOMEM);
        return;
    }

    driver_monitor_process(drv->port, caller, &si->monitor);
    drv->zmq_pid_socket[caller] = si;
    drv->zmq_fd_socket[si->fd] = si;

    zmqdrv_fprintf("socket %p owner %lu\r\n", si->socket, caller);

    reply_ok(drv->port, caller);
}
Exemple #4
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
static void
wrap_zmq_init(zmq_drv_t *drv, const uint8_t* bytes, size_t size)
{
    int io_threads  = *bytes;

    assert(sizeof(uint8_t) == size);

    zmqdrv_fprintf("init (io_threads: %d)\r\n", io_threads);

    // We only support a single zmq context, but zeromq itself supports multiple
    if (drv->zmq_context)
    {
        reply_error(drv->port, driver_caller(drv->port), EBUSY);
        return;
    }

    drv->terminating = false;
    drv->zmq_context = zmq_init(io_threads);

    if (!drv->zmq_context)
    {
        reply_error(drv->port, driver_caller(drv->port), zmq_errno());
        return;
    }

    zmqdrv_fprintf("init %p\r\n", drv->zmq_context);

    reply_ok(drv->port, driver_caller(drv->port));
}
Exemple #5
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
static void
wrap_zmq_connect(zmq_drv_t *drv, const uint8_t* bytes, size_t size)
{
    // expects the endpoint to be zero terminated
    char* endpoint  = (char*)bytes;

    // TODO: check for zero termination within size limit
    assert(sizeof(char) <= size); // Must always have at least the 0 terminating char.

    ErlDrvTermData caller = driver_caller(drv->port);

    if (drv->terminating)
    {
        reply_error(drv->port, caller, ETERM);
        return;
    }

    zmq_sock_info* si = drv->get_socket_info(caller);

    if (!si)
    {
        reply_error(drv->port, caller, ENODEV);
        return;
    }

    zmqdrv_fprintf("connect %p (endpoint: %s)\r\n", si->socket, endpoint);

    if (0 != zmq_connect(si->socket, endpoint))
    {
        reply_error(drv->port, caller, zmq_errno());
        return;
    }

    reply_ok(drv->port, caller);
}
int
rtpp_command_pre_parse(struct cfg *cf, struct rtpp_command *cmd)
{
    struct cmd_props cprops;

    if (fill_cmd_props(cf, cmd, &cprops) != 0) {
        RTPP_LOG(cf->stable->glog, RTPP_LOG_ERR, "unknown command \"%c\"",
          cmd->argv[0][0]);
        reply_error(cmd, ECODE_CMDUNKN);
        return (-1);
    }
    if (cmd->argc < cprops.min_argc || cmd->argc > cprops.max_argc) {
        RTPP_LOG(cf->stable->glog, RTPP_LOG_ERR, "%s command syntax error"
          ": invalid number of arguments (%d)", cmd->cca.rname, cmd->argc);
        reply_error(cmd, ECODE_PARSE_NARGS);
        return (-1);
    }
    if (cprops.has_cmods == 0 && cprops.cmods[0] != '\0') {
        RTPP_LOG(cf->stable->glog, RTPP_LOG_ERR, "%s command syntax error"
          ": modifiers are not supported by the command", cmd->cca.rname);
        reply_error(cmd, ECODE_PARSE_MODS);
        return (-1);
    }
    cmd->cca.call_id = cprops.has_call_id ? cmd->argv[1] : NULL;
    cmd->cca.from_tag = cprops.fpos > 0 ? cmd->argv[cprops.fpos] : NULL;
    cmd->cca.to_tag = cprops.tpos > 0 ? cmd->argv[cprops.tpos] : NULL;
    return (0);
}
Exemple #7
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
static void
wrap_zmq_term(zmq_drv_t *drv)
{
    zmqdrv_fprintf("term %p\r\n", drv->zmq_context);

    if (0 < drv->zmq_pid_socket.size())
    {
        for (zmq_pid_socket_map_t::iterator it = drv->zmq_pid_socket.begin(); it != drv->zmq_pid_socket.end(); ++it)
        {
            zmq_sock_info* si = it->second;

            if (si->busy)
            {
                // Remove socket from erlang vm polling
                driver_select(drv->port, si->fd, ERL_DRV_READ, 0);

                if (si->out_caller)
                {
                    reply_error(drv->port, si->out_caller, ETERM);
                    si->out_caller = 0;
                    zmq_msg_close(&si->out_msg);
                }
                if (si->in_caller)
                {
                    reply_error(drv->port, si->in_caller, ETERM);
                    si->in_caller = 0;
                }
                if (si->poll_caller)
                {
                    send_events(drv->port, si->poll_caller, (uint32_t)ZMQ_POLLERR);
                    si->poll_caller = 0;
                }

                si->busy = false;
            }
        }

        // TODO: Remove if zeromq itself ever gets fixed. As zmq_term() is a
        // blocking call, and will not return until all sockets are closed,
        // so do not allow it to be called while there are open sockets.
        drv->terminating = true;
        reply_error(drv->port, driver_caller(drv->port), EAGAIN);
        return;
    }

    // cross fingers and hope zmq_term() doesn't block, else we hardlock.
    if (0 != zmq_term(drv->zmq_context))
    {
        reply_error(drv->port, driver_caller(drv->port), zmq_errno());
        return;
    }

    drv->zmq_context = NULL;

    reply_ok(drv->port, driver_caller(drv->port));
}
Exemple #8
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
static void
wrap_zmq_recv(zmq_drv_t *drv, const uint8_t* bytes, size_t size)
{
    int flags = *bytes;

    assert(sizeof(uint8_t) == size);

    ErlDrvTermData caller = driver_caller(drv->port);

    if (drv->terminating)
    {
        reply_error(drv->port, caller, ETERM);
        return;
    }

    zmq_sock_info* si = drv->get_socket_info(caller);

    if (!si)
    {
        reply_error(drv->port, caller, ENODEV);
        return;
    }

    assert(0 == si->in_caller);

    zmqdrv_fprintf("recv %p (flags: %d)\r\n", si->socket, flags);

    zmq_msg_t msg;
    zmq_msg_init(&msg);

    if (0 == zmq_recv(si->socket, &msg, flags|ZMQ_NOBLOCK))
    {
        reply_ok_binary(drv->port, caller, zmq_msg_data(&msg), zmq_msg_size(&msg));
    }
    else if (ZMQ_NOBLOCK != (ZMQ_NOBLOCK & flags) && EAGAIN == zmq_errno())
    {
        // Caller requested blocking recv
        // No input available. Make the caller wait by not returning result
        zmqdrv_fprintf("recv %p blocking\r\n", si->socket);

        si->in_flags = flags;
        si->in_caller = caller;

        if (!si->busy)
        {
            driver_select(drv->port, si->fd, ERL_DRV_READ, 1);
            si->busy = true;
        }
    }
    else
    {
        reply_error(drv->port, caller, zmq_errno());
    }

    zmq_msg_close(&msg);
}
Exemple #9
0
void handle_pva_set( struct evhttp_request* req, void* handle )
{
	assert(req);
	assert(handle);

  const size_t buflen = EVBUFFER_LENGTH(req->input_buffer);  
  char* buf = malloc(buflen+1); // space for terminator
  memcpy( buf, EVBUFFER_DATA(req->input_buffer), buflen );  
  buf[buflen] = 0; // string terminator
  
  printf( "received %lu bytes\n", (unsigned long)buflen );
  printf( "   %s\n", buf );

  av_pva_t pva;
  int result = xdr_parse_pva( buf, &pva );

  if( result != 0 )			  
	 reply_error( req, HTTP_NOTMODIFIED, "pva POST failed: failed to parse XDR payload." );						
  else
	 {
		// set the new PVA
		(*_av.pva_set)( handle, &pva );				
		// get the PVA and return it so the client can see what happened
		handle_pva_get( req, handle );
	 }
} 
Exemple #10
0
/** @brief Copy pages then do a (true) software reset.
 *
 * When using CRC check, the page is now written on mismatch.
 *
 * Parameters:
 *  - dest page address (u32), must be aligned and in high 64KB (if relevant)
 *  - source page address (u32), must be aligned and in low 64KB (if relevant)
 *  - page count (u8), must be not null
 */
static void cmd_copy_pages(void)
{
  const addr_type dest = recv_addr();
  const addr_type src  = recv_addr();
  const uint8_t n = recv_u8();

#ifndef DISABLE_STRICT_CHECKS
  if(
      // note: overflows in computations are not handled
      n == 0 ||
      ((dest|src) & ((addr_type)SPM_PAGESIZE-1)) != 0 ||
#ifdef ADDR_SIZE_LARGE
      dest < 0x10000 || src >= 0x10000 ||
#else
      src + n*SPM_PAGESIZE > FLASHEND+1 ||
#endif
      dest + n*SPM_PAGESIZE > FLASHEND+1-SPM_PAGESIZE
    ) {
    reply_error(STATUS_BAD_VALUE);
    return;
  }
#endif

  reply_success(0);
  eeprom_busy_wait();
  roblocop_pgm_copy(dest, src, n);
}
Exemple #11
0
static void
handle_nomem(struct cfg_stable *cf, int fd, struct rtpp_command *cmd,
             int ecode, struct sockaddr **ia, int *fds,
             struct rtpp_session *spa, struct rtpp_session *spb)
{
    int i;

    rtpp_log_write(RTPP_LOG_ERR, cf->glog, "can't allocate memory");

    for (i = 0; i < 2; i++)
        if (ia[i] != NULL)
            free(ia[i]);

    if (spa != NULL) {
        if (spa->call_id != NULL)
            free(spa->call_id);

        free(spa);
    }

    if (spb != NULL)
        free(spb);

    for (i = 0; i < 2; i++)
        if (fds[i] != -1)
            close(fds[i]);

    reply_error(cf, fd, cmd, ecode);
}
Exemple #12
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
static void
wrap_zmq_close(zmq_drv_t *drv)
{
    ErlDrvTermData caller = driver_caller(drv->port);
    zmq_sock_info* si = drv->get_socket_info(caller);

    if (!si)
    {
        reply_error(drv->port, caller, ENODEV);
        return;
    }

    zmqdrv_fprintf("close %p\r\n", si->socket);

    driver_demonitor_process(drv->port, &si->monitor);

    if (si->busy)
    {
        // Remove socket from vm polling
        driver_select(drv->port, si->fd, ERL_DRV_READ, 0);
    }

    drv->zmq_pid_socket.erase(caller);
    drv->zmq_fd_socket.erase(si->fd);

    //zmq_close(Socket) is called in ~zmq_sock_info
    delete si;

    reply_ok(drv->port, caller);
}
Exemple #13
0
static void
handle_nomem(struct cfg *cf, struct rtpp_command *cmd,
  int ecode, struct ul_opts *ulop, int *fds,
  struct rtpp_session *spa, struct rtpp_session *spb)
{
    int i;

    rtpp_log_write(RTPP_LOG_ERR, cf->stable->glog, "can't allocate memory");
    rtpp_command_ul_opts_free(ulop);
    if (spa != NULL) {
        if (spa->call_id != NULL)
            free(spa->call_id);
        if (spa->tag != NULL)
            free(spa->tag);
        if (spa->tag_nomedianum != NULL)
            free(spa->tag_nomedianum);
        free(spa);
    }
    if (spb != NULL)
        free(spb);
    for (i = 0; i < 2; i++)
        if (fds[i] != -1)
            close(fds[i]);
    reply_error(cf, cmd, ecode);
}
Exemple #14
0
/** @brief Common init code for I2C master commands.
 *
 * Read and check the slave address, configure I2C and poll the slave.
 *
 * @return The I2C slave address on success, 0 on failure.
 */
static uint8_t init_cmd_i2c(void)
{
#ifdef ENABLE_I2C_SLAVE
#ifndef DISABLE_STRICT_CHECKS
  // command allowed in UART mode only
  if( proto_send != uart_send ) {
    reply_failure();
    return 0;
  }
#endif
#endif
  const uint8_t addr = recv_u8(); // slave addr
#ifndef DISABLE_STRICT_CHECKS
  if( addr < 0x08 || addr >= 0x78 ) {
    reply_error(STATUS_BAD_VALUE);
    return 0;
  }
#endif

  // configure I2C
  TWBR = I2C_BITRATE;
  TWCR = _BV(TWEN)|_BV(TWINT);
  if(I2C_PRESCALER & 1) TWSR |= _BV(TWPS0);
  if(I2C_PRESCALER & 2) TWSR |= _BV(TWPS1);

  return addr;
}
Exemple #15
0
/*
 * request start - end 
 * if file_size < end;
 * return start - file_size
 * */
void read_chunk(uint64_t chunkid, struct evhttp_request *req)
{
    DBG();
    struct evbuffer *evb = evbuffer_new();
    hdd_chunk *chunk = chunk_hashtable_get(chunkid);

    if (chunk == NULL) {
        reply_error(req, HTTP_NOTFOUND, "not found chunk %" PRIx64 ";",
                    chunkid);
        return;
    }

    hdd_chunk_printf(chunk);

    uint64_t start = 0, end = 0;
    const char *range = evhttp_find_header(req->input_headers, "Range");
    struct stat st;

    logging(LOG_DEUBG, "get range : %s", range);
    int fd = open(chunk->path, O_RDONLY);
    if (fstat(fd, &st) < 0) {
        reply_error(req, HTTP_NOTFOUND, "file not exist : %s", chunk->path);
        return;
    }
    logging(LOG_DEUBG, "st.st_size = : %d", st.st_size);

    if (range) {
        sscanf(range, "bytes=%" SCNu64 "-%" SCNu64, &start, &end);
        //假设文件st_size = 2
        //if end = 0, 应该返回1个字节           end=end
        //if end = 1, 应该返回0,1 共2个字节.    end = st_size - 1  || end = end
        //if end = 2, 还是应该2个字节.          end = st_size - 1
        if (st.st_size <= end)
            end = st.st_size - 1;
    } else {
        start = 0;
        end = st.st_size - 1;
    }
    logging(LOG_DEUBG, "get return range : %" PRIu64 " - %" PRIu64, start, end);
    logging(LOG_DEUBG, "d : %" PRIu64, end - start + 1);

    lseek(fd, start, SEEK_SET);
    evbuffer_add_file(evb, fd, (int)start, end - start + 1);    //如果编译的时候加上 -D_FILE_OFFSET_BITS=64 ,,evbuffer认为length = 0

    evhttp_send_reply(req, HTTP_OK, "OK", evb);
    evbuffer_free(evb);
}
Exemple #16
0
void write_chunk(uint64_t chunkid, struct evhttp_request *req)
{
    DBG();
    struct evbuffer *input;
    struct evbuffer *evb = evbuffer_new();

    if (evhttp_request_get_command(req) != EVHTTP_REQ_POST) {
        reply_error(req, HTTP_BADREQUEST, "should call write with POST");
        return;
    }
    uint64_t start = 0, end;
    const char *range = evhttp_find_header(req->input_headers, "Range");

    logging(LOG_DEUBG, "write Range Header: %s", range);
    if (range) {
        sscanf(range, "bytes=%" SCNu64 "-%" SCNu64, &start, &end);
    }

    input = req->input_buffer;
    hdd_chunk *chunk = hdd_create_chunk(chunkid, 0);    //TODO

    int fd = open(chunk->path, O_WRONLY | O_CREAT, 0755);
    logging(LOG_DEUBG, "write seek to : %" PRIu64 "", start);
    logging(LOG_DEUBG, "evbuffer_get_length(input) = %d",
            evbuffer_get_length(input));
    lseek(fd, start, SEEK_SET);

    if (-1 == fd) {
        reply_error(req, HTTP_INTERNAL, "could not open file : %s",
                    chunk->path);
        return;
    }

    int rst = 0;
    while (evbuffer_get_length(input) && (rst = evbuffer_write(input, fd)) > 0) {
        ;
    }

    /*evbuffer_write(input, fd); */
    close(fd);

    evbuffer_add(evb, "success", strlen("success"));
    evhttp_send_reply(req, HTTP_OK, "OK", evb);
    evbuffer_free(evb);
}
Exemple #17
0
static void
comm_flush(void)				/* flush output to driver */
{
	putc(COM_FLUSH, devout);
	fflush(devout);
	if (getc(devin) != COM_FLUSH)
		reply_error("flush");
	getstate();
}
Exemple #18
0
void handle_data( struct evhttp_request* req, interface_handle_pair_t* ihp )
{	
	assert(req);
	assert(ihp);
	assert(ihp->handle);

	av_interface_t interface = ihp->interface;
	void* handle = ihp->handle;
	
  switch(req->type )
	 {
	 case EVHTTP_REQ_GET:
		if( _av.data_get[interface] && _xdr_format_fn[interface].data )
		  {
				av_msg_t data;
				(*_av.data_get[interface])( handle, &data );			 
				char* xdr = _xdr_format_fn[interface].data( &data );
				assert(xdr);				
				reply_success( req, HTTP_OK, "data GET OK", xdr );
				free(xdr);
		  }
		else			
		  reply_error( req, HTTP_NOTFOUND, "data GET not found: No callback and/or formatter installed for interface" );									
		break;
		
	 case EVHTTP_REQ_HEAD:						
		 reply_success( req, HTTP_OK, "data HEAD OK", NULL );									
		 break;
		 
	 case EVHTTP_REQ_POST:
		{
		  /* 				if( _av.data */
		  /* 				av_data_t data; */
		  /* 				if( parse_xdr_data( req->payload, &data ) != 0 ) */
		  /* 					puts( "ERROR: failed to parse XDR on POST data" ); */
		  /* 				else				 */
		  /* 					(*_av.data_set)( handle, &data );  */
		  
		  reply_error( req, HTTP_NOTMODIFIED, "data POST error: data cannot be set." );									
		}	break;	
	 default:
		reply_error( req, HTTP_NOTMODIFIED, "data unrecognized action" );						
	 }
}
Exemple #19
0
void shutdown_handler(struct evhttp_request *req, void *arg)
{
    DBG();

    if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
        reply_error(req, HTTP_BADREQUEST, "should call with GET");
        return;
    }
    exit(0);
}
Exemple #20
0
void gen_handler(struct evhttp_request *req, void *arg)
{
    DBG();
    /*struct evbuffer *evb = evbuffer_new(); */

    const char *uri = evhttp_request_get_uri(req);
    struct evhttp_uri *decoded_uri = NULL;
    const char *path;
    char *decoded_path;

    /* Decode the URI */
    decoded_uri = evhttp_uri_parse(uri);
    if (!decoded_uri) {
        reply_error(req, HTTP_BADREQUEST, "Bad URI: %s", uri);
        return;
    }

    path = evhttp_uri_get_path(decoded_uri);
    if (!path) {
        logging(LOG_INFO, "request path is nil, replace it as /");
        path = "/";
    }

    decoded_path = evhttp_uridecode(path, 0, NULL);

    uint64_t chunkid;
    char *slash = strchr(path + 1, '/');
    *slash = '\0';
    const char *op = path + 1;
    sscanf(slash + 1, "%" SCNu64, &chunkid);

    logging(LOG_INFO, "%s, %" PRIu64, op, chunkid);

    if (strcmp(op, "put") == 0) {
        write_chunk(chunkid, req);
    } else if (strcmp(op, "get") == 0) {
        read_chunk(chunkid, req);
    } else {
        reply_error(req, HTTP_NOTFOUND, "not found: %s", uri);
        return;
    }
}
Exemple #21
0
void handle_cfg( struct evhttp_request* req, interface_handle_pair_t* ihp )
{	
	av_interface_t interface = ihp->interface;
	void* handle = ihp->handle;

  switch(req->type )
	 {
	 case EVHTTP_REQ_GET:
		if( _av.cfg_get[interface]  && _xdr_format_fn[interface].cfg )
		  {
			 av_msg_t cfg;
			 (*_av.cfg_get[interface])( handle, &cfg );			 
			 char* xdr = _xdr_format_fn[interface].cfg( &cfg );
			 assert(xdr);				
			 reply_success( req, HTTP_OK, "cfg GET OK", xdr );
			 free(xdr);
		  }
		else			
		  reply_error( req, HTTP_NOTFOUND, "cfg GET not found: No callback and/or formatter installed for interface" );									
		break;
		
	 case EVHTTP_REQ_HEAD:						
		 reply_success( req, HTTP_OK, "cfg HEAD OK", NULL );									
		 break;
		 
	 case EVHTTP_REQ_POST:
		{
		  /* 				if( _av.cfg */
		  /* 				av_cfg_t cfg; */
		  /* 				if( parse_xdr_cfg( req->payload, &cfg ) != 0 ) */
		  /* 					puts( "ERROR: failed to parse XDR on POST cfg" ); */
		  /* 				else				 */
		  /* 					(*_av.cfg_set)( handle, &cfg );  */
		  
		  reply_error( req, HTTP_NOTMODIFIED, "cfg POST error: cfg cannot be set." );									
		}	break;	
	 default:
		printf( "warning: unknown request type %d in handle_cfg\n", req->type );
		reply_error( req, HTTP_NOTMODIFIED, "cfg unrecognized action" );						
	 }
}
Exemple #22
0
static int query_voices_req(mrp_dbus_t *dbus, mrp_dbus_msg_t *req,
                            void *user_data)
{
    dbusif_t          *bus = (dbusif_t *)user_data;
    srs_context_t     *srs = bus->self->srs;
    const char        *lang;
    const char        *id;
    int                err;
    srs_client_t      *c;
    srs_voice_actor_t *actors;
    int                nactor;

    err = parse_voice_query(req, &id, &lang);

    if (err != 0) {
        reply_cancel(dbus, req, err, "internal error");

        return TRUE;
    }

    c = client_lookup_by_id(srs, id);

    if (c == NULL) {
        reply_error(dbus, req, 1, "you don't exists, go away");

        return TRUE;
    }

    nactor = client_query_voices(c, lang, &actors);

    if (nactor < 0)
        reply_error(dbus, req, 1, "voice actor query failed");
    else
        reply_voice_query(dbus, req, nactor, actors);

    client_free_queried_voices(actors);

    return TRUE;
}
bool AmBasicSipDialog::onRxReqSanity(const AmSipRequest& req)
{
  // Sanity checks
  if(!remote_tag.empty() && !req.from_tag.empty() &&
     (req.from_tag != remote_tag)){
    DBG("remote_tag = '%s'; req.from_tag = '%s'\n",
	remote_tag.c_str(), req.from_tag.c_str());
    reply_error(req, 481, SIP_REPLY_NOT_EXIST);
    return false;
  }

  if (r_cseq_i && req.cseq <= r_cseq){

    if (req.method == SIP_METH_NOTIFY) {
      if (!AmConfig::IgnoreNotifyLowerCSeq) {
	// clever trick to not break subscription dialog usage
	// for implementations which follow 3265 instead of 5057
	string hdrs = SIP_HDR_COLSP(SIP_HDR_RETRY_AFTER)  "0"  CRLF;

	INFO("remote cseq lower than previous ones - refusing request\n");
	// see 12.2.2
	reply_error(req, 500, SIP_REPLY_SERVER_INTERNAL_ERROR, hdrs);
	return false;
      }
    }
    else {
      INFO("remote cseq lower than previous ones - refusing request\n");
      // see 12.2.2
      reply_error(req, 500, SIP_REPLY_SERVER_INTERNAL_ERROR);
      return false;
    }
  }

  r_cseq = req.cseq;
  r_cseq_i = true;

  return true;
}
Exemple #24
0
void handle_geom( struct evhttp_request* req, void* handle )
{
	assert(req);
	assert(handle);

  switch(req->type )
	 {
	 case EVHTTP_REQ_GET:
		 {
			av_geom_t geom;
			 (*_av.geom_get)( handle, &geom );
			 
			 // encode the GEOM into xdr
			 char* xdr = xdr_format_geom( &geom );
			 assert(xdr);
			 reply_success( req, HTTP_OK, "geom GET OK", xdr);			
			 free(xdr);
		 } break;
	 case EVHTTP_REQ_HEAD:						
		 puts( "warning: geom HEAD not implemented" );
		 reply_success( req, HTTP_OK, "geom HEAD OK", NULL);			
		 break;		 
	 case EVHTTP_REQ_POST:
		 {
			 av_geom_t geom;
			 // todo- parse GEOM from XDR
			 bzero( &geom, sizeof(geom));
			 
			 (*_av.geom_set)( handle, &geom );
			 
			 puts( "warning: geom POST not implemented" );
			 reply_error( req, HTTP_NOTMODIFIED, "geom POST error: not imlemented" );						
		 } break;
	 default:
		 printf( "warning: unknown request type %d in handle_geom\n", req->type );
		 reply_error( req, HTTP_NOTMODIFIED, "geom unrecognized action" );						
	 }
}
Exemple #25
0
static int render_voice_req(mrp_dbus_t *dbus, mrp_dbus_msg_t *req,
                            void *user_data)
{
    dbusif_t      *bus = (dbusif_t *)user_data;
    srs_context_t *srs = bus->self->srs;
    const char    *id, *msg, *voice, *errmsg = NULL;
    double         rate, pitch;
    int            timeout, events, err;
    uint32_t       reqid;
    srs_client_t  *c;

    err = parse_render_voice(req, &id, &msg, &voice, &rate, &pitch, &timeout,
                             &events, &errmsg);

    if (err != 0) {
        reply_error(dbus, req, err, errmsg);

        return TRUE;
    }

    c = client_lookup_by_id(srs, id);

    if (c == NULL) {
        reply_error(dbus, req, 1, "you don't exists, go away");

        return TRUE;
    }

    reqid = client_render_voice(c, msg, voice, rate, pitch, timeout, events);

    if (reqid != SRS_VOICE_INVALID)
        reply_render(dbus, req, reqid);
    else
        reply_error(dbus, req, 1, "voice render request failed");

    return TRUE;
}
Exemple #26
0
void handle_tree( struct evhttp_request* req, void* dummy )
{
	assert(req);
	
	//	printf( "HEADERS: %s\n", req->input_headers );


/* 	struct evkeyval *item = NULL; */
/* 	puts( "HEADERS" ); */
/* 	TAILQ_FOREACH( item, req->input_headers, next ) */
/* 		printf( "HEADER: %s = %s\n", item->key, item->value ); */
	
/* 	const char* accept = evhttp_find_header( req->input_headers, "Accept" ); */

/*   printf( "ACCEPT: %s\n", accept ? accept : "<not found" ); */

	//add_std_hdrs( req );

	switch(req->type )
		{
		case EVHTTP_REQ_GET:
			{			 
				char* xdr = xdr_tree(NULL);				
				assert(xdr);
				reply_success( req, HTTP_OK, "Success", xdr );			
				free(xdr);
			} break;
		case EVHTTP_REQ_HEAD:						
		 reply_success( req, HTTP_OK, "Success", NULL );			
		 break;		 
		case EVHTTP_REQ_POST:
			reply_error( req, HTTP_NOTMODIFIED, "POST tree not implemented" );			 
			break;
		default:
			reply_error( req, HTTP_NOTMODIFIED, "unknown HTTP request type in handle tree" );			 
		}
}
Exemple #27
0
static int
comm_getcur(			/* get and return cursor position */
	int	*xp,
	int	*yp
)
{
	int	c;

	putc(COM_GETCUR, devout);
	fflush(devout);
	if (getc(devin) != COM_GETCUR)
		reply_error("getcur");
	c = getc(devin);
	*xp = getw(devin);
	*yp = getw(devin);
	return(c);
}
Exemple #28
0
/* Check for completed transfers, and remove their easy handles */
void check_multi_info(struct http_m_global *g)
{
	char *eff_url;
	CURLMsg *msg;
	int msgs_left;
	CURL *easy;
	CURLcode res;

	struct http_m_cell *cell;

	LM_DBG("REMAINING: %d\n", g->still_running);
	while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
		if (msg->msg == CURLMSG_DONE) {
			easy = msg->easy_handle;
			res = msg->data.result;
			curl_easy_getinfo(easy, CURLINFO_PRIVATE, &cell);
			curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
			LM_DBG("DONE: %s => (%d) %s\n", eff_url, res, cell->error);

			cell = http_m_cell_lookup(easy);
			if (msg->data.result != 0) {
				LM_ERR("handle %p returned error %d: %s\n", easy, res, cell->error);
				update_stat(errors, 1);
				reply_error(cell);
			} else {
				cell->reply->error[0] = '\0';
				cell->cb(cell->reply, cell->param);

				LM_DBG("reply: [%d] %.*s [%d]\n", (int)cell->reply->retcode, cell->reply->result->len, cell->reply->result->s, cell->reply->result->len);
				update_stat(replies, 1);
			}

			if (cell != 0) {
				LM_DBG("cleaning up cell %p\n", cell);
				unlink_http_m_cell(cell);
				free_http_m_cell(cell);
			}

			LM_DBG("Removing handle %p\n", easy);
			curl_multi_remove_handle(g->multi, easy);
			curl_easy_cleanup(easy);
		}
	}
}
Exemple #29
0
static void
handle_nomem(struct cfg *cf, struct rtpp_command *cmd,
  int ecode, struct ul_opts *ulop, struct rtpp_socket **fds,
  struct rtpp_session *spa)
{
    int i;

    RTPP_LOG(cf->stable->glog, RTPP_LOG_ERR, "can't allocate memory");
    rtpp_command_ul_opts_free(ulop);
    if (spa != NULL) {
        CALL_METHOD(spa->rcnt, decref);
    }
    if (fds != NULL) {
        for (i = 0; i < 2; i++)
            if (fds[i] != NULL)
                CALL_METHOD(fds[i]->rcnt, decref);
    }
    reply_error(cmd, ecode);
}
Exemple #30
0
static void
comm_comin(			/* read string from command line */
	char	*buf,
	char	*prompt
)
{
	putc(COM_COMIN, devout);
	if (prompt == NULL)
		putc(0, devout);
	else {
		putc(1, devout);
		myputs(prompt, devout);
	}
	fflush(devout);
	if (getc(devin) != COM_COMIN)
		reply_error("comin");
	mygets(buf, devin);
	getstate();
}