📄 ss7capd.c
字号:
/***************************************************************************** @(#) ss7capd.c,v openss7-0_9_2_E(0.9.2.2) 2007/01/21 20:22:41 ----------------------------------------------------------------------------- Copyright (c) 2001-2007 OpenSS7 Corporation <http://www.openss7.com/> Copyright (c) 1997-2000 Brian F. G. Bidulock <bidulock@openss7.org> All Rights Reserved. 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; version 2 of the License. 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., 675 Mass Ave, Cambridge, MA 02139, USA. ----------------------------------------------------------------------------- U.S. GOVERNMENT RESTRICTED RIGHTS. If you are licensing this Software on behalf of the U.S. Government ("Government"), the following provisions apply to you. If the Software is supplied by the Department of Defense ("DoD"), it is classified as "Commercial Computer Software" under paragraph 252.227-7014 of the DoD Supplement to the Federal Acquisition Regulations ("DFARS") (or any successor regulations) and the Government is acquiring only the license rights granted herein (the license rights customarily provided to non-Government users). If the Software is supplied to any unit or agency of the Government other than DoD, it is classified as "Restricted Computer Software" and the Government's rights in the Software are defined in paragraph 52.227-19 of the Federal Acquisition Regulations ("FAR") (or any successor regulations) or, in the cases of NASA, in paragraph 18.52.227-86 of the NASA Supplement to the FAR (or any successor regulations). ----------------------------------------------------------------------------- Commercial licensing and support of this software is available from OpenSS7 Corporation at a fee. See http://www.openss7.com/ ----------------------------------------------------------------------------- Last Modified 2007/01/21 20:22:41 by brian ----------------------------------------------------------------------------- ss7capd.c,v Revision 0.9.2.2 2007/01/21 20:22:41 brian - working up drivers Revision 0.9.2.1 2007/01/15 11:33:57 brian - added new and old signalling link utilities *****************************************************************************/#ident "@(#) ss7capd.c,v openss7-0_9_2_E(0.9.2.2) 2007/01/21 20:22:41"static char const ident[] = "ss7capd.c,v openss7-0_9_2_E(0.9.2.2) 2007/01/21 20:22:41";#include <stropts.h>#include <stdlib.h>#include <stdint.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/poll.h>#include <sys/time.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <fcntl.h>#ifdef _GNU_SOURCE#include <getopt.h>#endif#include <time.h>#include <signal.h>#include <syslog.h>#include <sys/utsname.h>#include <ss7/lmi.h>#include <ss7/lmi_ioctl.h>#include <ss7/sdli.h>#include <ss7/sdli_ioctl.h>#include <ss7/sdti.h>#include <ss7/sdti_ioctl.h>int output = 1;int debug = 0;int nomead = 0;int card = 0;int span = 0;int slot = 1;int cap_fd = -1;int link_state = 0;char outfile[256] = "";char errfile[256] = "";char outpath[256] = "";char errpath[256] = "";char lnkname[256] = "";char cfgfile[256] = "";char outpdir[256] = "/var/log/ss7capd";char devname[256] = "/dev/x400p-sl";#define BUFSIZE 512char cbuf[BUFSIZE];char dbuf[BUFSIZE];struct strbuf ctrl = { sizeof(*cbuf), 0, cbuf };struct strbuf data = { sizeof(*dbuf), 0, dbuf };struct strioctl ctl;struct capconfig { lmi_option_t opt; sdl_config_t sdl; sdt_config_t sdt;} capconf;uint32_t iftype = SDL_TYPE_DS0A;uint32_t ifrate = 56000;voidversion(int argc, char **argv){ if (!output && !debug) return; fprintf(stdout, "\%2$s\n\Copyright (c) 2001-2007 OpenSS7 Corporation. All Rights Reserved.\n\Distributed under GPL Version 2, included here by reference.\n\See `%1$s --copying' for copying permissions.\n\", argv[0], ident);}voidusage(int argc, char **argv){ if (!output && !debug) return; fprintf(stderr, "\Usage:\n\ %1$s [options] {-f|--cfgfile} cfgfile\n\ %1$s [options] {-e|--device} devname {-c|--card} card {-s|--span} span {-t|--slot} slot\n\ %1$s {-h|--help}\n\ %1$s {-V|--version}\n\", argv[0]);}voidhelp(int argc, char **argv){ if (!output && !debug) return; fprintf(stdout, "\Usage:\n\ %1$s [options] {-f|--cfgfile} cfgfile\n\ %1$s [options] {-e|--device} devname {-c|--card} card {-s|--span} span {-t|--slot} slot\n\ %1$s {-h|--help}\n\ %1$s {-V|--version}\n\ %1$s {-C|--copying}\n\Arguments:\n\ (none)\n\Options:\n\ Command Options:\n\ -c, --card card (default: %2$d)\n\ card number (starting at 0)\n\ -s, --span span (default: %3$d)\n\ span number (starting at 0)\n\ -t, --slot slot (default: %4$d)\n\ timeslot number (starting at 1)\n\ Capture Options:\n\ -d, --daemon (default: off)\n\ run in the background as a daemon\n\ -O, --outpdir outpdir (default: %5$s)\n\ output directory for capture and errors\n\ -n, --lnkname lnkname (default: ss7capd$$)\n\ link name\n\ -o, --outfile outfile (default: ${lnkname}.out)\n\ redirect output to outfile (always as daemon)\n\ -l, --logfile errfile (default: ${lnkname}.err)\n\ redirect errors to errfile (always as daemon)\n\ -f, --cfgfile cfgfile (default: ss7capd.conf)\n\ configuration file name\n\ -e, --device devname (default: %6$s)\n\ device name to open\n\ -a, --ds0a (default: ds0a)\n\ channel is ds0a rather than ds0\n\ -0, --ds0 (default: ds0a)\n\ channel is ds0 rather than ds0a\n\ General Options:\n\ -q, --quiet (default: off)\n\ suppress output\n\ -D, --debug [LEVEL] (default: %7$d)\n\ increase or set debugging verbosity\n\ -v, --verbose [LEVEL] (default: %8$d)\n\ increase or set verbosity of output\n\ -h, --help\n\ prints this usage information and exits\n\ -V, --version\n\ prints the version and exits\n\ -C, --copying\n\ prints copying permissions and exits\n\", argv[0], card, span, slot, outpdir, devname, debug, output);}static voidcopying(int argc, char *argv[]){ if (!output && !debug) return; fprintf(stdout, "\--------------------------------------------------------------------------------\n\%1$s\n\--------------------------------------------------------------------------------\n\Copyright (c) 2001-2007 OpenSS7 Corporation <http://www.openss7.com>\n\Copyright (c) 1997-2000 Brian F. G. Bidulock <bidulock@openss7.org>\n\\n\All Rights Reserved.\n\--------------------------------------------------------------------------------\n\This program is free software; you can redistribute it and/or modify it under\n\the terms of the GNU General Public License as published by the Free Software\n\Foundation; version 2 of the License.\n\\n\This program is distributed in the hope that it will be useful, but WITHOUT ANY\n\WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A\n\PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\\n\You should have received a copy of the GNU General Public License along with\n\this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave,\n\Cambridge, MA 02139, USA.\n\--------------------------------------------------------------------------------\n\U.S. GOVERNMENT RESTRICTED RIGHTS. If you are licensing this Software on behalf\n\of the U.S. Government (\"Government\"), the following provisions apply to you. If\n\the Software is supplied by the Department of Defense (\"DoD\"), it is classified\n\as \"Commercial Computer Software\" under paragraph 252.227-7014 of the DoD\n\Supplement to the Federal Acquisition Regulations (\"DFARS\") (or any successor\n\regulations) and the Government is acquiring only the license rights granted\n\herein (the license rights customarily provided to non-Government users). If the\n\Software is supplied to any unit or agency of the Government other than DoD, it\n\is classified as \"Restricted Computer Software\" and the Government's rights in\n\the Software are defined in paragraph 52.227-19 of the Federal Acquisition\n\Regulations (\"FAR\") (or any successor regulations) or, in the cases of NASA, in\n\paragraph 18.52.227-86 of the NASA Supplement to the FAR (or any successor\n\regulations).\n\--------------------------------------------------------------------------------\n\Commercial licensing and support of this software is available from OpenSS7\n\Corporation at a fee. See http://www.openss7.com/\n\--------------------------------------------------------------------------------\n\", ident);}/* events */enum { CAP_NONE = 0, CAP_SUCCESS = 0, CAP_TIMEOUT, CAP_PCPROTO, CAP_PROTO, CAP_DATA, CAP_FAILURE = -1,};int alm_signal = 0;int hup_signal = 0;int trm_signal = 0;voidsig_handler(int signum){ switch (signum) { case SIGALRM: alm_signal = 1; break; case SIGHUP: hup_signal = 1; break; case SIGTERM: trm_signal = 1; break; } return;}intsig_register(int signum, void (*handler) (int)){ sigset_t mask; struct sigaction act; act.sa_handler = handler ? handler : SIG_DFL; act.sa_flags = handler ? SA_RESTART : 0; act.sa_restorer = NULL; sigemptyset(&act.sa_mask); if (sigaction(signum, &act, NULL)) return CAP_FAILURE; sigemptyset(&mask); sigaddset(&mask, signum); sigprocmask(handler ? SIG_UNBLOCK : SIG_BLOCK, &mask, NULL); return CAP_SUCCESS;}inthup_catch(void){ return sig_register(SIGHUP, sig_handler);}intalm_catch(void){ return sig_register(SIGALRM, sig_handler);}inttrm_catch(void){ return sig_register(SIGTERM, sig_handler);}voidsig_catch(void){ trm_catch(); hup_catch(); alm_catch();}inthup_block(void){ return sig_register(SIGHUP, NULL);}intalm_block(void){ return sig_register(SIGALRM, NULL);}inttrm_block(void){ return sig_register(SIGTERM, NULL);}voidsig_block(void){ alm_block(); hup_block(); trm_block();}int cap_stop(void);voidcap_exit(int retval){ syslog(LOG_NOTICE, "Exiting %d", retval); fflush(stdout); fflush(stderr); cap_stop(); sig_block(); closelog(); exit(retval);}static struct timeval tv;voidfprint_time(FILE * stream){ if (0 > tv.tv_usec || tv.tv_usec > 1000000) { tv.tv_sec += (tv.tv_usec / 1000000); tv.tv_usec %= 1000000; } fprintf(stream, "%012ld.%06ld", tv.tv_sec, tv.tv_usec);};intftimestamp(void){ while (gettimeofday(&tv, NULL) < 0) if (errno == EAGAIN || errno == EINTR || errno == ERESTART) { syslog(LOG_ERR, "%m"); syslog(LOG_ERR, "Could not read timestamp: winging it%s", outpath); break; } return CAP_SUCCESS;}/* generate output header */voidoutput_header(void){ char buf[128] = ""; struct utsname uts; ftimestamp(); fprint_time(stdout); fprintf(stdout, " # SS7CAPD ss7capd.c,v 0.9.2.2 2007/01/21 20:22:41 brian Exp Output File Header\n"); uname(&uts); fprint_time(stdout); fprintf(stdout, " # machine: %s %s %s %s %s\n", uts.sysname, uts.nodename, uts.release, uts.version, uts.machine); fprint_time(stdout); fprintf(stdout, " # device: %s (%d:%d:%d) %s\n", devname, card, span, slot, lnkname); fprint_time(stdout); if (outpath[0] != '\0') snprintf(buf, sizeof(buf), outpath); else snprintf(buf, sizeof(buf), "(stdout)"); fprintf(stdout, " # original file name: %s\n", buf); fprint_time(stdout); gethostname(buf, sizeof(buf)); fprintf(stdout, " # host: %s\n", buf); fprint_time(stdout); fprintf(stdout, " # date: %s", ctime(&tv.tv_sec));}voidtrm_action(void){ syslog(LOG_WARNING, "Caught SIGTERM, shutting down"); cap_exit(0);}inthup_action(void){ syslog(LOG_WARNING, "Caught SIGHUP, reopening files."); if (output > 1) syslog(LOG_NOTICE, "Reopening output file %s", outpath); if (outpath[0] != '\0') { fflush(stdout); fclose(stdout); if (freopen(outpath, "a", stdout) == NULL) { syslog(LOG_ERR, "%m"); syslog(LOG_ERR, "Could not reopen stdout file %s", outpath); } output_header(); } if (output > 1) syslog(LOG_NOTICE, "Reopening error file %s", errpath); if (errpath[0] != '\0') { fflush(stderr); fclose(stderr); if (freopen(errpath, "a", stderr) == NULL) { syslog(LOG_ERR, "%m"); syslog(LOG_ERR, "Could not reopen stderr file %s", errpath); } } return (0);}intalm_action(void){ return (0);}intstart_timer(long duration){ struct itimerval setting = { {0, 0}, {duration / 1000, (duration % 1000) * 1000} }; if (alm_catch()) return CAP_FAILURE; if (setitimer(ITIMER_REAL, &setting, NULL)) return CAP_FAILURE; alm_signal = 0; return CAP_SUCCESS;}intstop_timer(void){ return alm_block();}voidprint_data(void){ static unsigned char hexchar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; int i; for (i = 0; i < data.len; i++) { fputc(hexchar[(dbuf[i] >> 4) & 0x0f], stdout); fputc(hexchar[(dbuf[i] >> 0) & 0x0f], stdout); }}intdecode_data(void){ ftimestamp(); fprint_time(stdout); fputc(' ', stdout); print_data(); fputc('\n', stdout); return CAP_DATA;}intdecode_ctrl(int flag){ if (output > 2) fprintf(stderr, "Got control meesage! %d\n", *(uint32_t *) cbuf); switch (*(uint32_t *) cbuf) { case SDT_RC_SIGNAL_UNIT_IND: decode_data(); return CAP_NONE; /* go around again */ case LMI_INFO_ACK: case LMI_OK_ACK: case LMI_ERROR_ACK: case LMI_ENABLE_CON: case LMI_DISABLE_CON: case LMI_OPTMGMT_ACK:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -