Example #1
0
File: protocol.c Project: fis/mcmap
packet_t *packet_read(packet_state_t *state)
{
	jint t = buf_getc(state);
	if (t < 0)
		return 0;

	unsigned type = t;

	state->p.type = type;

	struct packet_format_desc *fmt;
	if (type >= MAX_PACKET_FORMAT || !(fmt = &packet_format[t])->known)
	{
#if DEBUG_PROTOCOL >= 1
		log_print("IMMINENT CRASH, reading tail for log");
		unsigned char buf[256];
		for (int i = 0; i < sizeof buf; i++) buf[i] = buf_getc(state);
		for (int i = 0; i < sizeof buf; i+=16)
			log_print("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
			          buf[i+0],buf[i+1],buf[i+2],buf[i+3],buf[i+4],buf[i+5],buf[i+6],buf[i+7],
			          buf[i+8],buf[i+9],buf[i+10],buf[i+11],buf[i+12],buf[i+13],buf[i+14],buf[i+15]);
#endif
		dief("Unknown packet id: 0x%02x", type);
	}

	state->p.field_offset = state->offset;

	for (unsigned f = 0; f < fmt->nfields; f++)
	{
		state->p.field_offset[f] = state->buf_pos - state->buf_start;

		switch (fmt->ftype[f])
		{
		case FIELD_BYTE:
		case FIELD_UBYTE:
			if (!buf_skip(state, 1)) return 0;
			break;

		case FIELD_SHORT:
			if (!buf_skip(state, 2)) return 0;
			break;

		case FIELD_INT:
		case FIELD_FLOAT:
			if (!buf_skip(state, 4)) return 0;
			break;

		case FIELD_LONG:
		case FIELD_DOUBLE:
			if (!buf_skip(state, 8)) return 0;
			break;

		case FIELD_STRING:
			t = buf_get_jshort(state);
			if (!buf_skip(state, t*2)) return 0;
			break;

		case FIELD_ITEM:
			if (!buf_skip_item(state)) return 0;
			break;

		case FIELD_BYTE_ARRAY:
			t = buf_get_jint(state);
			if (!buf_skip(state, t)) return 0;
			break;

		case FIELD_BLOCK_ARRAY:
			t = buf_get_jshort(state);
			if (!buf_skip(state, 4*t)) return 0;
			break;

		case FIELD_ITEM_ARRAY:
			t = buf_get_jshort(state);
			for (int i = 0; i < t; i++)
				if (!buf_skip_item(state)) return 0;
			break;

		case FIELD_EXPLOSION_ARRAY:
			t = buf_get_jint(state);
			// FIXME: Possible over/underflow?
			if (!buf_skip(state, 3*t)) return 0;
			break;

		case FIELD_MAP_ARRAY:
			t = buf_getc(state); // Note: Unsigned
			if (!buf_skip(state, t)) return 0;
			break;

		case FIELD_ENTITY_DATA:
			while (1)
			{
				t = buf_getc(state);
				if (t == 127)
					break;
				switch (t >> 5)
				{
				case 0: if (!buf_skip(state, 1)) return 0; break;
				case 1: if (!buf_skip(state, 2)) return 0; break;
				case 2: case 3: if (!buf_skip(state, 4)) return 0; break;
				case 4: t = buf_get_jshort(state); if (!buf_skip(state, t)) return 0; break;
				case 5: if (!buf_skip(state, 5)) return 0; break;
				}
			}
			break;

		case FIELD_OBJECT_DATA:
			t = buf_get_jint(state);
			if (t > 0)
				if (!buf_skip(state, 6)) return 0; // Skip 3 short
			break;
		}
	}

	state->p.field_offset[fmt->nfields] = state->buf_pos - state->buf_start;

	state->p.size = state->buf_pos - state->buf_start;
	state->p.bytes = &state->buf[state->buf_start];

	state->buf_start = state->buf_pos;

	return &state->p;
}
Example #2
0
/*
 * Fetches the resource denoted by |uri|.
 */
