From 8eba0b36536708a9770eaf4be54c3407498b2241 Mon Sep 17 00:00:00 2001 From: MelwareDE Date: Tue, 31 May 2005 17:30:59 +0000 Subject: [PATCH] Initial checkin of version 0.3 -- started at sourceforge. --- COPYING | 340 ++++++++++ Makefile | 46 ++ README | 62 ++ capi.c | 1545 +++++++++++++++++++++++++++++++++++++++++++++ capiconn.c | 1785 ++++++++++++++++++++++++++++++++++++++++++++++++++++ capiconn.h | 435 +++++++++++++ data.c | 236 +++++++ data.h | 39 ++ debug.c | 116 ++++ debug.h | 39 ++ isdntun.c | 427 +++++++++++++ isdntun.h | 93 +++ options.c | 114 ++++ options.h | 69 ++ signals.c | 117 ++++ signals.h | 36 ++ tun.c | 69 ++ tun.h | 31 + 18 files changed, 5599 insertions(+) create mode 100644 COPYING create mode 100644 Makefile create mode 100644 README create mode 100644 capi.c create mode 100644 capiconn.c create mode 100644 capiconn.h create mode 100644 data.c create mode 100644 data.h create mode 100644 debug.c create mode 100644 debug.h create mode 100644 isdntun.c create mode 100644 isdntun.h create mode 100644 options.c create mode 100644 options.h create mode 100644 signals.c create mode 100644 signals.h create mode 100644 tun.c create mode 100644 tun.h diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..60549be --- /dev/null +++ b/COPYING @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + 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 + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1aee058 --- /dev/null +++ b/Makefile @@ -0,0 +1,46 @@ +# +# Makefile for ISDN Tunnel Daemon +# + +VERSION=0.3 + +PROG=itund + +LIBS=-lcapi20 -lz + +FILES=isdntun.o tun.o capiconn.o debug.o options.o \ + capi.o signals.o data.o + +CFLAGS=-Wall -O2 -I. -DVERSION=\"$(VERSION)\" + +DESTDIR=/ +BINDIR=sbin + +all: $(PROG) + @echo "Done!" + + +.SUFFIXES: +.SUFFIXES: .c .o + +.c.o: + $(CC) $(CFLAGS) -c -o $*.o $*.c + +$(PROG): $(FILES) + $(CC) -o $(PROG) $(FILES) $(LDFLAGS) $(LIBS) + +clean: + @rm -f *.o $(PROG) *.tgz + +install: $(PROG) + mkdir -p $(DESTDIR)/$(BINDIR) + install -m 0755 $(PROG) $(DESTDIR)/$(BINDIR)/$(PROG) + +release: + @rm -rf $(PROG)-$(VERSION) + @mkdir -p $(PROG)-$(VERSION) + @cp *.h *.c $(PROG)-$(VERSION) + @cp README Makefile COPYING CHANGES $(PROG)-$(VERSION) + @tar czf $(PROG)-$(VERSION).tgz $(PROG)-$(VERSION) + @rm -rf $(PROG)-$(VERSION) + @echo Archive created. diff --git a/README b/README new file mode 100644 index 0000000..b50e420 --- /dev/null +++ b/README @@ -0,0 +1,62 @@ + + ISDN-Tunnel-Daemon "itund" + License GNU GPL + +Copyright 2004-2005 SYSGO Real-Time Solutions AG +Klein-Winternheim, Germany +http://www.sysgo.com +http://www.elinos.com + +Copyright 2004-2005 Cytronics & Melware + +ItunD is a user-space daemon to provide a network tunnel over ISDN lines. It works +similiar (and most important together with) the ISDN4Linux net-devices. +The ISDN link is provided as just another "network-card" to the kernel and can +be configured easily. +To provide the needed network interface, the generic TUN/TAP driver of kernels +2.4 and 2.6 is used. This driver is also used by vtun.sourceforge.net. + + +Impotant Notes +-------------- + +1. User-Space daemon only, no kernel patches necessary. +2. Options similar to PPPD CAPI options (pppdcapiplugin). +3. Works with ISDN4Linux (isdn_net) network (e.g. rawip). + + +Requirements +------------ + +1. ISDN card providing CAPI2.0 interface (Eicon Networks, AVM, ...) + The legacy isdn subsystem (isdn.o) is NOT necessary. +2. Enabled (and loaded) Generic TUN/TAP network driver. + + +Features +-------- + +When using itund on both sides, you may want to use encap "zipip" +which is basically rawip, but compressed if possible. + + +Examples +======== + +Callback Server (reject and call back) + itund --encap=rawip --controller=1 --protocol=hdlc \ + --number=5678 --msn=1234 --demand --idle=60 \ + --inmsn=1234 --coso=local --cbdelay=3 + +Client (expecting callback) + itund --encap=rawip --controller=1 --protocol=hdlc \ + --number=1234 --msn=5678 --demand --idle=60 \ + --inmsn=5678 --coso=remote --dialtimeout=1 + + + + +armin.schindler@sysgo.com +armin@melware.de + +$Id$ diff --git a/capi.c b/capi.c new file mode 100644 index 0000000..d1eb94e --- /dev/null +++ b/capi.c @@ -0,0 +1,1545 @@ +/* + * CAPI interface handling + * + * heavily based on capiplugin.c of pppdcapiplugin by + * Carsten Paeth (calle@calle.in-berlin.de) + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "isdntun.h" +#include "capiconn.h" +#include "options.h" +#include +#include +#include + + +/* -------------------------------------------------------------------- */ + +static int exiting = 0; + +/* -------------------------------------------------------------------- */ + +typedef struct stringlist { + struct stringlist *next; + char *s; +} STRINGLIST; + +/* -------------------------------------------------------------------- */ + +static int curphase = -1; +static int wakeupnow = 0; + +/* -------------------------------------------------------------------- */ + +static void handlemessages(void) ; +static int shmatch(char *string, char *expr); +static void stringlist_free(STRINGLIST **pp); +static int stringlist_append_string(STRINGLIST **pp, char *s); +static STRINGLIST *stringlist_split(char *tosplit, char *seps); +static void makecallback(void); +static char *phase2str(int phase); +static void wakeupdemand(void); + +/* -------------------------------------------------------------------- */ + +static capiconn_context *ctx; +static unsigned applid; +#define CM(x) (1<<(x)) +#define CIPMASK_ALL 0x1FFF03FF +#define CIPMASK_VOICE (CM(1)|CM(4)|CM(5)|CM(16)|CM(26)) +#define CIPMASK_DATA (CM(2)|CM(3)) +static unsigned long cipmask = CIPMASK_ALL; +static int controller = 1; +static capi_contrinfo cinfo = { 0 , 0, 0 }; + +/* -------------------------------------------------------------------- */ + +/* + * numbers + */ +static STRINGLIST *numbers; +static STRINGLIST *callbacknumbers; +static STRINGLIST *clis; +static STRINGLIST *inmsns; +static STRINGLIST *controllers; + +/* + * protocol + */ +#define PROTO_HDLC 0 +#define PROTO_X75 1 +static int proto = PROTO_HDLC; + +/* + * leased line + */ +static unsigned char AdditionalInfo[1+2+2+31]; + +/* + * callback & COSO + */ +#define COSO_CALLER 0 +#define COSO_LOCAL 1 +#define COSO_REMOTE 2 +static int coso = COSO_CALLER; + +/* -------------------------------------------------------------------- */ +/* -------- Handle leased lines (CAPI-Bundling) ----------------------- */ +/* -------------------------------------------------------------------- */ + +static int decodechannels(char *teln, unsigned long *bmaskp, int *activep) +{ + unsigned long bmask = 0; + int active = !0; + char *s; + int channel; + int i; + + s = teln; + while (*s && *s == ' ') s++; + if (!*s) + fatal("option channels: list empty"); + if (*s == 'p' || *s == 'P') { + active = 0; + s++; + } + if (*s == 'a' || *s == 'A') { + active = !0; + s++; + } + while (*s) { + int digit1 = 0; + int digit2 = 0; + if (!isdigit(*s)) + goto illegal; + while (isdigit(*s)) { digit1 = digit1*10 + (*s - '0'); s++; } + channel = digit1; + if (channel <= 0 && channel > 30) + goto rangeerror; + if (*s == 0 || *s == ',' || *s == ' ') { + bmask |= (1 << digit1); + digit1 = 0; + if (*s) s++; + continue; + } + if (*s != '-') + goto illegal; + s++; + if (!isdigit(*s)) return -3; + while (isdigit(*s)) { digit2 = digit2*10 + (*s - '0'); s++; } + channel = digit2; + if (channel <= 0 && channel > 30) + goto rangeerror; + if (*s == 0 || *s == ',' || *s == ' ') { + if (digit1 > digit2) + for (i = digit2; i <= digit1 ; i++) + bmask |= (1 << i); + else + for (i = digit1; i <= digit2 ; i++) + bmask |= (1 << i); + digit1 = digit2 = 0; + if (*s) s++; + continue; + } + goto illegal; + } + if (activep) *activep = active; + if (bmaskp) *bmaskp = bmask; + return 0; +illegal: + fatal("option channels: illegal octet '%c'", *s); + return -1; +rangeerror: + fatal("option channels: channel %d out of range", channel); + return -1; +} + +static int channels2capi20(char *teln, unsigned char *AdditionalInfo) +{ + unsigned long bmask; + int active; + int i; + + decodechannels(teln, &bmask, &active); + /* info("\"%s\" 0x%lx %d\n", teln, bmask, active); */ + /* Length */ + AdditionalInfo[0] = 2+2+31; + /* Channel: 3 => use channel allocation */ + AdditionalInfo[1] = 3; AdditionalInfo[2] = 0; + /* Operation: 0 => DTE mode, 1 => DCE mode */ + if (active) { + AdditionalInfo[3] = 0; AdditionalInfo[4] = 0; + } else { + AdditionalInfo[3] = 1; AdditionalInfo[4] = 0; + } + /* Channel mask array */ + AdditionalInfo[5] = 0; /* no D-Channel */ + for (i=1; i <= 30; i++) + AdditionalInfo[5+i] = (bmask & (1 << i)) ? 0xff : 0; + return 0; +} + + + +/* -------------------------------------------------------------------- */ + +static void itun_check_options(void) +{ + static int init = 0; + + if (init) + return; + init = 1; + + /* + * protocol + */ + if (strcasecmp(opt_proto, "hdlc") == 0) { + proto = PROTO_HDLC; + } else if (strcasecmp(opt_proto, "x75") == 0) { + proto = PROTO_X75; + } else { + option_error("unknown protocol \"%s\"", opt_proto); + die(1); + } + cipmask = CIPMASK_DATA; + + /* + * coso + */ + if (opt_coso == 0) { + if (opt_cbflag) { + if (opt_number) coso = COSO_REMOTE; + else coso = COSO_LOCAL; + } else { + coso = COSO_CALLER; + } + } else if (strcasecmp(opt_coso, "caller") == 0) { + coso = COSO_CALLER; + } else if (strcasecmp(opt_coso, "local") == 0) { + coso = COSO_LOCAL; + } else if (strcasecmp(opt_coso, "remote") == 0) { + coso = COSO_REMOTE; + } else { + option_error("wrong value for option coso"); + die(1); + } + if (opt_cbflag) { + if (opt_coso) { + option_error("option clicb ignored"); + } else if (coso == COSO_REMOTE) { + dbglog("clicb selected coso remote"); + } else if (coso == COSO_LOCAL) { + dbglog("clicb selected coso local"); + } else { + option_error("option clicb ignored"); + } + } + + /* + * leased line + */ + if (opt_channels) { + channels2capi20(opt_channels, AdditionalInfo); + if (opt_number) + option_error("option number ignored"); + if (opt_numberprefix) + option_error("option numberprefix ignored"); + if (opt_callbacknumber) + option_error("option callbacknumber ignored"); + if (opt_msn) + option_error("option msn ignored"); + if (opt_inmsn) + option_error("option inmsn ignored"); + /* + * dialout + */ + } else if (opt_number) { + stringlist_free(&numbers); + numbers = stringlist_split(opt_number, " \t,"); + /* + * dialin + */ + } else { + if (coso == COSO_LOCAL) { + if (opt_callbacknumber == 0) { + option_error("option callbacknumber missing"); + die(1); + } + } + } + + if (opt_callbacknumber) { + stringlist_free(&callbacknumbers); + callbacknumbers = stringlist_split(opt_callbacknumber, " \t,"); + } + + /* + * ddi + */ + memset(&cinfo, 0, sizeof(cinfo)); + if (opt_ddi) { + STRINGLIST *parsed_ddi; + STRINGLIST *sl; + char *tmp; + parsed_ddi = stringlist_split(opt_ddi, " \t,"); + sl = parsed_ddi; + cinfo.ddi = strdup(sl->s); + if (cinfo.ddi == 0) { + stringlist_free(&parsed_ddi); + option_error("option ddi error"); + die(1); + } + if (sl->next && sl->next->s) { + sl = sl->next; + cinfo.ndigits = strtol(sl->s, &tmp, 10); + if (tmp == sl->s || *tmp) { + stringlist_free(&parsed_ddi); + option_error("option ddi error"); + die(1); + } + } + stringlist_free(&parsed_ddi); + } + + /* + * controller + */ + if (opt_controller) { + STRINGLIST *sl; + int count = 0, contr; + stringlist_free(&controllers); + controllers = stringlist_split(opt_controller, " \t,"); + for (sl = controllers; sl; sl = sl->next) { + count++; + contr = strtol(sl->s, NULL, 10); + if ((contr < 1) || (contr > 30)) { + option_error("illegal controller specification \"%s\"", + opt_controller); + die(1); + } + if (count == 1) + controller = contr; + } + if (count < 2) { + stringlist_free(&controllers); + } + } + + /* + * cli & inmsn + */ + if (opt_cli) { + STRINGLIST *sl; + char *old; + stringlist_free(&clis); + clis = stringlist_split(opt_cli, " \t,"); + for (sl = clis; sl; sl = sl->next) { + if (*sl->s != '*') { + old = sl->s; + sl->s = (char *)malloc(strlen(old)+2); + if (sl->s) { + *sl->s = '*'; + strcpy(sl->s+1, old); + free(old); + } else { + sl->s = old; + option_error("prepend '*' to cli failed"); + } + } + } + } + if (opt_inmsn) { + stringlist_free(&inmsns); + inmsns = stringlist_split(opt_inmsn, " \t,"); + } + + /* + * dial on demand + */ + if (demand) { + if (!opt_number && !opt_channels) { + option_error("number or channels missing for demand"); + die(1); + } + if (opt_voicecallwakeup) + cipmask |= CIPMASK_VOICE; + } else if (opt_voicecallwakeup) { + option_error("option voicecallwakeup ignored"); + opt_voicecallwakeup = 0; + } + + return; +} + +/* -------------------------------------------------------------------- */ +/* -------- Match with * and ? ---------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static int shmatch(char *string, char *expr) +{ + char *match = expr; + char *s = string; + char *p; + int escape = 0; + + while (*match && *s) { + if (escape) { + if (*s != *match) + return 0; + s++; + match++; + } else if (*match == '\\') { + match++; + escape = 1; + } else if (*match == '*') { + match++; + if (*match == 0) + return 1; + if (*match == '\\') + match++; + while ((p = strchr(s, *match)) != 0) { + if (shmatch(p+1, match+1)) + return 1; + s = p + 1; + } + return 0; + } else if (*match == '?') { + s++; + match++; + } else { + if (*s != *match) + return 0; + s++; + match++; + } + } + if (*s == 0) { + if (*match == 0) return 1; + if (*match == '*' && match[1] == 0) return 1; + } + return 0; +} + +/* -------------------------------------------------------------------- */ +/* -------- STRINGLIST for parsing ------------------------------------ */ +/* -------------------------------------------------------------------- */ + +static void stringlist_free(STRINGLIST **pp) +{ + STRINGLIST *p, *next; + + p = *pp; + while (p) { + next = p->next; + if (p->s) free(p->s); + free(p); + p = next; + } + *pp = 0; +} + +static int stringlist_append_string(STRINGLIST **pp, char *s) +{ + STRINGLIST *p; + for (; *pp; pp = &(*pp)->next) ; + if ((p = (STRINGLIST *)malloc(sizeof(STRINGLIST))) == 0) + return -1; + memset(p, 0, sizeof(STRINGLIST)); + if ((p->s = strdup(s)) == 0) { + free(p); + return -1; + } + p->next = 0; + *pp = p; + return 0; +} + +static STRINGLIST *stringlist_split(char *tosplit, char *seps) +{ + STRINGLIST *p = 0; + char *str = strdup(tosplit); + char *s; + if (!str) return 0; + for (s = strtok(str, seps); s; s = strtok(0, seps)) { + if (*s == 0) continue; /* if strtok is not working correkt */ + if (stringlist_append_string(&p, s) < 0) { + stringlist_free(&p); + free(str); + return 0; + } + } + free(str); + return p; +} + +/* -------------------------------------------------------------------- */ +/* -------- connection management ------------------------------------- */ +/* -------------------------------------------------------------------- */ + +typedef struct conn { + struct conn *next; + capi_connection *conn; + int type; +#define CONNTYPE_OUTGOING 0 +#define CONNTYPE_INCOMING 1 +#define CONNTYPE_IGNORE 2 +#define CONNTYPE_REJECT 3 +#define CONNTYPE_FOR_CALLBACK 4 + int inprogress; + int isconnected; +} CONN; + +static CONN *connections; + +/* -------------------------------------------------------------------- */ + +static CONN *conn_remember(capi_connection *conn, int type) +{ + CONN *p, **pp; + for (pp = &connections; *pp; pp = &(*pp)->next) ; + if ((p = (CONN *)malloc(sizeof(CONN))) == 0) { + int serrno = errno; + fatal("malloc failed - %s (%d)", + strerror(serrno), serrno); + return 0; + } + memset(p, 0, sizeof(CONN)); + p->conn = conn; + p->type = type; + p->next = 0; + switch (type) { + case CONNTYPE_OUTGOING: + case CONNTYPE_INCOMING: + case CONNTYPE_FOR_CALLBACK: + p->inprogress = 1; + p->isconnected = 0; + break; + default: + break; + } + *pp = p; + return p; +} + +static int conn_forget(capi_connection *conn) +{ + CONN **pp, *p; + for (pp = &connections; *pp && (*pp)->conn != conn; pp = &(*pp)->next) ; + if (*pp) { + p = *pp; + *pp = (*pp)->next; + free(p); + return 0; + } + return -1; +} + +static CONN *conn_find(capi_connection *cp) +{ + CONN *p; + for (p = connections; p; p = p->next) { + if (p->conn == cp) + return p; + } + return 0; +} + +/* -------------------------------------------------------------------- */ + +static int conn_connected(capi_connection *conn) +{ + CONN *p; + for (p = connections; p; p = p->next) { + if (p->conn == conn) { + p->isconnected = 1; + p->inprogress = 0; + return 0; + } + } + fatal("connected connection not found ??"); + return -1; +} + +/* -------------------------------------------------------------------- */ + +static int conn_incoming_connected(void) +{ + CONN *p; + for (p = connections; p; p = p->next) { + if (p->type == CONNTYPE_INCOMING) + return p->isconnected; + } + return 0; +} + +static int conn_incoming_exists(void) +{ + CONN *p; + for (p = connections; p; p = p->next) { + if (p->type == CONNTYPE_INCOMING) + return p->isconnected || p->inprogress; + } + return 0; +} + +/* -------------------------------------------------------------------- */ + +static int conn_inprogress(capi_connection *cp) +{ + CONN *p; + for (p = connections; p; p = p->next) { + if (p->conn == cp) + return p->inprogress; + } + return 0; +} + +static int conn_isconnected(capi_connection *cp) +{ + CONN *p; + if (cp) { + for (p = connections; p; p = p->next) { + if (p->conn == cp) + return p->isconnected; + } + } else { + for (p = connections; p; p = p->next) { + if (p->isconnected) + return 1; + } + } + return 0; +} + +/* -------------------------------------------------------------------- */ +/* -------------------------------------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static int act_controller = 1; + +static void reset_contr_status(void) +{ + act_controller = 1; +} + +static int get_next_contr(void) +{ + STRINGLIST *sl; + int contr, count = 0; + + if (!controllers) { + /* one controller only */ + return(controller); + } + + for (sl = controllers; sl; sl = sl->next) { + count++; + contr = strtol(sl->s, NULL, 10); + if (count == act_controller) { + act_controller++; + return(contr); + } + } + act_controller = 1; + return(strtol(controllers->s, NULL, 10)); +} + +static void do_listen(unsigned cipmask, unsigned cipmask2) +{ + STRINGLIST *sl; + int contr; + + if (!controllers) { + /* one controller only */ + (void) capiconn_listen(ctx, controller, cipmask, cipmask2); + return; + } + + for (sl = controllers; sl; sl = sl->next) { + contr = strtol(sl->s, NULL, 10); + (void) capiconn_listen(ctx, contr, cipmask, cipmask2); + } +} + +/* -------------------------------------------------------------------- */ +/* -------- demand & wakeup ------------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void setupincoming_for_demand(void) +{ + do_listen(cipmask, 0); + info("waiting for demand wakeup ..."); +} + +static void wakeupdemand(void) +{ + if (curphase != PHASE_DORMANT) { + info("wakeup not possible in phase %s, delayed", + phase2str(curphase)); + wakeupnow++; + return; + } + capi_wake_up(); +} + +/* -------------------------------------------------------------------- */ +/* -------- CAPI setup & handling ------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void init_capiconn(void) +{ + static int init = 0; + STRINGLIST *sl; + int contr; + + if (init) { + do_listen(0, 0); + return; + } + init = 1; + + if (!controllers) { + /* one controller only */ + if (capiconn_addcontr(ctx, controller, &cinfo) != CAPICONN_OK) { + (void)capiconn_freecontext(ctx); + (void)capi20_release(applid); + fatal("add controller %d failed", controller); + return; + } + if (cinfo.ddi) + dbglog("contr=%d ddi=\"%s\" n=%d", + controller, cinfo.ddi, cinfo.ndigits); + else + dbglog("contr=%d", controller); + } else { + for (sl = controllers; sl; sl = sl->next) { + contr = strtol(sl->s, NULL, 10); + if (capiconn_addcontr(ctx, contr, &cinfo) != CAPICONN_OK) { + (void)capiconn_freecontext(ctx); + (void)capi20_release(applid); + fatal("add controller %d failed", contr); + return; + } + if (cinfo.ddi) + dbglog("contr=%d ddi=\"%s\" n=%d", + contr, cinfo.ddi, cinfo.ndigits); + else + dbglog("contr=%d", contr); + } + } + + do_listen(0, 0); +} + +/* -------------------------------------------------------------------- */ + +void handle_capi_message(void) +{ + unsigned char *msg = 0; + if (capi20_get_message(applid, &msg) == 0) + capiconn_inject(applid, msg); +} + +static void handlemessages(void) +{ + unsigned char *msg = 0; + struct timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + if (capi20_waitformessage(applid, &tv) == 0) { + if (capi20_get_message(applid, &msg) == 0) + capiconn_inject(applid, msg); + } +} + +/* -------------------------------------------------------------------- */ + +static void dodisconnect(capi_connection *cp) +{ + CONN *conn; + time_t t; + + if ((conn = conn_find(cp)) == 0) + return; + (void)capiconn_disconnect(cp, 0); + conn->isconnected = conn->inprogress = 0; + + t = time(0)+10; + do { + handlemessages(); + } while (conn_find(cp) && time(0) < t); + + if (conn_find(cp)) + fatal("timeout while waiting for disconnect"); +} + +static void disconnectall(void) +{ + time_t t; + CONN *p; + + do_listen(0, 0); + for (p = connections; p; p = p->next) { + if (p->inprogress || p->isconnected) { + p->isconnected = p->inprogress = 0; + capiconn_disconnect(p->conn, 0); + } + } + t = time(0)+10; + do { + handlemessages(); + } while (connections && time(0) < t); + + if (connections && !exiting) + fatal("disconnectall failed"); +} + +/* -------------------------------------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static char *conninfo(capi_connection *p) +{ + static char buf[1024]; + capi_conninfo *cp = capiconn_getinfo(p); + char *callingnumber = ""; + char *callednumber = ""; + + if (cp->callingnumber && cp->callingnumber[0] > 2) + callingnumber = cp->callingnumber+3; + if (cp->callednumber && cp->callednumber[0] > 1) + callednumber = cp->callednumber+2; + + snprintf(buf, sizeof(buf), + "\"%s\" -> \"%s\" %s (pcli=0x%x/ncci=0x%x)", + callingnumber, callednumber, + cp->isincoming ? "incoming" : "outgoing", + cp->plci, cp->ncci + ); + buf[sizeof(buf)-1] = 0; + return buf; +} + +/* -------------------------------------------------------------------- */ +/* -------- reject reason handling (wuerg) ---------------------------- */ +/* -------------------------------------------------------------------- */ + +static unsigned dreason = 0; + +static int was_no_reject(void) +{ + if ((dreason & 0xff00) != 0x3400) + return 1; + switch (dreason) { + case 0x34a2: /* No circuit / channel available */ + return 1; + } + return 0; +} + +/* -------------------------------------------------------------------- */ +/* -------- disconnect handler ---------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void disconnected(capi_connection *cp, + int localdisconnect, + unsigned reason, + unsigned reason_b3) +{ + CONN *p; + + if ((p = conn_find(cp)) == 0) + return; + + conn_forget(cp); + + capi_is_disconnected(cp); + + switch (p->type) { + case CONNTYPE_OUTGOING: + case CONNTYPE_INCOMING: + break; + case CONNTYPE_IGNORE: + case CONNTYPE_REJECT: + return; + case CONNTYPE_FOR_CALLBACK: + dreason = reason; + break; + + } + if (reason != 0x3304) /* Another Applikation got the call */ + info("disconnect(%s): %s 0x%04x (0x%04x) - %s", + localdisconnect ? "local" : "remote", + conninfo(cp), + reason, reason_b3, capi_info2str(reason)); +} + +/* -------------------------------------------------------------------- */ +/* -------- incoming call handler ------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void incoming(capi_connection *cp, + unsigned contr, + unsigned cipvalue, + char *callednumber, + char *callingnumber) +{ + STRINGLIST *p; + char *s; + + info("incoming call: %s (0x%x)", conninfo(cp), cipvalue); + + if (conn_incoming_exists()) { + info("ignoring call, incoming connection exists"); + conn_remember(cp, CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + return; + } + + if (opt_inmsn) { + for (p = inmsns; p; p = p->next) { + if ( (s = strstr(callednumber, p->s)) != 0 + && strcmp(s, p->s) == 0) + break; + } + if (!p) { + info("ignoring call, msn %s not in \"%s\"", + callednumber, opt_inmsn); + conn_remember(cp, CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + return; + } + } else if (opt_msn) { + if ( (s = strstr(callednumber, opt_msn)) == 0 + || strcmp(s, opt_msn) != 0) { + info("ignoring call, msn mismatch (%s != %s)", + opt_msn, callednumber); + conn_remember(cp, CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + return; + } + } + + if (opt_cli) { + for (p = clis; p; p = p->next) { + if (shmatch(callingnumber, p->s)) + break; + } + if (!p) { + info("ignoring call, cli mismatch (%s != %s)", + opt_cli, callingnumber); + conn_remember(cp, CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + return; + } + } else if (opt_number) { + for (p = numbers; p; p = p->next) { + if ( (s = strstr(callingnumber, p->s)) != 0 + && strcmp(s, p->s) == 0) + break; + } + if (!p) { + info("ignoring call, number mismatch (%s != %s)", + opt_number, callingnumber); + conn_remember(cp, CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + return; + } + } else if (opt_acceptdelayflag) { + /* + * non cli or number match, + * give more specific listen a chance (bad) + */ + info("accept delayed, no cli or number match"); + sleep(1); + } + + switch (cipvalue) { + case 1: /* Speech */ + case 4: /* 3.1 kHz audio */ + case 5: /* 7 kHz audio */ + case 16: /* Telephony */ + case 26: /* 7 kHz telephony */ + if (opt_voicecallwakeup) { + goto wakeupdemand; + } else { + info("ignoring speech call from %s", + callingnumber); + conn_remember(cp,CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + } + break; + case 2: /* Unrestricted digital information */ + case 3: /* Restricted digital information */ + if (proto == PROTO_HDLC) { + if (demand) goto wakeupmatch; + if (coso == COSO_LOCAL) goto callback; + goto accept; + } else if (proto == PROTO_X75) { + if (demand) goto wakeupmatch; + if (coso == COSO_LOCAL) goto callback; + goto accept; + } else { + info("ignoring digital call from %s", + callingnumber); + conn_remember(cp,CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + } + break; + case 17: /* Group 2/3 facsimile */ + info("ignoring fax call from %s", + callingnumber); + conn_remember(cp,CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + break; + default: + info("ignoring type %d call from %s", + cipvalue, callingnumber); + conn_remember(cp,CONNTYPE_IGNORE); + (void) capiconn_ignore(cp); + break; + } + return; + +callback: + do_listen(0, 0); + dbglog("rejecting call: %s (0x%x)", conninfo(cp), cipvalue); + conn_remember(cp, CONNTYPE_REJECT); + capiconn_reject(cp); + makecallback(); + return; + +wakeupdemand: + dbglog("rejecting call: %s (0x%x)", conninfo(cp), cipvalue); + conn_remember(cp, CONNTYPE_REJECT); + capiconn_reject(cp); + wakeupdemand(); + return; + +wakeupmatch: + if (coso == COSO_LOCAL) + goto callback; + +accept: + switch (proto) { + default: + case PROTO_HDLC: + (void) capiconn_accept(cp, 0, 1, 0, 0, 0, 0, 0); + break; + case PROTO_X75: + (void) capiconn_accept(cp, 0, 0, 0, 0, 0, 0, 0); + break; + } + conn_remember(cp, CONNTYPE_INCOMING); + do_listen(0, 0); + return; +} + +/* -------------------------------------------------------------------- */ +/* -------- connection established handler ---------------------------- */ +/* -------------------------------------------------------------------- */ + +static void connected(capi_connection *cp, _cstruct NCPI) +{ + info("connected: %s", conninfo(cp)); + + if (conn_incoming_connected()) { + time_t t = time(0)+opt_connectdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + } + + capi_is_connected(cp); + + conn_connected(cp); + if (curphase == PHASE_DORMANT) + wakeupdemand(); +} + +/* -------------------------------------------------------------------- */ +/* -------- charge information ---------------------------------------- */ +/* -------------------------------------------------------------------- */ + +void chargeinfo(capi_connection *cp, unsigned long charge, int inunits) +{ + if (inunits) { + info("%s: charge in units: %d", conninfo(cp), charge); + } else { + info("%s: charge in currency: %d", conninfo(cp), charge); + } +} + +/* -------------------------------------------------------------------- */ +/* -------- tranfer capi message to CAPI ------------------------------ */ +/* -------------------------------------------------------------------- */ + +void put_message(unsigned appid, unsigned char *msg) +{ + unsigned err; + err = capi20_put_message (appid, msg); + if (err && !exiting) + fatal("putmessage(appid=%d) - %s 0x%x", + appid, capi_info2str(err), err); +} + +/* -------------------------------------------------------------------- */ +/* -------- capiconn module setup ------------------------------------- */ +/* -------------------------------------------------------------------- */ + +capiconn_callbacks callbacks = { + malloc: malloc, + free: free, + + disconnected: disconnected, + incoming: incoming, + connected: connected, + received: handle_capi_data, + datasent: handle_capi_sent, + chargeinfo: chargeinfo, + + capi_put_message: put_message, + + debugmsg: (void (*)(const char *, ...))dbglog, + infomsg: (void (*)(const char *, ...))info, + errmsg: (void (*)(const char *, ...))error +}; + +/* -------------------------------------------------------------------- */ +/* -------- create a connection --------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static capi_connection *setupconnection(int contr, char *num, int awaitingreject) +{ + struct capi_connection *cp; + char number[256]; + + snprintf(number, sizeof(number), "%s%s", + opt_numberprefix ? opt_numberprefix : "", num); + + if (proto == PROTO_HDLC) { + cp = capiconn_connect(ctx, + contr, /* contr */ + 2, /* cipvalue */ + opt_channels ? 0 : number, + opt_channels ? 0 : opt_msn, + 0, 1, 0, + 0, 0, 0, + opt_channels ? AdditionalInfo : 0, + 0); + } else if (proto == PROTO_X75) { + cp = capiconn_connect(ctx, + contr, /* contr */ + 2, /* cipvalue */ + opt_channels ? 0 : number, + opt_channels ? 0 : opt_msn, + 0, 0, 0, + 0, 0, 0, + opt_channels ? AdditionalInfo : 0, + 0); + } else { + fatal("unknown protocol \"%s\"", opt_proto); + return 0; + } + if (opt_channels) { + info("leased line (%s)", opt_proto); + } else if (awaitingreject) { + info("dialing %s (awaiting reject)", number); + } else { + info("dialing %s (%s)", number, opt_proto); + } + if (awaitingreject) + conn_remember(cp, CONNTYPE_FOR_CALLBACK); + else + conn_remember(cp, CONNTYPE_OUTGOING); + return cp; +} + +/* -------------------------------------------------------------------- */ +/* -------- connect leased line --------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void makeleasedline(void) +{ + capi_connection *cp; + int retry = 0; + time_t t; + + reset_contr_status(); + + do { + if (retry) { + t = time(0)+opt_redialdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + } + + cp = setupconnection(get_next_contr(), "", 0); + + t = time(0)+opt_dialtimeout; + do { + handlemessages(); + if (status != EXIT_OK) { + if (conn_find(cp)) { + info("status %d, disconnecting ...", status); + dodisconnect(cp); + } else { + die(status); + } + } + } while (time(0) < t && conn_inprogress(cp)); + + if (conn_isconnected(cp)) + goto connected; + + if (status != EXIT_OK) + die(status); + + } while (++retry < opt_dialmax); + +connected: + if (conn_isconnected(cp)) { + t = time(0)+opt_connectdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + } + + if (status != EXIT_OK) + die(status); + + if (!conn_isconnected(cp)) + fatal("couldn't make connection"); +} + +/* -------------------------------------------------------------------- */ +/* -------- connect a dialup connection ------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void makeconnection(STRINGLIST *numbers) +{ + capi_connection *cp = 0; + time_t t; + STRINGLIST *p; + int retry = 0; + + reset_contr_status(); + + do { + for (p = numbers; p; p = p->next) { + if (retry || p != numbers) { + t = time(0)+opt_redialdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + } + + cp = setupconnection(get_next_contr(), p->s, 0); + + t = time(0)+opt_dialtimeout; + do { + handlemessages(); + if (status != EXIT_OK) { + if (conn_find(cp)) { + info("status %d, disconnecting ...", status); + dodisconnect(cp); + } else { + die(status); + } + } + } while (time(0) < t && conn_inprogress(cp)); + + if (conn_isconnected(cp)) + goto connected; + + if (status != EXIT_OK) + die(status); + } + } while (++retry < opt_dialmax); + +connected: + if (conn_isconnected(cp)) { + t = time(0)+opt_connectdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + } + + if (!conn_isconnected(cp)) + error("couldn't make connection after %d retries", + retry); +} + +/* -------------------------------------------------------------------- */ +/* -------- dial and wait for callback -------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void makeconnection_with_callback(void) +{ + capi_connection *cp; + STRINGLIST *p; + time_t t; + int retry = 0; + + reset_contr_status(); + + do { + for (p = numbers; p; p = p->next) { + + if (retry || p != numbers) { +again: + t = time(0)+opt_redialdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + } + + cp = setupconnection(get_next_contr(), p->s, 1); + + /* Wait specific time for the server rejecting the call */ + t = time(0)+opt_dialtimeout; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t && conn_inprogress(cp)); + + if (conn_inprogress(cp)) { + (void)capiconn_disconnect(cp, 0); + dreason = 0x3490; + } + + if (conn_isconnected(cp)) { + dodisconnect(cp); + error("callback failed - other party answers the call (no reject)"); + } else if (was_no_reject()) { + goto again; + } else { + do_listen(cipmask, 0); + info("waiting for callback..."); + + /* Wait for server calling back */ + t = time(0)+opt_cbwait; + do { + handlemessages(); + if (status != EXIT_OK) { + do_listen(0, 0); + die(status); + } + } while (!conn_incoming_connected() && time(0) < t); + + if (conn_incoming_connected()) { + return; + } + info("callback failed (no call)"); + } + } + } while (++retry < opt_dialmax); + + error("callback failed (no call)"); +} + +/* -------------------------------------------------------------------- */ +/* -------- execute a callback ---------------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void makecallback(void) +{ + time_t t; + + t = time(0)+opt_cbdelay; + do { + handlemessages(); + if (status != EXIT_OK) + die(status); + } while (time(0) < t); + + if (callbacknumbers) + makeconnection(callbacknumbers); + else makeconnection(numbers); +} + +/* -------------------------------------------------------------------- */ +/* -------- wait for an incoming call --------------------------------- */ +/* -------------------------------------------------------------------- */ + +static void waitforcall(void) +{ + do_listen(cipmask, 0); + info("waiting for incoming call ..."); +} + +/* -------------------------------------------------------------------- */ +/* -------- PPPD state change hook ------------------------------------ */ +/* -------------------------------------------------------------------- */ + +static char *phase2str(int phase) +{ + static struct tmpbuf { + struct tmpbuf *next; + char buf[32]; + } buffer[] = { + { &buffer[1] }, + { &buffer[2] }, + { &buffer[0] }, + }; + static struct tmpbuf *p = &buffer[0]; + + switch (phase) { + case PHASE_DEAD: return "dead"; + case PHASE_INITIALIZE: return "initialize"; + case PHASE_SERIALCONN: return "serialconn"; + case PHASE_DORMANT: return "dormant"; + case PHASE_ESTABLISH: return "establish"; + case PHASE_AUTHENTICATE: return "authenticate"; + case PHASE_CALLBACK: return "callback"; + case PHASE_NETWORK: return "network"; + case PHASE_RUNNING: return "running"; + case PHASE_TERMINATE: return "terminate"; + case PHASE_DISCONNECT: return "disconnect"; + case PHASE_HOLDOFF: return "holdoff"; + } + p = p->next; + sprintf(p->buf,"unknown-%d", phase); + return p->buf; +} + +/* -------------------------------------------------------------------- */ + +int capi_new_phase(int phase) +{ + if (phase == curphase) { + info("phase %s, again.", phase2str(phase)); + return 0; + } + if (curphase != -1) { + info("phase %s (was %s).", + phase2str(phase), phase2str(curphase)); + } else { + info("phase %s.", phase2str(phase)); + } + curphase = phase; + switch (phase) { + case PHASE_INITIALIZE: + case PHASE_ESTABLISH: + case PHASE_AUTHENTICATE: + case PHASE_CALLBACK: + case PHASE_NETWORK: + case PHASE_RUNNING: + case PHASE_TERMINATE: + case PHASE_DISCONNECT: + case PHASE_HOLDOFF: + break; + + case PHASE_DEAD: + disconnectall(); + break; + + case PHASE_DORMANT: + status = EXIT_OK; + itun_check_options(); + init_capiconn(); + if (opt_inmsn || opt_cli) { + if (wakeupnow) + wakeupdemand(); + setupincoming_for_demand(); + } + break; + + case PHASE_SERIALCONN: + status = EXIT_OK; + wakeupnow = 0; + if (conn_isconnected(0)) + break; + itun_check_options(); + init_capiconn(); + if (opt_number) { + if (coso == COSO_REMOTE) { + makeconnection_with_callback(); + } else { + makeconnection(numbers); + } + } else if (opt_channels) { + makeleasedline(); + } else { + waitforcall(); + } + break; + } + return 0; +} + +/* -------------------------------------------------------------------- */ +/* -------- init/exit function ---------------------------------------- */ +/* -------------------------------------------------------------------- */ + +int itun_init(void) +{ + int serrno; + int err; + + if ((err = capi20_register (1, 8, 2048, &applid)) != 0) { + serrno = errno; + fatal("CAPI_REGISTER failed - %s (0x%04x) [%s (%d)]", + capi_info2str(err), err, + strerror(serrno), errno); + return(-1); + } + + if ((ctx = capiconn_getcontext(applid, &callbacks)) == 0) { + (void)capi20_release(applid); + fatal("get_context failed"); + return(-1); + } + info("init"); + return(capi20_fileno(applid)); +} + +void itun_exit(void) +{ + exiting = 1; + disconnectall(); + (void)capi20_release(applid); + info("exit"); +} + diff --git a/capiconn.c b/capiconn.c new file mode 100644 index 0000000..f2f5c4a --- /dev/null +++ b/capiconn.c @@ -0,0 +1,1785 @@ +/* + * CAPI connection handling + * + * heavily based on capiconn.* of pppdcapiplugin by + * Carsten Paeth (calle@calle.in-berlin.de) + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#include /* snprintf */ +#include +#include + +#include "isdntun.h" +#include "capiconn.h" + +/* xxxxxxxxxxxxxxxxxx */ +static _cmsg cmdcmsg; +static _cmsg cmsg; + +/* -------- defines -------------------------------------------------- */ + +#ifndef CAPI_MAXDATAWINDOW +#define CAPI_MAXDATAWINDOW 4 +#endif + +/* -------- type definitions ----------------------------------------- */ + +struct capiconn_context { + struct capiconn_context *next; + + unsigned appid; + struct capiconn_callbacks *cb; + + int ncontr; + struct capi_contr *contr_list; + + /* statistic */ + unsigned long nrecvctlpkt; + unsigned long nrecvdatapkt; + unsigned long nsentctlpkt; + unsigned long nsentdatapkt; +}; + +struct capi_contr { + + struct capi_contr *next; + struct capiconn_context *ctx; + + unsigned contrnr; + struct capi_contrinfo cinfo; + unsigned ddilen; + + /* + * LISTEN state + */ + int state; + _cdword infomask; + _cdword cipmask; + _cdword cipmask2; + + /* + * ID of capi message sent + */ + _cword msgid; + + /* + * B-Channels + */ + int nbchan; + struct capi_connection { + struct capi_connection *next; + struct capi_contr *contr; + struct capiconn_context *ctx; + + struct capi_conninfo conninfo; + + unsigned incoming:1, + disconnecting:1, + localdisconnect:1, + callednumbercomplete:1; + + _cword disconnectreason; + _cword disconnectreason_b3; + + _cdword plci; + _cdword ncci; /* ncci for CONNECT_ACTIVE_IND */ + _cword msgid; /* to identfy CONNECT_CONF */ + int state; + struct capi_ncci { + struct capi_connection *plcip; + struct capiconn_context *ctx; + _cdword ncci; + _cword msgid; /* to identfy CONNECT_B3_CONF */ + int state; + int oldstate; + /* */ + _cword datahandle; + struct ncci_datahandle_queue { + struct ncci_datahandle_queue *next; + _cword datahandle; + unsigned char *data; + } *ackqueue; + int ackqueuelen; + } *nccip; + } *connections; +}; + +typedef struct capi_ncci capi_ncci; +typedef struct capi_contr capi_contr; +typedef struct ncci_datahandle_queue ncci_datahandle_queue; + +/* -------- data definitions ----------------------------------------- */ + +capiconn_context *context_list = 0; + +/* -------- context handling ----------------------------------------- */ + +capiconn_context * +capiconn_getcontext(unsigned appid, capiconn_callbacks *cb) +{ + capiconn_context *ctx; + + if ((ctx = ((*cb->malloc)(sizeof(capiconn_context)))) == 0) + return 0; + memset(ctx, 0, sizeof(capiconn_context)); + + ctx->appid = appid; + ctx->cb = cb; + ctx->next = context_list; + context_list = ctx; + return ctx; +}; + +static void free_all_cards(capiconn_context *ctx) +{ +} + +int +capiconn_freecontext(capiconn_context *ctx) +{ + capiconn_context **pp; + for (pp = &context_list; *pp; pp = &(*pp)->next) { + if (*pp == ctx) { + *pp = (*pp)->next; + free_all_cards(ctx); + (*ctx->cb->free)(ctx); + return 0; + } + } + return -1; +} + +static inline capiconn_context *find_context(unsigned appid) +{ + capiconn_context *p; + for (p = context_list; p; p = p->next) + if (p->appid == appid) + return p; + return 0; +} + +int +capiconn_addcontr(capiconn_context *ctx, unsigned contr, capi_contrinfo *cinfo) +{ + capiconn_callbacks *cb = ctx->cb; + capi_contr *card; + + if (!(card = (capi_contr *) (*cb->malloc)(sizeof(capi_contr)))) + return CAPICONN_NO_MEMORY; + memset(card, 0, sizeof(capi_contr)); + card->contrnr = contr; + card->cinfo = *cinfo; + card->ctx = ctx; + if (card->cinfo.ddi) + card->ddilen = strlen(card->cinfo.ddi); + card->next = ctx->contr_list; + ctx->contr_list = card; + ctx->ncontr++; + return CAPICONN_OK; +} + +/* ------------------------------------------------------------------- */ + +static capi_contr *findcontrbynumber(capiconn_context *ctx, unsigned contr) +{ + capi_contr *p; + + for (p = ctx->contr_list; p; p = p->next) + if (p->contrnr == contr) + break; + return p; +} + +/* -------- plci management ------------------------------------------ */ + +static capi_connection *new_plci(capi_contr * card, int incoming) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + capi_connection *plcip; + + plcip = (capi_connection *) (*cb->malloc)(sizeof(capi_connection)); + + if (plcip == 0) + return 0; + + memset(plcip, 0, sizeof(capi_connection)); + plcip->contr = card; + plcip->ctx = ctx; + plcip->incoming = incoming; + plcip->state = ST_PLCI_NONE; + plcip->plci = 0; + plcip->msgid = 0; + plcip->next = card->connections; + card->connections = plcip; + + return plcip; +} + +static capi_connection *find_plci_by_plci(capi_contr * card, _cdword plci) +{ + capi_connection *p; + for (p = card->connections; p; p = p->next) + if (p->plci == plci) + return p; + return 0; +} + +static capi_connection *find_plci_by_msgid(capi_contr * card, _cword msgid) +{ + capi_connection *p; + for (p = card->connections; p; p = p->next) + if (p->msgid == msgid) + return p; + return 0; +} + +static capi_connection *find_plci_by_ncci(capi_contr * card, _cdword ncci) +{ + capi_connection *p; + for (p = card->connections; p; p = p->next) + if (p->plci == (ncci & 0xffff)) + return p; + return 0; +} + +static void free_plci(capi_contr * card, capi_connection * plcip) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + capi_connection **pp; + + for (pp = &card->connections; *pp; pp = &(*pp)->next) { + if (*pp == plcip) { + *pp = (*pp)->next; + (*cb->free)(plcip); + return; + } + } + (*cb->errmsg)("free_plci %p (0x%x) not found, Huh?", + plcip, plcip->plci); +} + +/* -------- ncci management ------------------------------------------ */ + +static inline capi_ncci *new_ncci(capi_contr * card, + capi_connection * plcip, + _cdword ncci) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + capi_ncci *nccip; + + nccip = (capi_ncci *) (*cb->malloc)(sizeof(capi_ncci)); + + if (nccip == 0) + return 0; + + memset(nccip, 0, sizeof(capi_ncci)); + nccip->ctx = ctx; + nccip->ncci = ncci; + nccip->state = ST_NCCI_NONE; + nccip->plcip = plcip; + nccip->datahandle = 0; + plcip->nccip = nccip; + plcip->ncci = ncci; + + return nccip; +} + +static inline capi_ncci *find_ncci(capi_contr * card, _cdword ncci) +{ + capi_connection *plcip; + + if ((plcip = find_plci_by_ncci(card, ncci)) == 0) + return 0; + + return plcip->nccip; +} + +static inline capi_ncci *find_ncci_by_msgid(capi_contr * card, + _cdword ncci, _cword msgid) +{ + capi_connection *plcip; + + if ((plcip = find_plci_by_ncci(card, ncci)) == 0) + return 0; + + return plcip->nccip; +} + +static void free_ncci(capi_contr * card, capi_ncci *nccip) +{ + capiconn_callbacks *cb = card->ctx->cb; + + nccip->plcip->nccip = 0; + (*cb->free)(nccip); +} + +/* ------------------------------------------------------------------- */ + +static void clr_conninfo1(capiconn_context *ctx, capi_conninfo *p) +{ + capiconn_callbacks *cb = ctx->cb; + + if (p->callednumber) { + (*cb->free)(p->callednumber); + p->callednumber = 0; + } + if (p->callingnumber) { + (*cb->free)(p->callingnumber); + p->callingnumber = 0; + } +} + +static void clr_conninfo2(capiconn_context *ctx, capi_conninfo *p) +{ + capiconn_callbacks *cb = ctx->cb; + + if (p->b1config) { + (*cb->free)(p->b1config); + p->b1config = 0; + } + if (p->b2config) { + (*cb->free)(p->b2config); + p->b2config = 0; + } + if (p->b3config) { + (*cb->free)(p->b3config); + p->b3config = 0; + } + if (p->bchaninfo) { + (*cb->free)(p->bchaninfo); + p->bchaninfo = 0; + } + if (p->ncpi) { + (*cb->free)(p->ncpi); + p->ncpi = 0; + } +} + +static void clr_conninfo(capiconn_context *ctx, capi_conninfo *p) +{ + clr_conninfo1(ctx, p); + clr_conninfo2(ctx, p); +} + +static int set_conninfo1a(capiconn_context *ctx, + capi_conninfo *p, + _cword cipvalue, + char *callednumber, + char *callingnumber) +{ + capiconn_callbacks *cb = ctx->cb; + _cbyte len; + + p->cipvalue = cipvalue; + if ((p->callednumber = (*cb->malloc)(128)) == 0) + goto fail; + if (callednumber) { + len = (_cbyte)strlen(callednumber); + if (callednumber[0] & 0x80) { + memcpy(p->callednumber+1, callednumber, len); + p->callednumber[0] = len; + p->callednumber[len+1] = 0; + } else { + memcpy(p->callednumber+2, callednumber, len); + p->callednumber[0] = len+1; + p->callednumber[1] = 0x81; + p->callednumber[len+2] = 0; + } + } else { + p->callednumber[0] = 0; + } + if ((p->callingnumber = (*cb->malloc)(128)) == 0) + goto fail; + if (callingnumber) { + len = (_cbyte)strlen(callingnumber); + memcpy(p->callingnumber+3, callingnumber, len); + p->callingnumber[0] = len+2; + p->callingnumber[1] = 0x00; + p->callingnumber[2] = 0x80; + p->callingnumber[len+3] = 0; + } else { + p->callingnumber[0] = 0; + } + return 0; +fail: + clr_conninfo1(ctx, p); + return -1; +} + +static int set_conninfo1b(capiconn_context *ctx, + capi_conninfo *p, + _cword cipvalue, + _cstruct callednumber, + _cstruct callingnumber) +{ + capiconn_callbacks *cb = ctx->cb; + _cbyte len; + + p->cipvalue = cipvalue; + + if ((p->callednumber = (*cb->malloc)(128)) == 0) + goto fail; + len = callednumber[0]; + memcpy(p->callednumber, callednumber, len+1); + p->callednumber[len+1] = 0; + + if ((p->callingnumber = (*cb->malloc)(128)) == 0) + goto fail; + len = callingnumber[0]; + memcpy(p->callingnumber, callingnumber, len+1); + p->callingnumber[len+1] = 0; + return 0; +fail: + clr_conninfo1(ctx, p); + return -1; +} + +static void extend_callednumber(capiconn_context *ctx, capi_conninfo *p, + char *number, _cbyte len) +{ + capiconn_callbacks *cb = ctx->cb; + _cbyte *curnumber = p->callednumber+2; + _cbyte clen = p->callednumber[0]-2; + + (*cb->debugmsg)("extend number %*.*s (len=%d)", + (int)len, (int)len, number, len); + + if (len >= clen && memcmp(curnumber, number, clen) == 0) { + memcpy(p->callednumber + 2, number, len); + p->callednumber[0] = 2 + len; + } else { + memcpy(p->callednumber + p->callednumber[0], number, len); + p->callednumber[0] += len; + } + p->callednumber[p->callednumber[0]+1] = 0; + (*cb->debugmsg)("extended to %s", p->callednumber+2); +} + +static int set_conninfo2(capiconn_context *ctx, + capi_conninfo *p, + _cword b1proto, _cword b2proto, _cword b3proto, + _cstruct b1config, _cstruct b2config, _cstruct b3config, + _cstruct bchaninfo, _cstruct ncpi) +{ + capiconn_callbacks *cb = ctx->cb; + + p->b1proto = b1proto; + p->b2proto = b2proto; + p->b3proto = b3proto; + if (b1config) { + if ((p->b1config = (*cb->malloc)(b1config[0]+1)) == 0) + goto fail; + memcpy(p->b1config, b1config, b1config[0]+1); + } + if (b2config) { + if ((p->b2config = (*cb->malloc)(b2config[0]+1)) == 0) + goto fail; + memcpy(p->b2config, b2config, b2config[0]+1); + } + if (b3config) { + if ((p->b3config = (*cb->malloc)(b3config[0]+1)) == 0) + goto fail; + memcpy(p->b3config, b3config, b3config[0]+1); + } + if (bchaninfo) { + if ((p->bchaninfo = (*cb->malloc)(bchaninfo[0]+1)) == 0) + goto fail; + memcpy(p->bchaninfo, bchaninfo, bchaninfo[0]+1); + } + if (ncpi) { + if ((p->ncpi = (*cb->malloc)(ncpi[0]+1)) == 0) + goto fail; + memcpy(p->ncpi, ncpi, ncpi[0]+1); + } + return 0; +fail: + clr_conninfo2(ctx, p); + return -1; +} + +capi_conninfo *capiconn_getinfo(capi_connection *p) +{ + p->conninfo.appid = p->ctx->appid; + p->conninfo.plci = p->plci; + p->conninfo.plci_state = p->state; + p->conninfo.ncci = p->ncci; + p->conninfo.ncci_state = p->nccip ? p->nccip->state : ST_NCCI_NONE; + p->conninfo.isincoming = p->incoming ? 1 : 0; + p->conninfo.disconnect_was_local = p->localdisconnect ? 1 : 0; + p->conninfo.disconnectreason = p->disconnectreason; + p->conninfo.disconnectreason_b3 = p->disconnectreason_b3; + return &p->conninfo; +} + +/* ------------------------------------------------------------------- */ + +static int capi_add_ack(capi_ncci *nccip, + _cword datahandle, + unsigned char *data) +{ + capiconn_context *ctx = nccip->ctx; + capiconn_callbacks *cb = ctx->cb; + ncci_datahandle_queue *n, **pp; + + if (nccip->ackqueuelen >= CAPI_MAXDATAWINDOW) + return 0; + n = (ncci_datahandle_queue *) + (*cb->malloc)(sizeof(ncci_datahandle_queue)); + if (!n) { + (cb->errmsg)("capiconn: cb->malloc ncci_datahandle failed"); + return -1; + } + n->next = 0; + n->datahandle = datahandle; + n->data = data; + for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) ; + *pp = n; + nccip->ackqueuelen++; + return 0; +} + +static unsigned char *capi_del_ack(capi_ncci *nccip, _cword datahandle) +{ + capiconn_context *ctx = nccip->ctx; + capiconn_callbacks *cb = ctx->cb; + ncci_datahandle_queue **pp, *p; + unsigned char *data; + + for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) { + if ((*pp)->datahandle == datahandle) { + p = *pp; + data = p->data; + *pp = (*pp)->next; + (*cb->free)(p); + nccip->ackqueuelen--; + return data; + } + } + return 0; +} + +/* -------- convert and send capi message ---------------------------- */ + +static void send_message(capi_contr * card, _cmsg * cmsg) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + + capi_cmsg2message(cmsg, cmsg->buf); + (*cb->capi_put_message) (ctx->appid, cmsg->buf); + ctx->nsentctlpkt++; +} + +/* -------- state machine -------------------------------------------- */ + +struct listenstatechange { + int actstate; + int nextstate; + int event; +}; + +static struct listenstatechange listentable[] = +{ + {ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ}, + {ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ}, + {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR}, + {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR}, + {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY}, + {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY}, + {ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK}, + {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK}, + { 0, 0, 0 }, +}; + +static void listen_change_state(capi_contr * card, int event) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + struct listenstatechange *p = listentable; + + while (p->event) { + if (card->state == p->actstate && p->event == event) { + (*cb->debugmsg)("controller %d: listen_change_state %d -> %d", + card->contrnr, card->state, p->nextstate); + card->state = p->nextstate; + return; + } + p++; + } + (*cb->debugmsg)("controller %d: listen_change_state state=%d event=%d ????", + card->contrnr, card->state, event); +} + +/* ------------------------------------------------------------------ */ + +static void p0(capi_contr * card, capi_connection * plcip) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + if (*cb->disconnected) + (*cb->disconnected)(plcip, + plcip->localdisconnect, + plcip->disconnectreason, + plcip->disconnectreason_b3); + clr_conninfo(ctx, &plcip->conninfo); + free_plci(card, plcip); +} + +/* ------------------------------------------------------------------ */ + +struct plcistatechange { + int actstate; + int nextstate; + int event; + void (*changefunc) (capi_contr * card, capi_connection * plci); +}; + +static struct plcistatechange plcitable[] = +{ + /* P-0 */ + {ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, 0}, + {ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, 0}, + {ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, 0}, + {ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, 0}, + /* P-0.1 */ + {ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0}, + {ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, 0}, + /* P-1 */ + {ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, 0}, + {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0}, + {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0}, + {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0}, + /* P-ACT */ + {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0}, + {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0}, + {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0}, + {ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, 0}, + {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, 0}, + /* P-2 */ + {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, 0}, + {ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, 0}, + {ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, 0}, + {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0}, + {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0}, + {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0}, + {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, 0}, + /* P-3 */ + {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, 0}, + {ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, 0}, + {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0}, + {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0}, + {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0}, + /* P-4 */ + {ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, 0}, + {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, 0}, + {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, 0}, + {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0}, + /* P-5 */ + {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, 0}, + /* P-6 */ + {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0}, + /* P-0.Res */ + {ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0}, + {ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, 0}, + /* P-RES */ + {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, 0}, + /* P-HELD */ + {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, 0}, + { 0, 0, 0, 0 }, +}; + +static void plci_change_state(capi_contr * card, capi_connection * plci, int event) +{ + capiconn_callbacks *cb = card->ctx->cb; + struct plcistatechange *p = plcitable; + while (p->event) { + if (plci->state == p->actstate && p->event == event) { + (cb->debugmsg)("plci_change_state:0x%x %d -> %d event=%d", + plci->plci, plci->state, p->nextstate, event); + plci->state = p->nextstate; + if (p->changefunc) + p->changefunc(card, plci); + return; + } + p++; + } + (*cb->errmsg)("plci_change_state:0x%x state=%d event=%d ????", + card->contrnr, plci->plci, plci->state, event); +} + +/* ------------------------------------------------------------------ */ + + +static void n0(capi_contr * card, capi_ncci * ncci) +{ + capiconn_context *ctx = card->ctx; + + capi_fill_DISCONNECT_REQ(&cmsg, + ctx->appid, + card->msgid++, + ncci->plcip->plci, + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ /* $$$$ */ + 0 /* Facilitydataarray */ + ); + send_message(card, &cmsg); + plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ); + free_ncci(card, ncci); +} + +/* ------------------------------------------------------------------ */ + +struct nccistatechange { + int actstate; + int nextstate; + int event; + void (*changefunc) (capi_contr * card, capi_ncci * ncci); +}; + +static struct nccistatechange nccitable[] = +{ + /* N-0 */ + {ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, 0}, + {ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, 0}, + /* N-0.1 */ + {ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, 0}, + {ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0}, + /* N-1 */ + {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, 0}, + {ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, 0}, + {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0}, + {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0}, + /* N-2 */ + {ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, 0}, + {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0}, + {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0}, + /* N-ACT */ + {ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, 0}, + {ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, 0}, + {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0}, + {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0}, + /* N-3 */ + {ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, 0}, + {ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0}, + {ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, 0}, + /* N-4 */ + {ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, 0}, + {ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR,0}, + /* N-5 */ + {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0}, + { 0, 0, 0, 0 }, +}; + +static void ncci_change_state(capi_contr * card, capi_ncci * ncci, int event) +{ + capiconn_callbacks *cb = card->ctx->cb; + struct nccistatechange *p = nccitable; + while (p->event) { + if (ncci->state == p->actstate && p->event == event) { + (*cb->debugmsg)("ncci_change_state:0x%x %d -> %d event=%d", + ncci->ncci, ncci->state, p->nextstate, event); + if (p->nextstate == ST_NCCI_PREVIOUS) { + ncci->state = ncci->oldstate; + ncci->oldstate = p->actstate; + } else { + ncci->oldstate = p->actstate; + ncci->state = p->nextstate; + } + if (p->changefunc) + p->changefunc(card, ncci); + return; + } + p++; + } + (*cb->errmsg)("ncci_change_state:0x%x state=%d event=%d ????", + ncci->ncci, ncci->state, event); +} + +/* ------------------------------------------------------------------- */ + +static void handle_controller(capiconn_context *ctx, _cmsg * cmsg) +{ + capi_contr *card = findcontrbynumber(ctx, cmsg->adr.adrController&0x7f); + capiconn_callbacks *cb = ctx->cb; + + if (!card) { + (*cb->errmsg)("capiconn: %s from unknown controller 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrController & 0x7f); + return; + } + switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) { + + case CAPI_LISTEN_CONF: /* Controller */ + (*cb->debugmsg)("contr %d: listenconf Info=0x%04x (%s) infomask=0x%x cipmask=0x%x capimask2=0x%x", + card->contrnr, cmsg->Info, + capi_info2str(cmsg->Info), + card->infomask, + card->cipmask, + card->cipmask2); + if (cmsg->Info) { + listen_change_state(card, EV_LISTEN_CONF_ERROR); + } else if (card->cipmask == 0) { + listen_change_state(card, EV_LISTEN_CONF_EMPTY); + } else { + listen_change_state(card, EV_LISTEN_CONF_OK); + } + break; + + case CAPI_MANUFACTURER_IND: /* Controller */ + goto ignored; + case CAPI_MANUFACTURER_CONF: /* Controller */ + goto ignored; + case CAPI_FACILITY_IND: /* Controller/plci/ncci */ + goto ignored; + case CAPI_FACILITY_CONF: /* Controller/plci/ncci */ + goto ignored; + case CAPI_INFO_IND: /* Controller/plci */ + goto ignored; + case CAPI_INFO_CONF: /* Controller/plci */ + goto ignored; + + default: + (*cb->errmsg)("got %s from controller 0x%x ???", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrController); + } + return; + + ignored: + (*cb->infomsg)("%s from controller 0x%x ignored", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrController); +} + +static void check_incoming_complete(capi_connection *plcip) +{ + capi_contr *card = plcip->contr; + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + capi_contrinfo *cip = &card->cinfo; + unsigned ddilen = plcip->contr->ddilen; + + if (ddilen) { + unsigned len = plcip->conninfo.callednumber[0]-2; + char *num = plcip->conninfo.callednumber+2; + char *start; + int ndigits; + + if ((start = strstr(num, cip->ddi)) != 0) + len = strlen(start); + ndigits = len - ddilen; + if (ndigits < cip->ndigits) { + (*cb->debugmsg)("%d digits missing (%s)", + cip->ndigits-ndigits, num); + return; + } + } + + if (plcip->callednumbercomplete) + return; + + plcip->callednumbercomplete = 1; + + if (*cb->incoming) + (*cb->incoming)(plcip, + plcip->contr->contrnr, + plcip->conninfo.cipvalue, + plcip->conninfo.callednumber+2, + plcip->conninfo.callingnumber+3); + + if (plcip->state == ST_PLCI_INCOMING) { + /* call not accepted, rejected or ignored */ + capi_fill_ALERT_REQ(&cmsg, + ctx->appid, + card->msgid++, + plcip->plci, /* adr */ + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + plcip->msgid = cmsg.Messagenumber; + send_message(card, &cmsg); + } +} + +static void handle_incoming_call(capi_contr * card, _cmsg * cmsg) +{ + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + capi_connection *plcip; + + if ((plcip = new_plci(card, 1)) == 0) { + (*cb->errmsg)("incoming call on contr %d: no memory, sorry.", card->contrnr); + goto ignore; + } + plcip->plci = cmsg->adr.adrPLCI; + if (set_conninfo1b(ctx, &plcip->conninfo, + cmsg->CIPValue, + cmsg->CalledPartyNumber, + cmsg->CallingPartyNumber) < 0) { + free_plci(card, plcip); + goto ignore; + } + plci_change_state(card, plcip, EV_PLCI_CONNECT_IND); + + (*cb->debugmsg)("incoming call contr=%d cip=%d %s -> %s", + card->contrnr, + cmsg->CIPValue, + plcip->conninfo.callingnumber + 3, + plcip->conninfo.callednumber + 2); + + if (cb->incoming == 0) + goto ignore; + + check_incoming_complete(plcip); + + return; + +ignore: + capi_fill_CONNECT_RESP(&cmdcmsg, + ctx->appid, + card->msgid++, + cmsg->adr.adrPLCI, + 1, /* ignore call */ + 0, + 0, + 0, + 0, + 0, + 0, + 0, /* ConnectedNumber */ + 0, /* ConnectedSubaddress */ + 0, /* LLC */ + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + capi_cmsg2message(&cmdcmsg, cmdcmsg.buf); + send_message(card, &cmdcmsg); +} + +static int handle_charge_info(capi_connection *plcip, _cmsg *cmsg) +{ + capiconn_context *ctx = plcip->ctx; + capiconn_callbacks *cb = ctx->cb; + unsigned char *p = cmsg->InfoElement; + unsigned long charge = 0; + + if ((cmsg->InfoNumber & 0x4000) && p[0] == 4) { + unsigned char *p = &cmsg->InfoElement[1]; + charge |= ((unsigned)p[0]); + charge |= ((unsigned)p[1]) << 8; + charge |= ((unsigned)p[2]) << 16; + charge |= ((unsigned)p[3]) << 24; + if (cb->chargeinfo) { + if (cmsg->InfoNumber & 0x1) + (*cb->chargeinfo)(plcip, charge, 0); + else (*cb->chargeinfo)(plcip, charge, 1); + } + return 1; + } else if (cmsg->InfoNumber == 0x28) { + if (p[0] > 10 && memcmp("*AOC2*12*", p+1, 9) == 0) { + int i, len = p[0]-10; + if (len > 8) len = 8; + for (i=0; i < len; i++) + charge = charge * 10 + (p[10+i] - '0'); + if (cb->chargeinfo) + (*cb->chargeinfo)(plcip, charge, 0); + return 1; + } else if (p[0] > 7 && memcmp("FR.", p+1, 3) == 0) { + int i, len = p[0]-3; + for (i=0; p[3+i] != '.' && i < len; i++) + charge = charge * 10 + (p[3+i] - '0'); + charge = charge * 10; + if (p[3+i] == '.' && i+1 < len) + charge += (p[3+i+1] - '0'); + if (cb->chargeinfo) + (*cb->chargeinfo)(plcip, charge, 0); + return 1; + } + } else if (cmsg->InfoNumber == 0x602) { + if (p[0] > 1 && p[1] == 0x01) { + int i, len = p[0]-1; + for (i=0; i < len; i++) + charge = charge * 10 + (p[1+i] - '0'); + if (cb->chargeinfo) + (*cb->chargeinfo)(plcip, charge, 0); + return 1; + } + } + return 0; +} + +static int handle_callednumber_info(capi_connection *plcip, _cmsg *cmsg) +{ + unsigned char *p = cmsg->InfoElement; + if (cmsg->InfoNumber == 0x0070) { + extend_callednumber(plcip->ctx, &plcip->conninfo, p+2, p[0]-1); + check_incoming_complete(plcip); + return 1; + } + return 0; +} + +static int handle_cause_info(capi_connection *plcip, _cmsg *cmsg) +{ + capiconn_context *ctx = plcip->ctx; + capiconn_callbacks *cb = ctx->cb; + + unsigned char *p = cmsg->InfoElement; + if (cmsg->InfoNumber == 0x0008) { + char buf[128]; + char *s, *end; + int i; + s = buf; end = s + sizeof(buf)-1; + *end = 0; + for (i=0; i < p[0]; i++) { + snprintf(s, end-s, " %02x", p[i+1]); + s += strlen(s); + } + (*cb->debugmsg)("cause bytes for plci 0x%x:%s", cmsg->adr.adrPLCI, buf); + return 1; + } + return 0; +} + +static void handle_plci(capiconn_context *ctx, _cmsg * cmsg) +{ + capi_contr *card = findcontrbynumber(ctx, cmsg->adr.adrController&0x7f); + capiconn_callbacks *cb = ctx->cb; + capi_connection *plcip; + + if (!card) { + (*cb->errmsg)("capiconn: %s from unknown controller 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrController & 0x7f); + return; + } + switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) { + + case CAPI_DISCONNECT_IND: /* plci */ + if (cmsg->Reason) { + (*cb->debugmsg)("%s reason 0x%x (%s) for plci 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI); + } + if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) { + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + goto notfound; + } + plcip->disconnectreason = cmsg->Reason; + plcip->disconnecting = 1; + plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND); + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP); + break; + + case CAPI_DISCONNECT_CONF: /* plci */ + if (cmsg->Info) { + (*cb->infomsg)("%s info 0x%x (%s) for plci 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->Info, capi_info2str(cmsg->Info), + cmsg->adr.adrPLCI); + } + if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) + goto notfound; + + plcip->disconnecting = 1; + break; + + case CAPI_ALERT_CONF: /* plci */ + if (cmsg->Info) { + (*cb->infomsg)("%s info 0x%x (%s) for plci 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->Info, capi_info2str(cmsg->Info), + cmsg->adr.adrPLCI); + } + break; + + case CAPI_CONNECT_IND: /* plci */ + handle_incoming_call(card, cmsg); + break; + + case CAPI_CONNECT_CONF: /* plci */ + if (cmsg->Info) { + (*cb->infomsg)("%s info 0x%x (%s) for plci 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->Info, capi_info2str(cmsg->Info), + cmsg->adr.adrPLCI); + } + if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber))) + goto notfound; + + plcip->plci = cmsg->adr.adrPLCI; + if (cmsg->Info) { + plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR); + } else { + plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK); + } + break; + + case CAPI_CONNECT_ACTIVE_IND: /* plci */ + + if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) + goto notfound; + + if (plcip->incoming) { + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND); + } else { + capi_ncci *nccip; + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + + nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI); + + if (!nccip) { + (*cb->errmsg)("no mem for ncci on contr %d, sorry", card->contrnr); + break; /* $$$$ */ + } + capi_fill_CONNECT_B3_REQ(cmsg, + ctx->appid, + card->msgid++, + plcip->plci, /* adr */ + plcip->conninfo.ncpi); + nccip->msgid = cmsg->Messagenumber; + send_message(card, cmsg); + plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND); + ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ); + } + break; + + case CAPI_INFO_IND: /* Controller/plci */ + + if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) + goto notfound; + + if (handle_charge_info(plcip, cmsg)) { + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + } else if (handle_callednumber_info(plcip, cmsg)) { + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + } else if (handle_cause_info(plcip, cmsg)) { + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + } else { + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + } + break; + + case CAPI_CONNECT_ACTIVE_CONF: /* plci */ + goto ignored; + case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */ + goto ignored; + case CAPI_FACILITY_IND: /* Controller/plci/ncci */ + goto ignored; + case CAPI_FACILITY_CONF: /* Controller/plci/ncci */ + goto ignored; + case CAPI_INFO_CONF: /* Controller/plci */ + goto ignored; + + default: + (*cb->errmsg)("got %s for plci 0x%x ???", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrPLCI); + } + return; + +ignored: + (*cb->infomsg)("%s for plci 0x%x ignored", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrPLCI); + return; + +notfound: + (*cb->debugmsg)("%s: plci 0x%x not found", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrPLCI); + return; +} + +static void handle_ncci(capiconn_context *ctx, _cmsg * cmsg) +{ + capi_contr *card = findcontrbynumber(ctx, cmsg->adr.adrController&0x7f); + capiconn_callbacks *cb = ctx->cb; + capi_connection *plcip; + capi_ncci *nccip; + unsigned char *data; + + if (!card) { + (*cb->errmsg)("capidrv: %s from unknown controller 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrController & 0x7f); + return; + } + switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) { + + case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */ + if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) + goto notfound; + + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND); + + + (*cb->debugmsg)("ncci 0x%x up", nccip->ncci); + + (*cb->connected)(nccip->plcip, cmsg->NCPI); + break; + + case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */ + goto ignored; + + case CAPI_CONNECT_B3_IND: /* ncci */ + + plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI); + if (plcip) { + nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI); + if (nccip) { + ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND); + capi_fill_CONNECT_B3_RESP(cmsg, + ctx->appid, + card->msgid++, + nccip->ncci, /* adr */ + 0, /* Reject */ + 0 /* NCPI */ + ); + send_message(card, cmsg); + ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP); + break; + } + (*cb->errmsg)("capidrv-%d: no mem for ncci, sorry", card->contrnr); + } else { + (*cb->errmsg)("capidrv-%d: %s: plci for ncci 0x%x not found", + card->contrnr, + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrNCCI); + } + capi_fill_CONNECT_B3_RESP(cmsg, + ctx->appid, + card->msgid++, + cmsg->adr.adrNCCI, + 2, /* Reject */ + plcip->conninfo.ncpi); + send_message(card, cmsg); + break; + + case CAPI_CONNECT_B3_CONF: /* ncci */ + + if (!(nccip = find_ncci_by_msgid(card, + cmsg->adr.adrNCCI, + cmsg->Messagenumber))) + goto notfound; + + nccip->ncci = cmsg->adr.adrNCCI; + nccip->plcip->ncci = cmsg->adr.adrNCCI; + if (cmsg->Info) { + (*cb->infomsg)("%s info 0x%x (%s) for ncci 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->Info, capi_info2str(cmsg->Info), + cmsg->adr.adrNCCI); + } + + if (cmsg->Info) + ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR); + else + ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK); + break; + + case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */ + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + break; + + case CAPI_DATA_B3_IND: /* ncci */ + /* handled in handle_data() */ + goto ignored; + + case CAPI_DATA_B3_CONF: /* ncci */ + if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) + goto notfound; + + data = capi_del_ack(nccip, cmsg->DataHandle); + if (data == 0) + break; + if (cb->datasent) + (*cb->datasent)(nccip->plcip, data); + break; + + case CAPI_DISCONNECT_B3_IND: /* ncci */ + if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) + goto notfound; + + nccip->plcip->disconnectreason_b3 = cmsg->Reason_B3; + nccip->plcip->disconnecting = 1; + ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND); + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP); + break; + + case CAPI_DISCONNECT_B3_CONF: /* ncci */ + if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) + goto notfound; + if (cmsg->Info) { + (*cb->infomsg)("%s info 0x%x (%s) for ncci 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->Info, capi_info2str(cmsg->Info), + cmsg->adr.adrNCCI); + ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR); + } + break; + + case CAPI_RESET_B3_IND: /* ncci */ + if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) + goto notfound; + ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND); + capi_cmsg_answer(cmsg); + send_message(card, cmsg); + break; + + case CAPI_RESET_B3_CONF: /* ncci */ + goto ignored; /* $$$$ */ + + case CAPI_FACILITY_IND: /* Controller/plci/ncci */ + goto ignored; + case CAPI_FACILITY_CONF: /* Controller/plci/ncci */ + goto ignored; + + default: + (*cb->errmsg)("%d: got %s for ncci 0x%x ???", + card->contrnr, + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrNCCI); + } + return; +ignored: + (*cb->infomsg)("%s for ncci 0x%x ignored", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrNCCI); + return; +notfound: + (*cb->errmsg)("%s: ncci 0x%x not found", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrNCCI); +} + + +static void handle_data(capiconn_context *ctx, _cmsg * cmsg) +{ + capi_contr *card = findcontrbynumber(ctx,cmsg->adr.adrController&0x7f); + capiconn_callbacks *cb = ctx->cb; + capi_ncci *nccip; + unsigned char *data; + + if (!card) { + (*cb->errmsg)("%s from unknown controller 0x%x", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrController & 0x7f); + return; + } + if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) { + (*cb->errmsg)("%s: ncci 0x%x not found", + capi_cmd2str(cmsg->Command, cmsg->Subcommand), + cmsg->adr.adrNCCI); + return; + } + data = (unsigned char *)cmsg->Data; + if (cb->received) + (*cb->received)(nccip->plcip, data, cmsg->DataLength); + capi_cmsg_answer(cmsg); + send_message(card, cmsg); +} + +static _cmsg s_cmsg; + +void capiconn_inject(unsigned applid, unsigned char *msg) +{ + capiconn_context *ctx = find_context(applid); + + if (!ctx) + return; + + capi_message2cmsg(&s_cmsg, msg); + if (s_cmsg.Command == CAPI_DATA_B3 && s_cmsg.Subcommand == CAPI_IND) { + handle_data(ctx, &s_cmsg); + ctx->nrecvdatapkt++; + return; + } + if ((s_cmsg.adr.adrController & 0xffffff00) == 0) + handle_controller(ctx, &s_cmsg); + else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0) + handle_plci(ctx, &s_cmsg); + else + handle_ncci(ctx, &s_cmsg); + ctx->nrecvctlpkt++; +} + +/* ------------------------------------------------------------------- */ + +capi_connection *capiconn_connect( + capiconn_context *ctx, + unsigned contr, + _cword cipvalue, + char *callednumber, /* remote number */ + char *callingnumber, /* own number */ + _cword b1proto, + _cword b2proto, + _cword b3proto, + _cstruct b1config, + _cstruct b2config, + _cstruct b3config, + _cstruct bchaninfo, + _cstruct ncpi) +{ + capi_contr *card = findcontrbynumber(ctx, contr); + capiconn_callbacks *cb = ctx->cb; + capi_connection *plcip; + + if (!card) { + (*cb->errmsg)("controller %d not found", contr); + return 0; + } + + if ((plcip = new_plci(card, 0)) == 0) { + (*cb->errmsg)("no mem for plci"); + return 0; + } + + if (set_conninfo1a(ctx, &plcip->conninfo, + cipvalue, + callednumber, + callingnumber) < 0) { + clr_conninfo1(ctx, &plcip->conninfo); + free_plci(card, plcip); + (*cb->errmsg)("no mem for connection info (1a)"); + return 0; + } + + if (set_conninfo2(ctx, &plcip->conninfo, + b1proto, b2proto, b3proto, + b1config, b2config, b3config, + bchaninfo, ncpi) < 0) { + clr_conninfo1(ctx, &plcip->conninfo); + clr_conninfo2(ctx, &plcip->conninfo); + free_plci(card, plcip); + (*cb->errmsg)("no mem for connection info (2)"); + return 0; + } + + capi_fill_CONNECT_REQ(&cmdcmsg, + ctx->appid, + card->msgid++, + card->contrnr, /* adr */ + plcip->conninfo.cipvalue, + plcip->conninfo.callednumber, + plcip->conninfo.callingnumber, + 0, /* CalledPartySubaddress */ + 0, /* CallingPartySubaddress */ + plcip->conninfo.b1proto, + plcip->conninfo.b2proto, + plcip->conninfo.b3proto, + plcip->conninfo.b1config, + plcip->conninfo.b2config, + plcip->conninfo.b3config, + 0, /* BC */ + 0, /* LLC */ + 0, /* HLC */ + plcip->conninfo.bchaninfo, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + + plcip->msgid = cmdcmsg.Messagenumber; + plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ); + send_message(card, &cmdcmsg); + return plcip; +} + +int capiconn_accept( + capi_connection *plcip, + _cword b1proto, + _cword b2proto, + _cword b3proto, + _cstruct b1config, + _cstruct b2config, + _cstruct b3config, + _cstruct ncpi) +{ + capi_contr *card = plcip->contr; + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + + if (plcip->state != ST_PLCI_INCOMING) + return CAPICONN_WRONG_STATE; + + if (set_conninfo2(ctx, &plcip->conninfo, + b1proto, b2proto, b3proto, + b1config, b2config, b3config, + 0, ncpi) < 0) { + clr_conninfo2(ctx, &plcip->conninfo); + (*cb->errmsg)("no mem for connection info (2)"); + return CAPICONN_NO_MEMORY; + } + + (*cb->debugmsg)("accept plci 0x%04x %d,%d,%d", + plcip->plci, + (int)plcip->conninfo.b1proto, + (int)plcip->conninfo.b2proto, + (int)plcip->conninfo.b3proto); + + capi_fill_CONNECT_RESP(&cmdcmsg, + ctx->appid, + card->msgid++, + plcip->plci, /* adr */ + 0, /* Reject */ + plcip->conninfo.b1proto, + plcip->conninfo.b2proto, + plcip->conninfo.b3proto, + plcip->conninfo.b1config, + plcip->conninfo.b2config, + plcip->conninfo.b3config, + 0, /* ConnectedNumber */ + 0, /* ConnectedSubaddress */ + 0, /* LLC */ + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + capi_cmsg2message(&cmdcmsg, cmdcmsg.buf); + plci_change_state(card, plcip, EV_PLCI_CONNECT_RESP); + send_message(card, &cmdcmsg); + return CAPICONN_OK; +} + +int capiconn_ignore(capi_connection *plcip) +{ + capi_contr *card = plcip->contr; + capiconn_context *ctx = card->ctx; + + if (plcip->state != ST_PLCI_INCOMING) + return CAPICONN_WRONG_STATE; + + capi_fill_CONNECT_RESP(&cmdcmsg, + ctx->appid, + card->msgid++, + plcip->plci, /* adr */ + 1, /* ignore call */ + 0, + 0, + 0, + 0, + 0, + 0, + 0, /* ConnectedNumber */ + 0, /* ConnectedSubaddress */ + 0, /* LLC */ + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + capi_cmsg2message(&cmdcmsg, cmdcmsg.buf); + plci_change_state(card, plcip, EV_PLCI_CONNECT_RESP); + send_message(card, &cmdcmsg); + return CAPICONN_OK; +} + +int capiconn_reject(capi_connection *plcip) +{ + capi_contr *card = plcip->contr; + capiconn_context *ctx = card->ctx; + + if (plcip->state != ST_PLCI_INCOMING) + return CAPICONN_WRONG_STATE; + + capi_fill_CONNECT_RESP(&cmdcmsg, + ctx->appid, + card->msgid++, + plcip->plci, /* adr */ + 2, /* Reject, normal call clearing */ + 0, + 0, + 0, + 0, + 0, + 0, + 0, /* ConnectedNumber */ + 0, /* ConnectedSubaddress */ + 0, /* LLC */ + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + capi_cmsg2message(&cmdcmsg, cmdcmsg.buf); + plci_change_state(card, plcip, EV_PLCI_CONNECT_RESP); + send_message(card, &cmdcmsg); + return CAPICONN_OK; +} + +int capiconn_disconnect(capi_connection *plcip, _cstruct ncpi) +{ + capi_contr *card = plcip->contr; + capiconn_context *ctx = card->ctx; + + if (plcip->disconnecting) + return CAPICONN_ALREADY_DISCONNECTING; + + if (plcip->nccip) { + plcip->disconnecting = 1; + plcip->localdisconnect = 1; + capi_fill_DISCONNECT_B3_REQ(&cmdcmsg, + ctx->appid, + card->msgid++, + plcip->ncci, + ncpi); + ncci_change_state(card, plcip->nccip, EV_NCCI_DISCONNECT_B3_REQ); + send_message(card, &cmdcmsg); + return CAPICONN_OK; + } + if (plcip->state == ST_PLCI_INCOMING) { + plcip->disconnecting = 1; + plcip->localdisconnect = 1; + return capiconn_reject(plcip); + } + if (plcip->plci) { + plcip->disconnecting = 1; + plcip->localdisconnect = 1; + capi_fill_DISCONNECT_REQ(&cmdcmsg, + ctx->appid, + card->msgid++, + plcip->plci, + 0, /* BChannelinformation */ + 0, /* Keypadfacility */ + 0, /* Useruserdata */ + 0 /* Facilitydataarray */ + ); + plci_change_state(card, plcip, EV_PLCI_DISCONNECT_REQ); + send_message(card, &cmdcmsg); + return CAPICONN_OK; + } + return CAPICONN_WRONG_STATE; +} + +static _cmsg sendcmsg; + +int capiconn_send(capi_connection *plcip, + unsigned char *data, + unsigned len) +{ + capi_contr *card = plcip->contr; + capiconn_context *ctx = card->ctx; + capiconn_callbacks *cb = ctx->cb; + + capi_ncci *nccip; + _cword datahandle; + + nccip = plcip->nccip; + if (!nccip || nccip->state != ST_NCCI_ACTIVE) + return CAPICONN_WRONG_STATE; + + datahandle = nccip->datahandle; + capi_fill_DATA_B3_REQ(&sendcmsg, ctx->appid, card->msgid++, + nccip->ncci, /* adr */ + data, /* Data */ + len, /* DataLength */ + datahandle, /* DataHandle */ + 0 /* Flags */ + ); + + if (capi_add_ack(nccip, datahandle, data) < 0) + return CAPICONN_NOT_SENT; + + capi_cmsg2message(&sendcmsg, sendcmsg.buf); + (*cb->capi_put_message) (ctx->appid, sendcmsg.buf); + nccip->datahandle++; + ctx->nsentdatapkt++; + return CAPICONN_OK; +} + + +/* -------- listen handling ------------------------------------------ */ + +static void send_listen(capi_contr *card) +{ + capiconn_context *ctx = card->ctx; + + card->infomask = 0; + card->infomask |= (1<<0); /* cause information */ + card->infomask |= (1<<2); /* Display */ + card->infomask |= (1<<6); /* Charge Info */ + if (card->ddilen) card->infomask |= (1<<7); /* Called Party Number */ + card->infomask |= (1<<8); /* Channel Info */ + + capi_fill_LISTEN_REQ(&cmdcmsg, ctx->appid, + card->msgid++, + card->contrnr, + card->infomask, + card->cipmask, + card->cipmask2, + 0, 0); + send_message(card, &cmdcmsg); + listen_change_state(card, EV_LISTEN_REQ); +} + +int +capiconn_listen(capiconn_context *ctx, + unsigned contr, unsigned cipmask, unsigned cipmask2) +{ + capi_contr *card = findcontrbynumber(ctx, contr & 0x7f); + + if (card == 0) + return CAPICONN_NO_CONTROLLER; + + card->cipmask = cipmask; /* 0x1FFF03FF */ + card->cipmask2 = cipmask2; /* 0 */ + + send_listen(card); + return CAPICONN_OK; +} + +int +capiconn_listenstate(capiconn_context *ctx, unsigned contr) +{ + capi_contr *card = findcontrbynumber(ctx, contr & 0x7f); + + if (card == 0) + return CAPICONN_NO_CONTROLLER; + if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE) + return CAPICONN_WRONG_STATE; + return CAPICONN_OK; +} diff --git a/capiconn.h b/capiconn.h new file mode 100644 index 0000000..b257a83 --- /dev/null +++ b/capiconn.h @@ -0,0 +1,435 @@ +/* + * CAPI connection handling + * + * heavily based on capiconn.* of pppdcapiplugin by + * Carsten Paeth (calle@calle.in-berlin.de) + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#ifndef __CAPICONN_H__ +#define __CAPICONN_H__ + +#include +#include + +/* + * CAPI_MESSAGES: + * capiconn_inject(): + * inject capi message into state machine + * + * capiconn_context: + * capiconn_getcontext(): + * get a context and supply callback functions + * capiconn_freecontext(): + * free a context allocated with "capiconn_getcontext" + * capiconn_addcontr(): + * add a controller to the context + * capi_connection: + * capiconn_connect(): + * connection setup, return a capi_connection. + * capiconn_accept(): + * accept an incoming connection + * capiconn_ignore(): + * ignore an incoming connection + * capiconn_reject(): + * reject an incoming connection + * capiconn_send(): + * send data to the connection + * capiconn_disconnect(): + * disconnect a connection + * capiconn_getinfo() + * get infos about the connection + * capiconn_listen(): + * setup listen + */ + +/* -------- returncodes -------------------------------------------------- */ + +#define CAPICONN_OK 0 +#define CAPICONN_NO_CONTROLLER -1 +#define CAPICONN_NO_MEMORY -2 +#define CAPICONN_WRONG_STATE 1 +#define CAPICONN_NOT_SENT 2 +#define CAPICONN_ALREADY_DISCONNECTING 3 + +/* -------- states for CAPI2.0 machine ----------------------------------- */ + +/* + * LISTEN state machine + */ +#define ST_LISTEN_NONE 0 /* L-0 */ +#define ST_LISTEN_WAIT_CONF 1 /* L-0.1 */ +#define ST_LISTEN_ACTIVE 2 /* L-1 */ +#define ST_LISTEN_ACTIVE_WAIT_CONF 3 /* L-1.1 */ + + +#define EV_LISTEN_REQ 1 /* L-0 -> L-0.1 + L-1 -> L-1.1 */ +#define EV_LISTEN_CONF_ERROR 2 /* L-0.1 -> L-0 + L-1.1 -> L-1 */ +#define EV_LISTEN_CONF_EMPTY 3 /* L-0.1 -> L-0 + L-1.1 -> L-0 */ +#define EV_LISTEN_CONF_OK 4 /* L-0.1 -> L-1 + L-1.1 -> L.1 */ + +/* + * per plci state machine + */ +#define ST_PLCI_NONE 0 /* P-0 */ +#define ST_PLCI_OUTGOING 1 /* P-0.1 */ +#define ST_PLCI_ALLOCATED 2 /* P-1 */ +#define ST_PLCI_ACTIVE 3 /* P-ACT */ +#define ST_PLCI_INCOMING 4 /* P-2 */ +#define ST_PLCI_FACILITY_IND 5 /* P-3 */ +#define ST_PLCI_ACCEPTING 6 /* P-4 */ +#define ST_PLCI_DISCONNECTING 7 /* P-5 */ +#define ST_PLCI_DISCONNECTED 8 /* P-6 */ +#define ST_PLCI_RESUMEING 9 /* P-0.Res */ +#define ST_PLCI_RESUME 10 /* P-Res */ +#define ST_PLCI_HELD 11 /* P-HELD */ + +#define EV_PLCI_CONNECT_REQ 1 /* P-0 -> P-0.1 + */ +#define EV_PLCI_CONNECT_CONF_ERROR 2 /* P-0.1 -> P-0 + */ +#define EV_PLCI_CONNECT_CONF_OK 3 /* P-0.1 -> P-1 + */ +#define EV_PLCI_FACILITY_IND_UP 4 /* P-0 -> P-1 + */ +#define EV_PLCI_CONNECT_IND 5 /* P-0 -> P-2 + */ +#define EV_PLCI_CONNECT_ACTIVE_IND 6 /* P-1 -> P-ACT + */ +#define EV_PLCI_CONNECT_REJECT 7 /* P-2 -> P-5 + P-3 -> P-5 + */ +#define EV_PLCI_DISCONNECT_REQ 8 /* P-1 -> P-5 + P-2 -> P-5 + P-3 -> P-5 + P-4 -> P-5 + P-ACT -> P-5 + P-Res -> P-5 (*) + P-HELD -> P-5 (*) + */ +#define EV_PLCI_DISCONNECT_IND 9 /* P-1 -> P-6 + P-2 -> P-6 + P-3 -> P-6 + P-4 -> P-6 + P-5 -> P-6 + P-ACT -> P-6 + P-Res -> P-6 (*) + P-HELD -> P-6 (*) + */ +#define EV_PLCI_FACILITY_IND_DOWN 10 /* P-0.1 -> P-5 + P-1 -> P-5 + P-ACT -> P-5 + P-2 -> P-5 + P-3 -> P-5 + P-4 -> P-5 + */ +#define EV_PLCI_DISCONNECT_RESP 11 /* P-6 -> P-0 + */ +#define EV_PLCI_CONNECT_RESP 12 /* P-6 -> P-0 + */ + +#define EV_PLCI_RESUME_REQ 13 /* P-0 -> P-0.Res + */ +#define EV_PLCI_RESUME_CONF_OK 14 /* P-0.Res -> P-Res + */ +#define EV_PLCI_RESUME_CONF_ERROR 15 /* P-0.Res -> P-0 + */ +#define EV_PLCI_RESUME_IND 16 /* P-Res -> P-ACT + */ +#define EV_PLCI_HOLD_IND 17 /* P-ACT -> P-HELD + */ +#define EV_PLCI_RETRIEVE_IND 18 /* P-HELD -> P-ACT + */ +#define EV_PLCI_SUSPEND_IND 19 /* P-ACT -> P-5 + */ +#define EV_PLCI_CD_IND 20 /* P-2 -> P-5 + */ + +/* + * per ncci state machine + */ +#define ST_NCCI_PREVIOUS -1 +#define ST_NCCI_NONE 0 /* N-0 */ +#define ST_NCCI_OUTGOING 1 /* N-0.1 */ +#define ST_NCCI_INCOMING 2 /* N-1 */ +#define ST_NCCI_ALLOCATED 3 /* N-2 */ +#define ST_NCCI_ACTIVE 4 /* N-ACT */ +#define ST_NCCI_RESETING 5 /* N-3 */ +#define ST_NCCI_DISCONNECTING 6 /* N-4 */ +#define ST_NCCI_DISCONNECTED 7 /* N-5 */ + +#define EV_NCCI_CONNECT_B3_REQ 1 /* N-0 -> N-0.1 */ +#define EV_NCCI_CONNECT_B3_IND 2 /* N-0 -> N.1 */ +#define EV_NCCI_CONNECT_B3_CONF_OK 3 /* N-0.1 -> N.2 */ +#define EV_NCCI_CONNECT_B3_CONF_ERROR 4 /* N-0.1 -> N.0 */ +#define EV_NCCI_CONNECT_B3_REJECT 5 /* N-1 -> N-4 */ +#define EV_NCCI_CONNECT_B3_RESP 6 /* N-1 -> N-2 */ +#define EV_NCCI_CONNECT_B3_ACTIVE_IND 7 /* N-2 -> N-ACT */ +#define EV_NCCI_RESET_B3_REQ 8 /* N-ACT -> N-3 */ +#define EV_NCCI_RESET_B3_IND 9 /* N-3 -> N-ACT */ +#define EV_NCCI_DISCONNECT_B3_IND 10 /* N-4 -> N.5 */ +#define EV_NCCI_DISCONNECT_B3_CONF_ERROR 11 /* N-4 -> previous */ +#define EV_NCCI_DISCONNECT_B3_REQ 12 /* N-1 -> N-4 + N-2 -> N-4 + N-3 -> N-4 + N-ACT -> N-4 */ +#define EV_NCCI_DISCONNECT_B3_RESP 13 /* N-5 -> N-0 */ + +/* ----------------------------------------------------------------------- */ + +char *capiconn_version(void); + +/* -------- context ------------------------------------------------------ */ + +struct capi_connection; typedef struct capi_connection capi_connection; +struct capiconn_context; typedef struct capiconn_context capiconn_context; + +struct capiconn_callbacks +{ + /* ---------- memory functions ----------------- */ + + void *(*malloc)(size_t size); + void (*free)(void *buf); + + /* ---------- connection callbacks ------------- */ + + /* + * the capi_connection will be destoried after + * calling this function + */ + void (*disconnected)(capi_connection *, + int localdisconnect, + unsigned reason, + unsigned reason_b3); + /* + * The application should call capiconn_accept() + * capiconn_ignore() or capiconn_reject() inside + * of the function. If not called an ALERT_REQ will + * be sent, to let the application time to decide ... + */ + void (*incoming)(capi_connection *, + unsigned contr, + unsigned chipvalue, + char *callednumber, + char *callingnumber); + + /* + * Channel is ready to send and receive data + */ + void (*connected)(capi_connection *, _cstruct); + + /* + * Data received on channel, the data pointer + * can not be used, after this function is called. + */ + void (*received)(capi_connection *, + unsigned char *data, + unsigned datalen); + + /* + * called for every call to capiconn_send(). + */ + void (*datasent)(capi_connection *, unsigned char *); + + /* + * charge info received + */ + void (*chargeinfo)(capi_connection *, + unsigned long charge, + int inunits); + /* + * capi functions + */ + void (*capi_put_message) (unsigned appid, unsigned char *msg); + + /* + * message functions + */ + void (*debugmsg)(const char *fmt, ...); + void (*infomsg)(const char *fmt, ...); + void (*errmsg)(const char *fmt, ...); +}; +typedef struct capiconn_callbacks capiconn_callbacks; + +/* -------- context functions -------------------------------------------- */ + +capiconn_context *capiconn_getcontext(unsigned appid, capiconn_callbacks *env); + +int capiconn_freecontext(capiconn_context *ctx); + +/* -------- inject capi message into state machine ----------------------- */ + +void capiconn_inject(unsigned appid, unsigned char *msg); + +/* -------- add controller to context ------------------------------------ */ + +struct capi_contrinfo { + int bchannels; + char *ddi; + int ndigits; /* Durchwahllaenge */ +}; +typedef struct capi_contrinfo capi_contrinfo; + +/* + * returncodes: + * CAPICONN_OK - controller added to the context + * CAPICONN_NO_MEMORY - callback "malloc" returns no memory. + */ +int capiconn_addcontr(capiconn_context *ctx, + unsigned contr, capi_contrinfo *cinfo); + +/* -------- initiate a connection & disconnect a connection -------------- */ + +/* + * returncodes: + * a capi connection or 0 if + * - controller not found + * - memory problems + */ + +capi_connection * +capiconn_connect( + capiconn_context *ctx, + unsigned contr, + _cword cipvalue, + char *callednumber, /* remote number */ + char *callingnumber, /* own number */ + _cword b1proto, + _cword b2proto, + _cword b3proto, + _cstruct b1config, + _cstruct b2config, + _cstruct b3config, + _cstruct bchaninfo, + _cstruct ncpi); + +/* + * returncodes: + * CAPICONN_OK - disconnect initiated + * CAPICONN_ALREADY_DISCONNECTING - disconnect already initiated + * CAPICONN_WRONG_STATE - should not happen + */ +int capiconn_disconnect(capi_connection *connection, _cstruct ncpi); + +/* -------- reaction on incoming calls ----------------------------------- */ + +/* + * returncodes: + * CAPICONN_OK - Accept initiated + * CAPICONN_WRONG_STATE - "conn" ist not in state "incoming" + * CAPICONN_NO_MEMORY - callback "malloc" returns no memory. + */ +int capiconn_accept( + capi_connection *conn, + _cword b1proto, + _cword b2proto, + _cword b3proto, + _cstruct b1config, + _cstruct b2config, + _cstruct b3config, + _cstruct ncpi); + +/* + * returncodes: + * CAPICONN_OK - call will be ignored + * CAPICONN_WRONG_STATE - "conn" ist not in state "incoming" + */ +int capiconn_ignore(capi_connection *conn); + +/* + * returncodes: + * CAPICONN_OK - call will be rejected + * CAPICONN_WRONG_STATE - "conn" ist not in state "incoming" + */ +int capiconn_reject(capi_connection *conn); + +/* + * returncode: + * CAPICONN_OK - Data sent to CAPI + * CAPICONN_NOT_SENT - Data not sent (8 messages not acked) + * CAPICONN_WRONG_STATE - Connection is not connected + */ +int capiconn_send(capi_connection *plcip, + unsigned char *data, + unsigned len); + +/* + * get info about connection + */ + +struct capi_conninfo { + unsigned appid; + + unsigned plci; + int plci_state; + unsigned ncci; + int ncci_state; + unsigned isincoming:1, + disconnect_was_local; + unsigned disconnectreason; + unsigned disconnectreason_b3; + + /* user supplied */ + _cword cipvalue; + _cstruct callednumber; + _cstruct callingnumber; + _cword b1proto; + _cword b2proto; + _cword b3proto; + _cstruct b1config; + _cstruct b2config; + _cstruct b3config; + _cstruct bchaninfo; + _cstruct ncpi; +}; +typedef struct capi_conninfo capi_conninfo; + +/* + * returncode: + * always a pointer to the conninfo. + * the conninfo will not be update automaticly, + * you always have to call this function to get + * the actual informations. + */ +capi_conninfo *capiconn_getinfo(capi_connection *p); + +/* + * returncodes: + * CAPICONN_OK - Listen request sent + * CAPICONN_NO_CONTROLLER - Controller "contr" not added with + * capiconn_addcontr() + */ +int capiconn_listen(capiconn_context *ctx, + unsigned contr, unsigned cipmask, unsigned cipmask2); + +/* + * returncodes: + * CAPICONN_OK - got ack for listen request + * CAPICONN_NO_CONTROLLER - Controller "contr" not added with + * capiconn_addcontr() + * CAPICONN_WRONG_STATE - got no ack for listen request + */ +int capiconn_listenstate(capiconn_context *ctx, unsigned contr); + +#endif /* __CAPICONN_H__ */ diff --git a/data.c b/data.c new file mode 100644 index 0000000..1cc0392 --- /dev/null +++ b/data.c @@ -0,0 +1,236 @@ +/* + * Data handling functions + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "isdntun.h" +#include "options.h" +#include "tun.h" +#include "capiconn.h" + +int data_sent = 0; +time_t idle_last_use = 0; + +static capi_connection *conn = NULL; + +#undef DEBUG_DATA + +/* + * flush the data in the interface + */ +void empty_interface(void) +{ + fd_set rfds; + struct timeval tv; + int ret; + unsigned char buffer[2048]; + + info("empty interface %s queue", dev_name); + + while (1) { + tv.tv_sec = 0; + tv.tv_usec = 0; + FD_ZERO(&rfds); + FD_SET(if_fd, &rfds); + + ret = select(if_fd + 1, &rfds, NULL, NULL, &tv); + if (ret < 1) + break; + read(if_fd, buffer, sizeof(buffer)); + dbglog("flushed one buffer"); + } +} + +/* + * compress data + */ +static void compress_data(unsigned char *buf, int *len) +{ + unsigned long l1, l2; + unsigned char b[2048]; + + if (*len < 16) + return; + + l1 = (unsigned long)*len; + memcpy(b, buf, *len); + + l2 = sizeof(b); + + buf[0] = 0; + if ((compress(buf + 1, &l2, b, l1) != Z_OK) || + ((l2 + 1) > *len)) { + memcpy(buf + 1, b, *len); + *len += 1; + dbglog("if data not compressed len=%d", *len); + return; + } + *len = (int)(l2 + 1); + buf[0] = 1; + dbglog("if data compressed %ld->%d", l1, *len); +} + +/* + * compress data + */ +static void uncompress_data(unsigned char *buf, unsigned int *len) +{ + unsigned long l1, l2; + unsigned char b[2048]; + + memcpy(b, buf, *len); + *len -= 1; + + if (*b == 0) { /* not compressed */ + memcpy(buf, b + 1, *len); + return; + } + + l1 = (unsigned long)*len; + l2 = sizeof(b); + + if (uncompress(buf, &l2, b + 1, l1) != Z_OK) { + *len = 0; + error("uncompress error"); + return; + } + *len = (unsigned int)l2; + dbglog("if data uncompressed %ld->%d", l1, *len); +} + +/* + * handle data coming from device + */ +void handle_if_data(void) +{ + int len; + unsigned char buffer[2048]; + if ((len = read(if_fd, buffer, sizeof(buffer))) < 1) { + error("error %d reading %s", len, dev_name); + return; + } + + #ifdef DEBUG_DATA + { + int i; + FILE *f; + if ((f = fopen("/tmp/itun.txt", "a")) != NULL) { + fprintf(f, "inet %03d: ", len); + for (i = 0; i < len; i++) + fprintf(f, "%02x ", buffer[i]); + fprintf(f, "\n"); + fclose(f); + } + } + #endif + + if (encap == ENCAP_ZIPIP) + compress_data(buffer, &len); + + data_sent = 0; + + if (capiconn_send(conn, buffer, len) != CAPICONN_OK) { + error("data not sent"); + } + + idle_last_use = time(NULL); +} + +/* + * handle data coming from capi (isdn) + */ +void handle_capi_data(capi_connection *con, unsigned char* data, unsigned int len) +{ + int dlen; + + dlen = len; + dbglog("capi data to if of len %d", dlen); + + if (encap == ENCAP_ZIPIP) + uncompress_data(data, &dlen); + + if (write(if_fd, data, dlen) != dlen) + error("Error writing isdn data to %s", dev_name); + + idle_last_use = time(NULL); + + #ifdef DEBUG_DATA + { + int i; + FILE *f; + if ((f = fopen("/tmp/itun.txt", "a")) != NULL) { + fprintf(f, "isdn %03d: ", dlen); + for (i = 0; i < dlen; i++) + fprintf(f, "%02x ", data[i]); + fprintf(f, "\n"); + fclose(f); + } + } + #endif +} + +/* + * isdn data have been sent + */ +void handle_capi_sent(capi_connection *con, unsigned char* data) +{ + dbglog("data sent confirmed"); + data_sent = 1; +} + +/* + * get status of connection + */ +int is_connected(void) +{ + if (conn) + return(1); + return(0); +} + +/* + * if connection is established, store connection pointer + */ +void capi_is_connected(capi_connection *con) +{ + conn = con; + data_sent = 1; /* ready for data */ + idle_last_use = time(NULL); + new_phase(PHASE_RUNNING); +} + +/* + * if connection is released, clear connection pointer + */ +void capi_is_disconnected(capi_connection *con) +{ + conn = NULL; + new_phase(PHASE_DISCONNECT); +} + diff --git a/data.h b/data.h new file mode 100644 index 0000000..7566659 --- /dev/null +++ b/data.h @@ -0,0 +1,39 @@ +/* + * Data handling functions + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#ifndef __ITUN_DATA_H__ +#define __ITUN_DATA_H__ + +#include "capiconn.h" + +extern int data_sent; +extern time_t idle_last_use; + +extern void handle_if_data(void); +extern void empty_interface(void); +extern void handle_capi_data(capi_connection *con, unsigned char* data, unsigned int len); +extern void handle_capi_sent(capi_connection *con, unsigned char* data); +extern void capi_is_connected(capi_connection *con); +extern void capi_is_disconnected(capi_connection *con); +extern int is_connected(void); + +#endif /* __ITUN_DATA_H__ */ diff --git a/debug.c b/debug.c new file mode 100644 index 0000000..4e3220d --- /dev/null +++ b/debug.c @@ -0,0 +1,116 @@ +/* + * Debug handling + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#include +#include +#include +#include +#include + +#include "isdntun.h" +#include "options.h" + +int debug_level = 2; + +/* + * syslog + */ +static void do_syslog(int prio, char *format, va_list ap) +{ + unsigned char buf[2048]; + int len; + + len = sprintf(buf, "%s: ", opt_name); + vsnprintf(buf + len, sizeof(buf), format, ap); + syslog(prio, buf); +} + +/* + * debug + */ +static void debug_output(int level, char *format, va_list ap) +{ + switch (level) { + case 0: + case 1: + case 2: + do_syslog(LOG_ERR, format, ap); + break; + case 3: + do_syslog(LOG_INFO, format, ap); + break; + } + + if (level > debug_level) + return; + + fprintf(stderr, "Itun %d: ", level); + vfprintf(stderr, format, ap); + fprintf(stderr, "\n"); +} + +void option_error(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + debug_output(0, format, ap); + va_end(ap); +} + +void fatal(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + debug_output(1, format, ap); + va_end(ap); + exit(-1); +} + +void error(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + debug_output(2, format, ap); + va_end(ap); +} + +void info(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + debug_output(3, format, ap); + va_end(ap); +} + +void dbglog(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + debug_output(4, format, ap); + va_end(ap); +} + diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..8f30804 --- /dev/null +++ b/debug.h @@ -0,0 +1,39 @@ +/* + * Debug handling + * + * Copyright 2004 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#ifndef __ITUN_DEBUG_H__ +#define __ITUN_DEBUG_H__ + +/* + * debug + */ + +extern int debug_level; +extern void fatal(char *format, ...); +extern void error(char *format, ...); +extern void info(char *format, ...); +extern void dbglog(char *format, ...); +extern void option_error(char *format, ...); + + +#endif /* __ITUN_DEBUG_H__ */ + diff --git a/isdntun.c b/isdntun.c new file mode 100644 index 0000000..0e470a9 --- /dev/null +++ b/isdntun.c @@ -0,0 +1,427 @@ +/* + * ISDN tunnel Daemon "ItunD" + * + * Copyright 2004-2005 SYSGO Real-Time Solutions AG + * Klein-Winternheim, Germany + * All rights reserved. + * + * Author: Armin Schindler + * + * Copyright 2004-2005 Cytronics & Melware + * + * Armin Schindler + * + * $Id$ + * + * 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. See . + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "isdntun.h" +#include "options.h" +#include "signals.h" +#include "tun.h" + +static char pid_file[128]; + +int if_fd; +int capi_fd; + +int status = EXIT_OK; +int iphase = PHASE_DEAD; + +void new_phase(int phase) +{ + iphase = phase; + capi_new_phase(phase); +} + +/* + * exit + */ +void die(int val) +{ + error("die with %d", val); + itun_close(if_fd); + itun_exit(); + unlink(pid_file); + exit(val); +} + +/* + * usage + */ +static void usage(char *prg) +{ + fprintf(stderr, "ISDN-Tunnel Daemon Version %s (c) 2004-2005 Cytronics & Melware, SYSGO AG\n", VERSION); + fprintf(stderr, "usage: %s