/* $Id: pjnath_server.c,v 1.11 2008/08/11 07:10:14 gg2265 Exp $ */
/* 
 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.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 
 */
#include "server.h"
#include<fcntl.h>
#include<signal.h>

#define THIS_FILE	"pjnath_server.c"

//Globals
struct server_options gServerOptions;
pj_caching_pool gCp;
pj_stun_server *gpSrv;
pj_stun_usage *gpTurn;

void shutdown_server(int a){
  
  pj_thread_desc handler_desc;
  pj_thread_t *server_thread;
  
  //register thread with pjlib
  pj_thread_register("srvr", handler_desc, &server_thread);
  pj_stun_server_destroy(gpSrv);
  pj_pool_factory_dump(&gCp.factory, PJ_TRUE);
  pj_shutdown();

  //Bug fixed
  //churn does not work in first time.
  exit(0);
  
}

int get_stun_srv_port() {
  return gServerOptions.stun_srv_port;
}


int init_server(struct server_options *options){
  pj_status_t status;
  struct pj_sockaddr_in sttaddr;

  signal(SIGTERM, shutdown_server);
  signal(SIGINT, shutdown_server);

  options->stun_srv_port=0;//DEFAULT_STUN_SERVER_PORT;
  memcpy(&gServerOptions, options, sizeof(struct server_options)); 
  gServerOptions.stun_srv_port=0;
/*
    pj_init();
    pjlib_util_init();
    pjnath_init();
	*/
  pj_caching_pool_init(&gCp, &pj_pool_factory_default_policy, 0);

    status = pj_stun_server_create(&gCp.factory, 1, &gpSrv);
    if (status != PJ_SUCCESS) {
	pj_stun_perror(THIS_FILE, "Unable to create server", status);
	return 1;
    }

    /*
    status = pj_stun_bind_usage_create(gpSrv, NULL, 3478, NULL);
    if (status != PJ_SUCCESS) {
	pj_stun_perror(THIS_FILE, "Unable to create bind usage", status);
	return 1;
    }
    */

    status = pj_stun_turn_usage_create(gpSrv, pj_SOCK_DGRAM(), NULL,
				       gServerOptions.stun_srv_port, gServerOptions.use_fingerprint, &gpTurn);
    if (status != PJ_SUCCESS) {
	pj_stun_perror(THIS_FILE, "Unable to create bind usage", status);
	return 1;
    }

    //find stun server port
    custom_getsockname(gpTurn, &sttaddr);
    gServerOptions.stun_srv_port = pj_ntohs(sttaddr.sin_port);
    //printf("stun server port=%d\n", gServerOptions.stun_srv_port);
      
      
    if (gServerOptions.user_name && gServerOptions.password) {
	pj_stun_auth_cred cred;
	pj_bzero(&cred, sizeof(cred));
	cred.type = PJ_STUN_AUTH_CRED_STATIC;
	cred.data.static_cred.realm = pj_str(gServerOptions.realm);
	cred.data.static_cred.username = pj_str(gServerOptions.user_name);
	cred.data.static_cred.data_type = 0;
	cred.data.static_cred.data = pj_str(gServerOptions.password);
	cred.data.static_cred.nonce = pj_str(gServerOptions.nonce);
	pj_stun_turn_usage_set_credential(gpTurn, &cred);
    }

    //server_main(gpSrv);

    //pj_stun_server_destroy(gpSrv);
    //pj_pool_factory_dump(&gCp.factory, PJ_TRUE);
    //pj_shutdown();
    return 0;
  
}

void print_server_resource_usage() {
  custom_print_resource_usage(gpTurn);
}//print_server_resource_usage

void custom_get_turn_allocations(pj_stun_usage *pTurn, pjnath_turn_allocation *arr, int *len);

void get_turn_allocations(pjnath_turn_allocation *arr, int *len) {
  custom_get_turn_allocations(gpTurn, arr, len);
}//get_turn_allocations

void getsendrecvbw(long* sbw, long* rbw) {
  //gpTurn is NULL when we run a client
  if(NULL!=gpTurn) {
    get_turn_sendrcvbw(gpTurn, sbw, rbw);
  }
}//gpTurn