static void fetch_uri(const struct URI *uri)
{
  spdylay_session_callbacks callbacks;
  int fd;
  SSL_CTX *ssl_ctx;
  SSL *ssl;
  struct Request req;
  struct Connection connection;
  int rv;
  nfds_t npollfds = 1;
  struct pollfd pollfds[1];
  uint16_t spdy_proto_version;

  request_init(&req, uri);

  setup_spdylay_callbacks(&callbacks);

  /* Establish connection and setup SSL */
  fd = connect_to(req.host, req.port);
  ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  if(ssl_ctx == NULL) {
    dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL));
  }
  init_ssl_ctx(ssl_ctx, &spdy_proto_version);
  ssl = SSL_new(ssl_ctx);
  if(ssl == NULL) {
    dief("SSL_new", ERR_error_string(ERR_get_error(), NULL));
  }
  /* To simplify the program, we perform SSL/TLS handshake in blocking
     I/O. */
  ssl_handshake(ssl, fd);

  connection.ssl = ssl;
  connection.want_io = IO_NONE;

  /* Here make file descriptor non-block */
  make_non_block(fd);
  set_tcp_nodelay(fd);

  spdylay_printf("[INFO] SPDY protocol version = %d\n", spdy_proto_version);
  rv = spdylay_session_client_new(&connection.session, spdy_proto_version,
                                  &callbacks, &connection);
  if(rv != 0) {
    diec("spdylay_session_client_new", rv);
  }

  /* Submit the HTTP request to the outbound queue. */
  submit_request(&connection, &req);

  pollfds[0].fd = fd;
  ctl_poll(pollfds, &connection);

  /* Event loop */
  while(spdylay_session_want_read(connection.session) ||
        spdylay_session_want_write(connection.session)) {
    int nfds = poll(pollfds, npollfds, -1);
    if(nfds == -1) {
      dief("poll", strerror(errno));
    }
    if(pollfds[0].revents & (POLLIN | POLLOUT)) {
      exec_io(&connection);
    }
    if((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
      die("Connection error");
    }
    ctl_poll(pollfds, &connection);
  }

  /* Resource cleanup */
  spdylay_session_del(connection.session);
  SSL_shutdown(ssl);
  SSL_free(ssl);
  SSL_CTX_free(ssl_ctx);
  shutdown(fd, SHUT_WR);
  close(fd);
  request_free(&req);
}
Example #3
0
int main(int argc, char ** argv){
  int c=0;
  int ret=0;
  playlist_t pl;

  /*  mpc_bool_t mpc_ret=FALSE;*/
  struct option myOpt[] = {
    {"verbose", ARG_NO, 0, 'v'},
    {"quiet", ARG_NO, 0, 'q'},
    {"gain", ARG_YES, 0, 'g'},
    {"audiodevice", ARG_YES, 0, 'a'},
    {"au", ARG_YES, 0, 'u'},
    {"cdr", ARG_YES, 0, 'c'},
    {"wav", ARG_YES, 0, 'w'},
    {"list", ARG_YES, 0, '@'},
    {"random", ARG_NO, 0, 'Z'},
    {"shuffle", ARG_NO, 0, 'z'},
    {"help", ARG_NO, 0, 'h'},
    {"version", ARG_NO, 0, 'V'},
    {NULL, 0, 0, '\0'}
  };

  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);

#ifdef DEBUG
  /* see glibc-doc, chapter "Debugging Memory Allocation"
   * for the use of mcheck.h, mtrace(), MALLOC_TRACE and mtrace(1);
   */
  mtrace();
  debugf("mpc123 compiled with DEBUG=%d enabled", DEBUG);
  if(!getenv("MALLOC_TRACE")){
    debug("WARNING: you have not set a MALLOC_TRACE destination file");
  }else{
    debug("* * malloc_trace activated; to check mpc123 for memory leaks,");
    debugf("* * type the following command: \"mtrace %s %s\"",
           argv[0], getenv("MALLOC_TRACE"));
  }
