#include <miknet/miknet.h>

int mikpeer (miknode_t *n)
{
	int sock, i, pos = 0;
	struct sockaddr_storage addr;
	socklen_t addrlen;

	sock = accept(n->tcp, (struct sockaddr *)&addr, &addrlen);
	if (sock < 0)
		return mik_debug(ERR_SOCKET);

	if (n->peerc >= n->peermax) {
		close(sock);
		return ERR_PEER_MAX;
	}

	n->peerc++;

	for (i = 0; i < n->peermax; ++i) {
		if (n->peers[i].state == MIK_DISC) {
			pos = i;
			break;
		}
	}

	n->peers[pos].state = MIK_CONN;
	n->peers[pos].tcp = sock;
	n->peers[pos].addr = addr;
	n->peers[pos].addrlen = addrlen;
	n->peers[pos].sent = 0;
	n->peers[pos].recvd = 0;

	return 0;
}

int mikpeer_close (mikpeer_t *p)
{
	close(p->tcp);
	memset(&p->addr, 0, sizeof(struct sockaddr_storage));

	p->state = MIK_DISC;
	p->tcp = 0;
	p->addrlen = 0;
	p->sent = 0;
	p->recvd = 0;

	return 0;
}
Create a constructor and deconstructor for the peer structure.
Create a constructor and deconstructor for the peer structure.

Peers are created over the SOCK_STREAM socket, and so that is preferred.
The array which holds all the peers of a node, node.peers, determines whether
a slot is empty or occupied by the state of the peer. MIK_CONN is active, and
MIK_DISC is an empty slot.
