Beispiel #1
0
void finish_connecting(connection_t *c) {
	ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);

	c->last_ping_time = now;

	send_id(c);
}
Beispiel #2
0
/*
  accept a new tcp connect and create a
  new connection
*/
bool handle_new_meta_connection(int sock) {
	connection_t *c;
	sockaddr_t sa;
	int fd;
	socklen_t len = sizeof(sa);

	fd = accept(sock, &sa.sa, &len);

	if(fd < 0) {
		logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
		return false;
	}

	sockaddrunmap(&sa);

	c = new_connection();
	c->name = xstrdup("<unknown>");
	c->outcipher = myself->connection->outcipher;
	c->outdigest = myself->connection->outdigest;
	c->outmaclength = myself->connection->outmaclength;
	c->outcompression = myself->connection->outcompression;

	c->address = sa;
	c->hostname = sockaddr2hostname(&sa);
	c->socket = fd;
	c->last_ping_time = now;

	ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);

	configure_tcp(c);

	connection_add(c);

	c->allow_request = ID;
	send_id(c);

	return true;
}
Beispiel #3
0
void setup()
{
	serial_buffer_head = serial_buffer;
	serial_buffer_tail = serial_buffer;
	serial_overflow = false;
	debug_value = 0x1337;
	arch_setup_start();
	enabled_pins = NUM_DIGITAL_PINS;
	for (uint8_t p = 0; p < NUM_DIGITAL_PINS; ++p) {
		pin[p].duty = 255;
		// Reset state is unset, then unset the pin.
		pin[p].state = CTRL_UNSET << 2 | CTRL_RESET;
		UNSET(p);
	}
	pin_events = 0;
	notified_current_fragment = 0;
	current_fragment = notified_current_fragment;
	last_fragment = current_fragment;
	filling = 0;
	// Disable all adcs.
	for (uint8_t a = 0; a < NUM_ANALOG_INPUTS; ++a) {
		for (uint8_t i = 0; i < 2; ++i) {
			adc[a].linked[i] = ~0;
			adc[a].value[i] = 1 << 15;
			adc[a].is_on = false;
		}
	}
	adc_phase = INACTIVE;
	adc_current = ~0;
	adc_next = ~0;
	// Set up communication state.
	command_end = 0;
	had_data = false;
	ping = 0;
	out_busy = 0;
	ff_in = 0;
	ff_out = 0;
	reply_ready = 0;
	adcreply_ready = 0;
	timeout = false;
	timeout_time = 0;
	// Set up homing state.
	homers = 0;
	home_step_time = 0;
	// Set up movement state.
	last_len = 0;
	stopping = -1;
	arch_set_speed(0);
	current_len = 0;
	active_motors = 0;
	move_phase = 0;
	full_phase = 1;
	// Set up led state.
	led_fast = 0;
	led_last = millis();
	led_phase = 0;
	led_pin = ~0;
	stop_pin = ~0;
	probe_pin = ~0;
	spiss_pin = ~0;
	audio = 0;
	audio_motor = 0;
	// Do arch-specific things.  This fills printerid and uuid.
	arch_setup_end();
	// Inform host of reset.
	send_id(CMD_STARTUP);
}
Beispiel #4
0
int receive_proxy_meta(connection_t *c, int start, int lenin) {
	switch(proxytype) {
	case PROXY_SOCKS4:
	case PROXY_SOCKS4A:
		if(c->buflen < 8)
			return 0;
		if(c->buffer[0] == 0 && c->buffer[1] == 0x5a) {
			if(c->address.sa.sa_family == AF_UNKNOWN)
				update_address_ipv4(c, c->buffer + 4, c->buffer + 2);

			ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted");
			c->allow_request = ID;
			c->status.proxy_passed = true;
			send_id(c);
			return 8;
		} else {
			logger(LOG_ERR, "Proxy request rejected");
			return -1;
		}

	case PROXY_SOCKS5:
		if(c->buflen < 2)
			return 0;
		if(c->buffer[0] != 0x05 || c->buffer[1] == (char)0xff) {
			logger(LOG_ERR, "Proxy authentication method rejected");
			return -1;
		}
		int offset = 2;
		if(c->buffer[1] == 0x02) {
			if(c->buflen < 4)
				return 0;
			if(c->buffer[2] != 0x05 || c->buffer[3] != 0x00) {
				logger(LOG_ERR, "Proxy username/password rejected");
				return -1;
			}
			offset += 2;
		}
		if(c->buflen - offset < 7)
			return 0;
		if(c->buffer[offset] != 0x05  || c->buffer[offset + 1] != 0x00) {
			logger(LOG_ERR, "Proxy request rejected");
			return -1;
		}
		int replen = offset + 6;
		switch(c->buffer[offset + 3]) {
			case 0x01: // IPv4
				if(c->address.sa.sa_family == AF_UNKNOWN)
					update_address_ipv4(c, c->buffer + offset + 4, c->buffer + offset + 8);
				replen += 4;
				break;
			case 0x03: // Hostname
				if(c->address.sa.sa_family == AF_UNKNOWN)
					update_address_ipv4(c, "\0\0\0\1", "\0\0");
				replen += ((uint8_t *)c->buffer)[offset + 4];
				break;
			case 0x04: // IPv6
				if(c->address.sa.sa_family == AF_UNKNOWN)
					update_address_ipv6(c, c->buffer + offset + 4, c->buffer + offset + 20);
				replen += 16;
				break;
			default:
				logger(LOG_ERR, "Proxy reply malformed");
				return -1;
		}
		if(c->buflen < replen) {
			return 0;
		} else {
			ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted");
			c->allow_request = ID;
			c->status.proxy_passed = true;
			send_id(c);
			return replen;
		}

	case PROXY_HTTP: {
		char *p = memchr(c->buffer, '\n', c->buflen);
		if(!p || p - c->buffer >= c->buflen)
			return 0;

		while((p = memchr(p + 1, '\n', c->buflen - (p + 1 - c->buffer)))) {
			if(p > c->buffer + 3 && !memcmp(p - 3, "\r\n\r\n", 4))
				break;
		}

		if(!p)
			return 0;

		if(c->buflen < 9)
			return 0;

		if(!strncasecmp(c->buffer, "HTTP/1.1 ", 9)) {
			if(!strncmp(c->buffer + 9, "200", 3)) {
				if(c->address.sa.sa_family == AF_UNKNOWN)
					update_address_ipv4(c, "\0\0\0\1", "\0\0");
				logger(LOG_DEBUG, "Proxy request granted");
				replen = p  + 1 - c->buffer;
				c->allow_request = ID;
				c->status.proxy_passed = true;
				send_id(c);
				return replen;
			} else {
				p = memchr(c->buffer, '\n', c->buflen);
				p[-1] = 0;
				logger(LOG_ERR, "Proxy request rejected: %s", c->buffer + 9);
				return false;
			}
		} else {
			logger(LOG_ERR, "Proxy reply malformed");
			return -1;
		}
	}

	default:
		abort();
	}
}
void ClientHandler::client_handler_thread()
{
  int num;
  network_message message;
  int pointeroffset = 0;
  char network_str[100];

  inet_ntop(AF_INET, &client_addr.sin_addr, network_str, sizeof(network_str));
  std::cout  << " connection from " << network_str
    << ":" << client_addr.sin_port << std::endl;
  while(!stop)
  {
    //get message type
    if ((num = read(client_socket, ((char *)&message)+pointeroffset, sizeof(network_message)-pointeroffset)) == -1)
    {
      std::cout <<"recv error" << std::endl;
      disconnected();
    }
    else if(num == 0)
    { //disconnected
      disconnected();
      std::cout << "disconnected" << std::endl;
      break;
    }
    pointeroffset = (pointeroffset + num) % sizeof(message);
    if (pointeroffset == 0)
    {
      switch(message.type)
      {
        case message_type::hello:
          if(!handle_hello(message) || !send_id())
          {
            disconnected();
          }
        break;
        case message_type::new_card:

        break;
        case message_type::accepted:

        break;
        case message_type::denied:

        break;
        case message_type::i_have_card:

        break;
        case message_type::give_id:

        break;
        case message_type::client_list:

        break;
        case message_type::set_ready:
          handle_ready_set(message.value);
        break;
        case message_type::state_update:

        break;
      }
    }
  }
  close(client_socket);
}
Beispiel #6
0
bool id_h(connection_t *c) {
	char name[MAX_STRING_SIZE];

	if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
		logger(LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
		       c->hostname);
		return false;
	}

	/* Check if identity is a valid name */

	if(!check_id(name) || !strcmp(name, myself->name)) {
		logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
		       c->hostname, "invalid name");
		return false;
	}

	/* If this is an outgoing connection, make sure we are connected to the right host */

	if(c->outgoing) {
		if(strcmp(c->name, name)) {
			logger(LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
			       c->name);
			return false;
		}
	} else {
		if(c->name) {
			free(c->name);
		}

		c->name = xstrdup(name);
	}

	/* Check if version matches */

	if(c->protocol_version != myself->connection->protocol_version) {
		logger(LOG_ERR, "Peer %s (%s) uses incompatible version %d",
		       c->name, c->hostname, c->protocol_version);
		return false;
	}

	if(bypass_security) {
		if(!c->config_tree) {
			init_configuration(&c->config_tree);
		}

		c->allow_request = ACK;

		if(!c->outgoing) {
			send_id(c);
		}

		return send_ack(c);
	}

	if(!c->config_tree) {
		init_configuration(&c->config_tree);

		if(!read_connection_config(c)) {
			logger(LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname,
			       c->name);
			return false;
		}
	}

	if(!read_rsa_public_key(c)) {
		return false;
	}

	c->allow_request = METAKEY;

	if(!c->outgoing) {
		send_id(c);
	}

	return send_metakey(c);
}