#endif

  while ((c=getopt_long(argc, argv, _GETOPT_FLAGS, myOpt, NULL)) != -1){
    switch(c){
      case 'z':			/* shuffle the pl once */
        options.shuffle=1;
        break;
      case 'Z':			/* choose a random entry from pl each time */
        options.random=1;
        break;
      case 'v':			/* be logorroical */
        options.verbosity++;
        break;
      case 'q':			/* be asocial */
        options.verbosity=0;
        break;
      case '@':     /* playlist */
        options.playlist=optarg;
        break;
      case 'o':     /* output driver */
        options.ao_driver=optarg;
        break;
      case 'a':     /* libao device to use */
        options.ao_dev=optarg;
        break;
      case 'w':     /* output to .wav */
        options.ao_driver="wav";
        options.foutput=optarg;
        break;
      case 'u':     /* output to .au */
        options.ao_driver="au";
        options.foutput=optarg;
        break;
      case 'c':     /* output to raw pcm */
        options.ao_driver="raw";
        options.foutput=optarg;
      case 'g':     /* set gain */
        options.volume=atof(optarg)/100.0;
        if(options.volume<0.0 || options.volume>1.0){
          dief("Value %s out of range, try values between 0 and 100\n", optarg);
        }
        break;
      case 'V':
        version();
        exit(0);
        break;
      case 'h':			/* need help */
      default:
        usage(argv[0]);
        exit(1);
    }
  }

  /* intialize singals' hendler */
  mpc123_initsigh();

  /* set default ao_driver and ao_device if none were
   * specified on the command line
   */
  if( !options.ao_driver ){
    options.ao_driver="oss";
  }
  if( !options.ao_dev ){
    /* if the driver is set, but not the output device, use
     * the default devices */
    mpc123_choose_default_dev_by_driver();
  }

  /* no playlist? and no files on the command line either? too bad! */
  if( !(argv[optind] || options.playlist) ){
    usage(argv[0]);
    exit(1);
  }

  say(1, COPYRIGHT_NOTICE "\n");

  /* dump options */
  debugf("opts.shuffle=%d\n"
         "  opts.random=%d\n"
         "  opts.verbosity=%d\n"
         "  opts.foutput=\"%s\"\n"
         "  opts.playlist=\"%s\"\n"
         "  opts.ao_driver=\"%s\"\n"
         "  opts.ao_dev=\"%s\"\n"
         "  opts.volume=%f",
         options.shuffle, options.random, options.verbosity,
         options.foutput, options.playlist, options.ao_driver,
         options.ao_dev, options.volume);

  /* optind? :) */
  debugf("argv[optind (%d)]=\"%s\"", optind, argv[optind]);

  /* prepare the playlist */
  if(options.playlist){
    if((ret=populate_playlist_from_file(&pl, options.playlist)))
      dief("Error populating playlist from file \"%s\": error %d\n",
           options.playlist, ret);
  }else{
    if((ret=populate_playlist_from_argv(&pl, argv, optind, argc-optind)))
      dief("Error populating playlist from command line: error %d\n",
           ret);
  }

  do_play_playlist(&pl);
  free_playlist(&pl);
  return 0;
}
Example #4
0
void set_file_var(var_t *var, unsigned int id, unsigned long long mult, char *filename, char *format) {
	file_t *file = NULL;
	unsigned int i, size;

	// find file in file_list
	for (i=0; i < file_list_len; i++) {
		file = &(file_list[i]);
		if (strcmp(file->name, filename) == 0)
			break;
	}

	var->id = id;
	var->mult = mult;
	var->filebuf = file->buffer;

	size = parse_scan_line(&var->type, &var->is_counter, &var->format, format);
	var->var = smalloc(size);
	var->last = smalloc(size);

	switch (var->type) {
		case string_type:
			var->func = get_string;
			memset(var->var, 0, size);
			memset(var->last, 0, size);
			break;
		case char_type:
			var->func = get_char;
			*(char *) var->var = 0;
			*(char *) var->last = 0;
			break;
		case uchar_type:
			var->func = get_uchar;
			*(unsigned char *) var->var = 0;
			*(unsigned char *) var->last = 0;
			break;
		case short_type:
			var->func = get_short;
			*(short *) var->var = 0;
			*(short *) var->last = 0;
			break;
		case ushort_type:
			var->func = get_ushort;
			*(unsigned short *) var->var = 0;
			*(unsigned short *) var->last = 0;
			break;
		case int_type:
			var->func = get_int;
			*(int *) var->var = 0;
			*(int *) var->last = 0;
			break;
		case uint_type:
			var->func = get_uint;
			*(unsigned int *) var->var = 0;
			*(unsigned int *) var->last = 0;
			break;
		case long_type:
			var->func = get_long;
			*(long *) var->var = 0;
			*(long *) var->last = 0;
			break;
		case ulong_type:
			var->func = get_ulong;
			*(unsigned long *) var->var = 0;
			*(unsigned long *) var->last = 0;
			break;
		case longlong_type:
			var->func = get_longlong;
			*(long long *) var->var = 0;
			*(long long *) var->last = 0;
			break;
		case ulonglong_type:
			var->func = get_ulonglong;
			*(unsigned long long *) var->var = 0;
			*(unsigned long long *) var->last = 0;
			break;
		case float_type:
			var->func = get_float;
			*(float *) var->var = 0.0;
			*(float *) var->last = 0.0;
			break;
		case double_type:
			var->func = get_double;
			*(double *) var->var = 0.0;
			*(double *) var->last = 0.0;
			break;
		case longdouble_type:
			var->func = get_longdouble;
			*(long double *) var->var = 0.0;
			*(long double *) var->last = 0.0;
			break;
		default:
			dief("invalid var type");
			break;
	}
}
Example #5
0
/*
 * Fetches the resource denoted by |uri|.
 */
