
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"
#include<file_comp/file_comp.h>

extern global_data_t gd;

extern pthread_mutex_t fd_mutex;



// zitherとの最初のネゴシエーションをする関数
int start_neg(void)
{
int stat, interval;

stat = 0;

char * recdata = (char *)calloc(IP_LEN, sizeof(char));
char * intr = (char *)calloc(IP_LEN, sizeof(char));

interval = (gd.bf.life_int + gd.bf.life_los + INT_PLUS);

intr = safe_sprintf(intr, BUF_MAX, "%s :: COM: NEG DATA: %s,%s\n", gd.bf.c_name, CL_VAR, gd.bf.c_info);

pthread_mutex_lock(&fd_mutex);
if((stat = socket_send(gd.ip.sk, intr, interval)) > 0){
   recdata = select_fdcat(gd.ip.sk, recdata, &stat, BUF_MAX, interval);
   }
if(stat < 0){
   safe_exit("start_neg(1): socket err exit \n");
   }
pthread_mutex_unlock(&fd_mutex);

if(0 == memcmp(recdata, "500 NG", 6)){
   fprintf(stderr,"start_neg(): youer name is %s not login, check neg.conf\n", gd.bf.c_name);
   safe_exit("2 start_neg() exit \n"); /* It ends. */
   }
else{
   fprintf(stderr,"start_neg(): youer name is %s login OK!\n", gd.bf.c_name);
   }

#ifdef FREE_BUG
printf("FREE: single stat_neg(): gd.bf.recdata %s\n", recdata);
printf("FREE: single stat_neg(): gd.bf.intr %s\n", intr);
#endif
free(recdata);
free(intr);

return 0;
}





