Пример #1
0
bool send_request(connection_t *c, const char *format, ...) {
	va_list args;
	char request[MAXBUFSIZE];
	int len;

	/* Use vsnprintf instead of vxasprintf: faster, no memory
	   fragmentation, cleanup is automatic, and there is a limit on the
	   input buffer anyway */

	va_start(args, format);
	len = vsnprintf(request, MAXBUFSIZE, format, args);
	va_end(args);

	if(len < 0 || len > MAXBUFSIZE - 1) {
		logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
			   c->name, c->hostname);
		return false;
	}

	logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[atoi(request)], c->name, c->hostname, request);

	request[len++] = '\n';

	if(c == everyone) {
		broadcast_meta(NULL, request, len);
		return true;
	} else
		return send_meta(c, request, len);
}
Пример #2
0
ReadStatus MP4Reader::parse() {
  mp4AudioSpecificConfig cfg;
  FXuchar* buffer;
  FXuint   size;
  FXint    ntracks;

  FXASSERT(handle==NULL);
  FXASSERT(packet);

  handle = mp4ff_open_read(&callback);
  if (handle==NULL)
    goto error;

  ntracks = mp4ff_total_tracks(handle);
  if (ntracks<=0)
    goto error;

  for (FXint i=0;i<ntracks;i++) {
    if ((mp4ff_get_decoder_config(handle,i,&buffer,&size)==0) && buffer && size) {
      if (NeAACDecAudioSpecificConfig(buffer,size,&cfg)==0) {

        af.set(AP_FORMAT_S16,mp4ff_get_sample_rate(handle,i),mp4ff_get_channel_count(handle,i));
        af.debug();

        if (size>packet->space()) {
          GM_DEBUG_PRINT("MP4 config buffer is too big for decoder packet");
          free(buffer);
          goto error;
          }

        track=i;
        frame=0;
        nframes=mp4ff_num_samples(handle,i);
        stream_length=mp4ff_get_track_duration(handle,i);


        packet->append(buffer,size);
        packet->flags|=AAC_FLAG_CONFIG|AAC_FLAG_FRAME;
        engine->decoder->post(new ConfigureEvent(af,Codec::AAC));

        send_meta();

        engine->decoder->post(packet);

        packet=NULL;
        flags|=FLAG_PARSED;
        free(buffer);
        return ReadOk;
        }
      free(buffer);
      }
    }

error:
  packet->unref();
  return ReadError;
  }
