示例#1
0
/**
 * [Server only]
 * Handle a new connection from a client. Set up connection details and
 * initialize sequence numbers.
 *
 * pkt: The SYN segment from the client.
 * returns: The conn_t associated with the new connection.
 */
conn_t *tcp_new_connection(char *pkt) { ASSERT_SERVER_ONLY;
  /* Ignore if too many clients are connected. */
  if (num_connected >= MAX_NUM_CLIENTS) {
    fprintf(stderr, "[ERROR] Maximum number of clients (%d) reached\n",
            MAX_NUM_CLIENTS);
    return NULL;
  }
  num_connected++;

  iphdr_t *ip_hdr = (iphdr_t *) pkt;
  tcphdr_t *syn = (tcphdr_t *) (pkt + IP_HDR_SIZE);

  /* Set up connection details and add to list of connections. */
  conn_t *conn = calloc(sizeof(conn_t), 1);
  conn_setup(conn, ntohl(ip_hdr->saddr), ntohs(syn->th_sport), unix_socket);
  conn->their_init_seqno = ntohl(syn->th_seq);
  conn->ackno = conn->their_init_seqno + 1;
  conn_add(conn);

  /* Send a SYN-ACK to the client. */
  send_synack(conn);

  /* Get window size of the client. */
  ctcp_cfg->send_window = ntohs(syn->window);
  ctcp_config_t *config_copy = calloc(sizeof(ctcp_config_t), 1);
  memcpy(config_copy, ctcp_cfg, sizeof(ctcp_config_t));

  /* Student code. */
  ctcp_state_t *state = ctcp_init(conn, config_copy);
  conn->state = state;

  fprintf(stderr, "[INFO] Client connected\n");
  return conn;
}
示例#2
0
文件: irc-core.c 项目: Adam-/irssi
void irc_core_init(void)
{
	CHAT_PROTOCOL_REC *rec;

	rec = g_new0(CHAT_PROTOCOL_REC, 1);
	rec->name = "IRC";
	rec->fullname = "Internet Relay Chat";
	rec->chatnet = "ircnet";

        rec->case_insensitive = TRUE;

	rec->create_chatnet = create_chatnet;
        rec->create_server_setup = create_server_setup;
        rec->create_channel_setup = create_channel_setup;
	rec->create_server_connect = create_server_connect;
	rec->destroy_server_connect = destroy_server_connect;

	rec->server_init_connect = irc_server_init_connect;
	rec->server_connect = irc_server_connect;
	rec->channel_create =
		(CHANNEL_REC *(*) (SERVER_REC *, const char *,
				   const char *, int))
                irc_channel_create;
	rec->query_create =
		(QUERY_REC *(*) (const char *, const char *, int))
                irc_query_create;

	chat_protocol_register(rec);
        g_free(rec);

        irc_session_init();
	irc_chatnets_init();
	irc_servers_init();
	irc_channels_init();
	irc_queries_init();

	ctcp_init();
	irc_commands_init();
	irc_irc_init();
	lag_init();
	netsplit_init();
	irc_expandos_init();

	settings_check();
	module_register("core", "irc");
}
示例#3
0
/**
 * Start a client.
 *
 * server: String containing the server to connect to.
 * port: The port the client will run on.
 */
int start_client(char *server, char *port) {
  if (do_config_server(server) < 0 || do_config(port) < 0)
    return -1;

  /* Initialize connection with server. Go to student code. */
  conn_t *conn = tcp_handshake();
  ctcp_config_t *config_copy = calloc(sizeof(ctcp_config_t), 1);
  memcpy(config_copy, ctcp_cfg, sizeof(ctcp_config_t));
  ctcp_state_t *state = ctcp_init(conn, config_copy);
  if (state == NULL) {
    fprintf(stderr, "[ERROR] Could not connect to server!\n");
    return -1;
  }
  fprintf(stderr, "[INFO] Connected to server\n");
  config->sconn->state = state;

  setup_poll();
  do_loop();
  return 0;
}