/*
 *               Copyright (C) 2007, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */


#ifndef __SOC_H__
#define __SOC_H__


#ifdef __cplusplus
extern "C" {
#endif


#include <types.h>


/* TODO: probably better to move BOOL/TRUE/FALSE to types.h */
#define BOOL                                int
#define TRUE                                1
#define FALSE                               0

#define OS_LOW_ENTROPY_DATA_SIZE            32

#define CPS_MAX_DN_LEN                      255
#define CPS_MAX_CN_LEN                      79

#define SOC_PF_INET                         2

#define SOC_SOCK_STREAM                     1
#define SOC_SOCK_DGRAM                      2

#define SOC_SHUT_RD                         0
#define SOC_SHUT_WR                         1
#define SOC_SHUT_RDWR                       2

#define SOC_CERT_OUTOFDATE                  0x8000
#define SOC_CERT_BADSERVER                  0x4000

#define SOC_CERT_ERRMASK                    0x00ff

enum {
    SOC_CERT_NOERROR = 0,
    SOC_CERT_NOROOTCA,
    SOC_CERT_BADSIGNATURE,
    SOC_CERT_UNKNOWN_SIGALGORITHM,
    SOC_CERT_UNKNOWN_PUBKEYALGORITHM
};

#define SOC_EINPROGRESS                     -26
#define SOC_EINVAL                          -28
#define SOC_ENOTCONN                        -56


typedef u32 SOCLInAddr;

typedef struct _SOCInterface {
    u32 _dummy_;
} SOCInterface;

typedef struct _SOCInAddr {
    u32 addr;
} SOCInAddr;

typedef struct _SOCSockAddr {
    u8 len;
    u8 family;
    u8 data[6];
} SOCSockAddr;

typedef struct _SOCSockAddrIn {
    u8 len;
    u8 family;
    u16 port;
    SOCInAddr addr;
} SOCSockAddrIn;

typedef struct _SOCCaInfo {
    char *dn;           /* distinguished name */
    int modulus_len;    /* lendth of modulus */
    u8 *modulus;        /* modulus */
    int exponent_len;   /* length of public exponent */
    u8 *exponent;       /* public exponent */
} SOCCaInfo, CPSCaInfo;

typedef struct _CPSSslConnection {
    char issuer[CPS_MAX_DN_LEN + 1];
    char subject[CPS_MAX_DN_LEN + 1];
    char cn[CPS_MAX_CN_LEN + 1];
    char *server_name;
    CPSCaInfo **ca_info;
    int ca_builtins;
    int (*auth_callback) (int, struct _CPSSslConnection *, int);
} SOCSslConnection, CPSSslConnection;


int SOC_OpenLib();
int SOC_CloseLib();

void OS_GetLowEntropyData(u32 buffer[OS_LOW_ENTROPY_DATA_SIZE / sizeof(u32)]);

SOCLInAddr SOCL_Resolve(const char* hostname);

void SOC_GetMacAddr(SOCInterface *interface, u8 *addr);

u16 SOC_HtoNs(u16 hostshort);
u32 SOC_HtoNl(u32 hostlong);
int SOC_InetAtoN(const char *cp, SOCInAddr *inp);

int SOC_Socket(int pf, int type, int protocol);
int SOC_Close(int s);
int SOC_Shutdown(int s, int how);
int SOC_Bind(int s, const void *sockAddr);
int SOC_Connect(int s, const void *sockAddr);
int SOC_Accept(int s, void *sockAddr);
int SOC_Recv(int s, void *buf, int len, int flags);
int SOC_RecvFrom(int s, void *buf, int len, int flags, void *sockFrom);
int SOC_Send(int s, const void *buf, int len, int flags);
int SOC_SendTo(int s, const void *buf, int len, int flags, const void *sockTo);
int SOC_GetSockName(int s, void *sockAddr);

void SOC_AddRandomSeed(void *seed, u32 len);
int SOC_EnableSsl(int s, SOCSslConnection *con);


#ifdef __cplusplus
}
#endif


#endif /* __SOC_H__ */
