/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2011 Stephane Bisaro, aka Iri <iri@irizone.net>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/





#ifndef __MAIN_NET_H__
#define __MAIN_NET_H__


#if defined _WIN32 || defined __WIN32__
#define WINVER 0x0501   /* XP */
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if defined VERSION_WIN || defined __WIN32__ || defined WIN32
#include <windows.h>
#include "scol_plugin_win.h"
#else
#include "scol_plugin_lin.h"
#endif
#include "macros.h"


#if defined _WIN32 || defined __WIN32__
#include <winsock2.h>
#include <ws2tcpip.h>
#include <unistd.h>
#elif ((defined linux) || (defined __linux))
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
/* TODO */
#endif


/* $Iri : New TYPVAR for API definitions */
#define SCTYPVAR(x) (int (__cdecl *)(struct Mmachine *))ITOM(x)



/* objects */
int ObjFtp;



#define NETFTP_MAXBUFFER        512
#define NETFTP_RESPONSE_CMD     0
#define NETFTP_RESPONSE_DATAS   1

#define NETFTP_RESPONSEOK       0
#define NETFTP_FAILSEND         1
#define NETFTP_FAILCONNEXION    2
#define NETFTP_FAILUSER         3
#define NETFTP_FAILPASSWORD     4

#define NETFTP_PASSIVEMODE      0
#define NETFTP_ACTIVEMODE       1


#define NET_AF_INET             AF_INET
#define NET_AF_INET6            AF_INET6
#define NET_AF_UNSPEC           AF_UNSPEC



/* Machine constantes */
/* FTP part */
#define NET_FTP_CB              1
#define NET_FTP_CB_RECEIVE      0

#define NET_FTP_PKG_NB          19

#define NET_FTPR_SIZE           3
#define NET_FTP_HANDLE          0
#define NET_FTP_NAME            1
#define NET_FTP_KILLED          2


/* Networks part */
#define NET_NET_PKG_NB          5



/* Structures */
/* FTP part */
typedef struct NetFtpObject *NetFtpObject;
struct NetFtpObject
{
    int port;
    int portdatas;
    int code;   /* response */
    int flag;   /* passive (0) or active (1) mode */
    int active;
    int sizereply;
    SOCKET sockCmd;
    SOCKET sockDatas;
    char *domain;
    char *domaindatas;
    char *lastCmd;
    char *reply[512];   /* last responses */
    char *buffer;       /* returned datas */
    char *username;
    char *password;
    #if defined _WIN32 || defined __WIN32__
    char *ipsrv;
    char *ipsrvdatas;
    #else
    IN_ADDR ipsrv;
    IN_ADDR ipsrvdatas;
    #endif
};

int nbSocks;





int SCOLftpLoadAPI (mmachine m);    /* load ftp api */
int SCOLnetworksLoadAPI (mmachine m);/* load networks api */


int net_connection_init (void);
int net_connection_end (SOCKET sock);
int net_connection_socket (int port, char *iphost);
#if defined _WIN32 || defined __WIN32__
void net_get_host (char *domain, char **ip);
#else
void net_get_host (char *domain, IN_ADDR *ip);
#endif
int net_get_XYZ (SOCKET sock, NetFtpObject o, char **reply);
int net_connection_write (SOCKET sock, char *cmd);


/* misc. */
int searchCharMem (const char * word, char c);
void replaceChar (char *s, char cin, char cout);
int searchChar (char * word, char c);


#endif  /* __MAIN_NET_H__ */

