/*  libfreenet
 *  Copyright 2001 Steven Hazel <sah@thalassocracy.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifndef __FN_PROTOCOL_H__
#define __FN_PROTOCOL_H__

#include <sys/types.h>
#include <arpa/inet.h>

#include <rijndael-api-fst.h>
#include <config.h>

#include "status.h"
#include "message.h"

#define FN_TRUE (1 == 1)
#define FN_FALSE (!FN_TRUE)

/* encryption key length */
#define FN_KEY_BITS 128
#define FN_KEY_BYTES (FN_KEY_BITS/8)

/* diffie-hellman key length */
#define FN_DHK_LEN 128
#define FN_DHK_MPI_LEN (FN_DHK_LEN+2)

#define FN_HASH_BYTES 20

#define FN_VERSION  ((float)1.299)
#define FN_REVISION ((float)1.65)
#define FN_BUILD    1

#define FN_DEFAULT_PORT 19114

#define FN_DH_N "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF"

#define FN_DH_G "2"

#define FN_DH 30
#define FN_DL 4

#define FN_STOREDATA_MSG         1
#define FN_HANDSHAKEREQUEST_MSG  2
#define FN_HANDSHAKEREPLY_MSG    3
#define FN_QUERYRESTARTED_MSG    4
#define FN_DATAREQUEST_MSG       5
#define FN_DATAREPLY_MSG         6
#define FN_TIMEDOUT_MSG          7
#define FN_REQUESTFAILED_MSG     8
#define FN_INSERTREQUEST_MSG     9
#define FN_INSERTREPLY_MSG      10
#define FN_DATAINSERT_MSG       11
#define FN_UNKNOWN_MSG          12


typedef struct {
  int fbpos;
  unsigned char fb[FN_KEY_BYTES];
  rijndael_keyInstance key;
  rijndael_cipherInstance cipher;
} freenet_rijndael_system;

typedef struct {
  int socket;
  freenet_rijndael_system sendsys;
  freenet_rijndael_system recvsys;
  int expect_callback;
  time_t timeout;
} freenet_connection;


int freenet_senddata(freenet_connection *connection, const char *buffer,
                     int len);

int freenet_sendmsg (freenet_connection *connection, freenet_message *message);

int freenet_readdata(freenet_connection *connection, char *buffer, int len);

int freenet_recvmsg (freenet_connection *connection, freenet_message *message);

int freenet_connect (freenet_connection *connection, char *hostaddress,
                     int port);

int freenet_get_connection (freenet_connection *connection, u_short port);

/* initialize the connection struct and setup the ciphers */
int freenet_auth_connection (freenet_connection *connection);

int freenet_close_connection (freenet_connection *connection);

const char *freenet_strerror (int status);

#endif /* __FN_PROTOCOL_H__ */
