mgc.c

来自「一个Megaco实现源代码」· C语言 代码 · 共 576 行 · 第 1/2 页

C
576
字号
// ``The contents of this file are subject to the Erlang Public License,// Version 1.1, (the "License"); you may not use this file except in// compliance with the License. You should have received a copy of the// Erlang Public License along with this software. If not, it can be// retrieved via the world wide web at http://www.erlang.org/.//// Software distributed under the License is distributed on an "AS IS"// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See// the License for the specific language governing rights and limitations// under the License.//// The Initial Developer of the Original Code is Ericsson Utvecklings AB.// Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings// AB. All Rights Reserved.''////     $Id$#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <netdb.h>#include "megaco_session.h"#include "ic.h"#include "mgc.h"#include "mgc_user.h"#include "verbose.h"#define FALSE 0#define TRUE  1#define NODENAMESZ  512#define HOSTNAMESZ  256#define COOKIESZ    128#define NAMESZ      128#define INBUFSZ    1024#define OUTBUFSZ   1024 #define V( proto ) if (verbose) VERBOSE( proto )#define FNAME_DEFAULT          "megaco_session"#define COOKIE_DEFAULT         "nocookie"#define UNAME_DEFAULT          "mgc"#define DNAME_DEFAULT          "controller"  /* The mid *//* Global variables */static CORBA_Environment *send_env;  /* This represents us */static CORBA_Environment *receive_env; /*  */static char cookie[COOKIESZ];                  /* default value */static char host[HOSTNAMESZ] = "";             /* own host */static char fhost[HOSTNAMESZ];                 /* localhost is default */static char fname[NAMESZ]    = FNAME_DEFAULT;  /* default value */static char fnode[NODENAMESZ]; static char dname[NAMESZ]    = DNAME_DEFAULT;static char uname[NAMESZ]    = UNAME_DEFAULT;static int  verbose = FALSE;static char unode[NODENAMESZ];static int  readline(FILE* file, char* buf, int bufSz);static void getconfig(int argc, char **argv);static void usage();static int sd;static int epmd_fd;/* Session specific */static void mgc_main(CORBA_Environment *s_env, 		     CORBA_Environment *r_env, 		     int fd, int sd);// extern ___map___* ___merge___(___map___*,int); /* Initialization and termination */static ___map___* InitSwitch();static int  einit(int *sd, int *portnr, int *epmd_fd);static void eterminate(int *fd, int *sd, int *epmd_fd);/* Low level connection stuff */static int getport(int sockd);static int getlisten(int port);int main(int argc, char **argv) {    int portnr;        getconfig(argc, argv);        /* create file descriptors */    if (einit(&sd, &portnr, &epmd_fd) < 0)	return -1;    /* Hook for session user */    V( ("starting mgc -> initiate session user\n") );    mgc_user_initiate(verbose, dname, unode, send_env, receive_env);        /* start server loop */    V( ("starting mgc -> entering main server loop\n") );    mgc_main(send_env, receive_env, sd, epmd_fd);        /* Hook for session user */    V( ("stopping mgc -> terminate session user\n") );    mgc_user_terminate(send_env, receive_env);    /* Cleanup file descriptors and stuff */    mgc_exit(send_env, receive_env);    return 0;}static void init_cookie(){  int   hlen;  char* home;  if (NULL == (home = getenv("HOME"))) {    /* fprintf(stderr,"can't find home directory\n");  */    strcpy(cookie, COOKIE_DEFAULT);    return;  }  hlen = strlen(home);  {    char  cookie_file_name[hlen + 15];    FILE* cookie_file;        sprintf(cookie_file_name, "%s/.erlang.cookie", home);    if (NULL == (cookie_file = fopen(cookie_file_name, "r"))) {      /* fprintf(stderr,"can't open cookie file\n");  */      strcpy(cookie, COOKIE_DEFAULT);      return;    }        if (0 > readline(cookie_file, cookie, sizeof(cookie))) {      strcpy(cookie, COOKIE_DEFAULT);    }    fclose(cookie_file);        /* printf("cookie: '%s'\n", cookie); */  }}static int readline(FILE* file, char* buf, int bufSz){  buf[bufSz-1] = 1;  if (fgets(buf, bufSz, file)) {    if ((buf[bufSz-1] == 0) && (buf[bufSz-2] != '\n')) {      /* Did not get the entire line: flush in the rest */      do {        buf[bufSz-1] = 1;        if (!fgets(buf,bufSz,file)) {	  return 0; /* eof */	}      } while ((buf[bufSz-1] == 0) && (buf[bufSz-2] != '\n'));      buf[0] = 0;      return -1; /* long */    }    return 1; /* line ok */  }  return 0; /* eof */}static void getconfig(int argc, char **argv) {  int i = 1;    init_cookie();    /* retrieve arguments */  while (i < argc) {        /* Check for '-cookie' argument */    if (strcmp(argv[i],"-cookie") == 0) {      if (++i < argc) {	strcpy(cookie, argv[i++]);      } else {	fprintf(stderr,"Missing argument for option '-cookie'\n");	exit(-1);      }    } else if (strcmp(argv[i],"-fhost") == 0) {      if (++i < argc) {	strcpy(fhost, argv[i++]);      } else {	fprintf(stderr,"Missing argument for mandatory option '-fhost'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-fname") == 0) {      if (++i < argc) {	strcpy(fname, argv[i++]);      } else {	fprintf(stderr,"Missing argument for option '-fname'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-dname") == 0) {      if (++i < argc) {	strcpy(dname, argv[i++]);      } else {	fprintf(stderr, "Missing argument for option '-dname'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-sname") == 0) {      if (++i < argc) {        if (strlen(host) == 0) {          strcpy(uname, argv[i++]);          if ((gethostname(host,HOSTNAMESZ))) {            fprintf(stderr,"can't find own hostname\n");            exit(-1);          }        } else {          /* Host name already set. Duplicate name options */          fprintf(stderr,                   "Duplicate name options. Host name already set to %s\n",                   host);          usage();          exit(-1);        }      } else {	fprintf(stderr, "Missing argument for option '-uname'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-name") == 0) {      if (++i < argc) {        /* Fully qualified node name, i.e. name@host.         * Split the string into two parts, the name          * and the host parts */        if (strlen(host) == 0) {          char* full_node_name = argv[i++];          char* tmp = strstr(full_node_name,"@");                    if (tmp == NULL) {            /* We don't support this variant. Yet... */            fprintf(stderr,                     "We do not yet support this naming scheme for '-name'\n");            usage();            exit(-1);          } else {            char* name = strtok(full_node_name,"@");            tmp++;                        /* We are done for now */            strcpy(uname,name);            strcpy(host,tmp);          }        } else {          /* Host name already set. Duplicate name options */          fprintf(stderr,                   "Duplicate name options. Host name already set to %s\n",                   host);          usage();          exit(-1);        }      } else {        fprintf(stderr, "Missing argument for option '-name'\n");        usage();        exit(-1);      }    } else if (strcmp(argv[i],"-v") == 0) {      verbose = TRUE;      i++;    } else if (strcmp(argv[i],"-h") == 0) {      usage();      exit(1);    } else if (strcmp(argv[i],"-version") == 0) {      fprintf(stderr, "%s\n", VERSION);      exit(1);    } else {      fprintf(stderr,"ERROR: unknown option: '%s'\n", argv[i]);      usage();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?