Пример #3
0
void broadcast_meta(connection_t *from, const char *buffer, int length) {
	avl_node_t *node;
	connection_t *c;

	for(node = connection_tree->head; node; node = node->next) {
		c = node->data;

		if(c != from && c->status.active)
			send_meta(c, buffer, length);
	}
}
Пример #4
0
bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
	/* If there already is a lot of data in the outbuf buffer, discard this packet.
           We use a very simple Random Early Drop algorithm. */

	if(2.0 * c->outbuflen / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
		return true;

	if(!send_request(c, "%d %hd", PACKET, packet->len))
		return false;

	return send_meta(c, (char *)packet->data, packet->len) && flush_meta(c);
}
Пример #5
0
static void
cockpit_internal_metrics_tick (CockpitMetrics *metrics,
                               gint64 timestamp)
{
  CockpitInternalMetrics *self = (CockpitInternalMetrics *)metrics;
  struct timeval now_timeval;
  gint64 now;

  gettimeofday (&now_timeval, NULL);
  now = timestamp_from_timeval (&now_timeval);

  /* Reset samples
   */
  for (int i = 0; i < self->n_metrics; i++)
    {
      MetricInfo *info = &self->metrics[i];
      if (info->desc->instanced)
        g_hash_table_foreach (info->instances, instance_reset, NULL);
      else
        info->value = NAN;
    }

  /* Sample
   */
  if (self->samplers & CPU_SAMPLER)
    cockpit_cpu_samples (COCKPIT_SAMPLES (self));
  if (self->samplers & MEMORY_SAMPLER)
    cockpit_memory_samples (COCKPIT_SAMPLES (self));
  if (self->samplers & BLOCK_SAMPLER)
    cockpit_block_samples (COCKPIT_SAMPLES (self));
  if (self->samplers & NETWORK_SAMPLER)
    cockpit_network_samples (COCKPIT_SAMPLES (self));
  if (self->samplers & MOUNT_SAMPLER)
    cockpit_mount_samples (COCKPIT_SAMPLES (self));
  if (self->samplers & CGROUP_SAMPLER)
    cockpit_cgroup_samples (COCKPIT_SAMPLES (self));
  if (self->samplers & DISK_SAMPLER)
    cockpit_disk_samples (COCKPIT_SAMPLES (self));

  /* Check for disappeared instances
   */
  for (int i = 0; i < self->n_metrics; i++)
    {
      MetricInfo *info = &self->metrics[i];
      if (info->desc->instanced)
        if (g_hash_table_foreach_remove (info->instances, instance_unseen, NULL) > 0)
          self->need_meta = TRUE;
    }

  /* Send a meta message if necessary.  This will also allocate a new
     buffer and setup the instance indices.
   */
  if (self->need_meta)
    {
      send_meta (self);
      self->need_meta = FALSE;
    }

  /* Ship them out
   */
  double **buffer = cockpit_metrics_get_data_buffer (COCKPIT_METRICS (self));
  for (int i = 0; i < self->n_metrics; i++)
    {
      MetricInfo *info = &self->metrics[i];
      if (info->desc->instanced)
        {
          GHashTableIter iter;
          gpointer key, value;

          g_hash_table_iter_init (&iter, info->instances);
          while (g_hash_table_iter_next (&iter, &key, &value))
            {
              InstanceInfo *inst = value;
              buffer[i][inst->index] = inst->value;
            }
        }
      else
        buffer[i][0] = info->value;
    }

  cockpit_metrics_send_data (COCKPIT_METRICS (self), now);
  cockpit_metrics_flush_data (COCKPIT_METRICS (self));
}
Пример #6
0
static bool send_proxyrequest(connection_t *c) {
	switch(proxytype) {
		case PROXY_HTTP: {
			char *host;
			char *port;

			sockaddr2str(&c->address, &host, &port);
			send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
			free(host);
			free(port);
			return true;
		}
		case PROXY_SOCKS4: {
			if(c->address.sa.sa_family != AF_INET) {
				logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
				return false;
			}
			char s4req[9 + (proxyuser ? strlen(proxyuser) : 0)];
			s4req[0] = 4;
			s4req[1] = 1;
			memcpy(s4req + 2, &c->address.in.sin_port, 2);
			memcpy(s4req + 4, &c->address.in.sin_addr, 4);
			if(proxyuser)
				memcpy(s4req + 8, proxyuser, strlen(proxyuser));
			s4req[sizeof s4req - 1] = 0;
			c->tcplen = 8;
			return send_meta(c, s4req, sizeof s4req);
		}
		case PROXY_SOCKS5: {
			int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
			c->tcplen = 2;
			if(proxypass)
				len += 3 + strlen(proxyuser) + strlen(proxypass);
			char s5req[len];
			int i = 0;
			s5req[i++] = 5;
			s5req[i++] = 1;
			if(proxypass) {
				s5req[i++] = 2;
				s5req[i++] = 1;
				s5req[i++] = strlen(proxyuser);
				memcpy(s5req + i, proxyuser, strlen(proxyuser));
				i += strlen(proxyuser);
				s5req[i++] = strlen(proxypass);
				memcpy(s5req + i, proxypass, strlen(proxypass));
				i += strlen(proxypass);
				c->tcplen += 2;
			} else {
				s5req[i++] = 0;
			}
			s5req[i++] = 5;
			s5req[i++] = 1;
			s5req[i++] = 0;
			if(c->address.sa.sa_family == AF_INET) {
				s5req[i++] = 1;
				memcpy(s5req + i, &c->address.in.sin_addr, 4);
				i += 4;
				memcpy(s5req + i, &c->address.in.sin_port, 2);
				i += 2;
				c->tcplen += 10;
			} else if(c->address.sa.sa_family == AF_INET6) {
				s5req[i++] = 3;
				memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
				i += 16;
				memcpy(s5req + i, &c->address.in6.sin6_port, 2);
				i += 2;
				c->tcplen += 22;
			} else {
				logger(DEBUG_ALWAYS, LOG_ERR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
				return false;
			}
			if(i > len)
				abort();
			return send_meta(c, s5req, sizeof s5req);
		}
		case PROXY_SOCKS4A:
			logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
			return false;
		case PROXY_EXEC:
			return true;
		default:
			logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
			return false;
	}
}
Пример #7
0
bool send_proxyrequest(connection_t *c) {
	switch(proxytype) {
	case PROXY_SOCKS4:
		if(c->address.sa.sa_family != AF_INET) {
			logger(LOG_ERR, "Can only connect to numeric IPv4 addresses through a SOCKS 4 proxy!");
			return false;
		}
	case PROXY_SOCKS4A: {
		if(c->address.sa.sa_family != AF_INET && c->address.sa.sa_family != AF_UNKNOWN) {
			logger(LOG_ERR, "Can only connect to IPv4 addresses or hostnames through a SOCKS 4a proxy!");
			return false;
		}
		int len = 9;
		if(proxyuser)
			len += strlen(proxyuser);
		if(c->address.sa.sa_family == AF_UNKNOWN)
			len += 1 + strlen(c->address.unknown.address);
		char s4req[len];
		s4req[0] = 4;
		s4req[1] = 1;
		if(c->address.sa.sa_family == AF_INET) {
			memcpy(s4req + 2, &c->address.in.sin_port, 2);
			memcpy(s4req + 4, &c->address.in.sin_addr, 4);
		} else {
			uint16_t port = htons(atoi(c->address.unknown.port));
			memcpy(s4req + 2, &port, 2);
			memcpy(s4req + 4, "\0\0\0\1", 4);
			strcpy(s4req + (9 + (proxyuser ? strlen(proxyuser) : 0)), c->address.unknown.address);
		}
		if(proxyuser)
			strcpy(s4req + 8, proxyuser);
		else
			s4req[8] = 0;
		s4req[sizeof s4req - 1] = 0;
		c->allow_request = PROXY;
		return send_meta(c, s4req, sizeof s4req);
	}

	case PROXY_SOCKS5: {
		int len = 3 + 6;
		if(c->address.sa.sa_family == AF_INET) {
			len += 4;
		} else if(c->address.sa.sa_family == AF_INET6) {
			len += 16;
		} else if(c->address.sa.sa_family == AF_UNKNOWN) {
			len += 1 + strlen(c->address.unknown.address);
		} else {
			logger(LOG_ERR, "Address family %x not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
			return false;
		}
		if(proxypass)
			len += 3 + strlen(proxyuser) + strlen(proxypass);
		char s5req[len];
		int i = 0;
		s5req[i++] = 5;
		s5req[i++] = 1;
		if(proxypass) {
			s5req[i++] = 2;
			s5req[i++] = 1;
			s5req[i++] = strlen(proxyuser);
			strcpy(s5req + i, proxyuser);
			i += strlen(proxyuser);
			s5req[i++] = strlen(proxypass);
			strcpy(s5req + i, proxypass);
			i += strlen(proxypass);
		} else {
			s5req[i++] = 0;
		}
		s5req[i++] = 5;
		s5req[i++] = 1;
		s5req[i++] = 0;
		if(c->address.sa.sa_family == AF_INET) {
			s5req[i++] = 1;
			memcpy(s5req + i, &c->address.in.sin_addr, 4);
			i += 4;
			memcpy(s5req + i, &c->address.in.sin_port, 2);
			i += 2;
		} else if(c->address.sa.sa_family == AF_INET6) {
			s5req[i++] = 4;
			memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
			i += 16;
			memcpy(s5req + i, &c->address.in6.sin6_port, 2);
			i += 2;
		} else if(c->address.sa.sa_family == AF_UNKNOWN) {
			s5req[i++] = 3;
			int len = strlen(c->address.unknown.address);
			s5req[i++] = len;
			memcpy(s5req + i, c->address.unknown.address, len);
			i += len;
			uint16_t port = htons(atoi(c->address.unknown.port));
			memcpy(s5req + i, &port, 2);
			i += 2;
		} else {
			logger(LOG_ERR, "Unknown address family while trying to connect to SOCKS5 proxy");
			return false;
		}
		if(i > len)
			abort();
		c->allow_request = PROXY;
		return send_meta(c, s5req, sizeof s5req);
	}

	case PROXY_HTTP: {
		char *host;
		char *port;

		sockaddr2str(&c->address, &host, &port);
		send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
		free(host);
		free(port);
		c->allow_request = PROXY;
		return true;
	}

	case PROXY_EXEC:
		return true;

	default:
		logger(LOG_ERR, "Unknown proxy type");
		return false;
	}
}