⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mg.c

📁 一个Megaco实现源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
// ``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 "mg.h"#include "mg_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      "mg"          /* The name part our node name */#define DNAME_DEFAULT      "gateway"     /* Our mid */#define TRANSPORT_DEFAULT  TRANSPORT_UDP#define ENCODING_DEFAULT   ENCODING_TEXT/* Global variables */static CORBA_Environment *send_env;  /* This represents us */static CORBA_Environment *receive_env; /*  */static char cookie[COOKIESZ];                       /* default value */static char mhost[HOSTNAMESZ];                      /* localhost is default */static char transport[NAMESZ] = TRANSPORT_DEFAULT;  static char encoding[NAMESZ] = ENCODING_DEFAULT;  static char host[HOSTNAMESZ] = "";                  /* own host */static char dname[NAMESZ]    = DNAME_DEFAULT;static char fhost[HOSTNAMESZ];                      /* localhost is default */static char fname[NAMESZ]    = FNAME_DEFAULT;       /* default value */static char fnode[NODENAMESZ]; static char uname[NAMESZ]    = UNAME_DEFAULT;       /* default value */static char unode[NODENAMESZ];static int  verbose = FALSE;static int  stop    = FALSE;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 mg_main(CORBA_Environment *s_env, 		    CORBA_Environment *r_env, 		    int fd, int sd);/* 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 mg -> initiate session user\n") );    mg_user_initiate(verbose, stop, 		     dname, unode, mhost, 		     transport, encoding, 		     send_env, receive_env);        /* start server loop */    V( ("starting mg -> entering main server loop\n") );    mg_main(send_env, receive_env, sd, epmd_fd);        /* Hook for session user */    V( ("stopping mg -> terminate session user\n") );    mg_user_terminate(send_env, receive_env);    /* Cleanup file descriptors and stuff */    mg_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 '-sname'\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],"-mhost") == 0) {      if (++i < argc) {	strcpy(mhost, argv[i++]);      } else {	fprintf(stderr, "Missing argument for option '-mhost'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-t") == 0) {      if (++i < argc) {	strcpy(transport, argv[i++]);      } else {	fprintf(stderr, "Missing argument for option '-t'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-e") == 0) {      if (++i < argc) {	strcpy(encoding, argv[i++]);      } else {	fprintf(stderr, "Missing argument for option '-e'\n");	usage();	exit(-1);      }    } else if (strcmp(argv[i],"-v") == 0) {      verbose = TRUE;      i++;    } else if (strcmp(argv[i],"-stop") == 0) {      stop = 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]);

⌨️ 快捷键说明

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