/* this is the goo header file 
   written by l0om
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

/*****************
 CONSTANTS
 *****************/

#define		FAILD		0
#define		SUCCESS		1
#define 	ALLOC_ERR	2
#define		MQ_ERR		3
#define 	FORK_ERR	4
#define		NO_SUCH_CLI	5
#define		UNKNOWN		6
#define		ILLGL_PXY	7
#define		CONNFAILD	8
#define		SOCKETERR	9
#define		DNSERROR	10
#define		SENDERR		11
#define		READERR		12
#define		TOO_LARGE	13
#define		NOANSWER	14

#define 	MAXREQ		150
#define		MAXPRXY		50
#define		HTTPREQ		255
#define		BUFFLEN		500
#define		URLLEN		120

#define		CMD		100
#define		CLIDENT		101
#define		CLIANW		102

#define		GETRESLT	200
#define		GETURLS		201
#define		QUIT		202
#define		SETPRXY		203
#define		NOPRXY		204

#define		RES_PATTN	"of about <b>"
#define		URL_PATTN	"<h2 class=r><a href="
#define		DEF_GOOGLE	"www.google.com"	
						// dont change DEF_GOOGLE
						// without changing RES_PATTN
						// and URL_PATTN

/******************
 STRUCTS
 ******************/

typedef struct _goo_client {
	u_char id;		//intern id of goo child
	char reqtyp;		//type of request
	int mq_id;		//the special message queue
	pid_t pid;		//the childs pid
	char proxy[MAXPRXY];		//enter proxy to request here (name:port)
	struct _goo_client *next;	//pointer on next child 
} goo_client;

typedef struct _goo_t {
	u_char resultc;		//last received result code
	int goo_results;
	struct _goo_client *cli;	//the client list
} goo_t;

typedef struct  {
	long mtyp;
	u_char id;
	pid_t pid;
	int cli_mq;
} cli_ident;
	
typedef struct {
	long mtyp;
	u_char cmdtyp;
	goo_client c;
	u_int urls;
	char req[MAXREQ];
} srv_cmd;

typedef struct {
	long mtyp;
	u_char resultc;			//result code
	char data[10][URLLEN];		//the received urls
	char urls;			//have read this much results
	u_long	goo_res;		//results 
	char req[MAXREQ];
} cli_ans;

/*****************
 PROTOTYPES
 ****************/

goo_t		init_goo();
int		goo_cli(int ncli, goo_t *ginf);
int		delete_goo(goo_t *ginf);
void		goo_cli_set(int cli, u_char opt, void *data, goo_t *ginf);
int		goo_cli_req(int cli, char *req, goo_t *ginf, u_int urls);
int		goo_cli_cnt(goo_t *ginf);
cli_ans		goo_results(goo_t *ginf);
void		goo_prnt_er(goo_t *ginf);

//void	usleep(long usec);
char		*http_response(int sockfd, int *nbytes, goo_t *ginf);
void		free_clis(goo_t *ginf);
goo_client 	*ret_cli_inf(u_char id, goo_t *ginf);
u_long		get_result(char *req, cli_ans *ans, goo_client me, goo_t *ginf);
int     	get_urls(char *req, cli_ans *ans, goo_client me, goo_t *ginf, int start);
void		append(goo_t *ginf, goo_client *new);
char		*encode(char *str);
int		connect_me(char *dest, int port, int *sock, goo_t *ginf);