static void fetch_uri(const struct URI *uri)
{
  spdylay_session_callbacks callbacks;
  int fd;
  struct Request req;
  struct Connection connection;
  int rv;
  nfds_t npollfds = 1;
  struct pollfd pollfds[1];
  uint16_t spdy_proto_version = 3;

  request_init(&req, uri);

  setup_spdylay_callbacks(&callbacks);

  /* Establish connection and setup SSL */
  fd = connect_to(req.host, req.port);
  if (-1 == fd)
    abort ();

  connection.fd = fd;
  connection.want_io = IO_NONE;

  /* Here make file descriptor non-block */
  make_non_block(fd);
  set_tcp_nodelay(fd);

  printf("[INFO] SPDY protocol version = %d\n", spdy_proto_version);
  rv = spdylay_session_client_new(&connection.session, spdy_proto_version,
                                  &callbacks, &connection);
  if(rv != 0) {
    diec("spdylay_session_client_new", rv);
  }

  /* Submit the HTTP request to the outbound queue. */
  submit_request(&connection, &req);

  pollfds[0].fd = fd;
  ctl_poll(pollfds, &connection);

  /* Event loop */
  while(spdylay_session_want_read(connection.session) ||
        spdylay_session_want_write(connection.session)) {
    int nfds = poll(pollfds, npollfds, -1);
    if(nfds == -1) {
      dief("poll", strerror(errno));
    }
    if(pollfds[0].revents & (POLLIN | POLLOUT)) {
      exec_io(&connection);
    }
    if((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
      die("Connection error");
    }
    ctl_poll(pollfds, &connection);
  }

  /* Resource cleanup */
  spdylay_session_del(connection.session);
  shutdown(fd, SHUT_WR);
  MHD_socket_close_(fd);
  request_free(&req);
}
Example #6
0
static void set_cloexec(int fd)
{
    if (fcntl(fd, F_SETFD, O_CLOEXEC) == -1)
        dief("failed to set O_CLOEXEC to fd %d", fd);
}