Compare commits

..

3 Commits
0.3.2 ... 0.2.0

Author SHA1 Message Date
Kevin P. Fleming
d623b2412e remove extraneous svn:executable properties
git-svn-id: https://origsvn.digium.com/svn/libpri/tags/0.2.0@266 2fbb986a-6c06-0410-b554-c9c1f0a7f128
2005-11-29 18:39:18 +00:00
Kevin P. Fleming
63699af574 automatic tag renames
git-svn-id: https://origsvn.digium.com/svn/libpri/tags/0.2.0@263 2fbb986a-6c06-0410-b554-c9c1f0a7f128
2005-11-27 04:17:23 +00:00
Admin Commit
2323c14331 This commit was manufactured by cvs2svn to create tag 'v0-2-0'.
git-svn-id: https://origsvn.digium.com/svn/libpri/tags/v0-2-0@16 2fbb986a-6c06-0410-b554-c9c1f0a7f128
2001-12-07 19:45:45 +00:00
12 changed files with 269 additions and 1549 deletions

View File

@@ -1,7 +1,3 @@
libpri 0.3.0
-- Fix talking to switch
-- Add pri dump
-- Add test application
-- Fix strncpy stuff
libpri 0.1.2
-- Added PRI_EVENT_HANGUP_ACK so you can know when the disconnect was

View File

@@ -26,25 +26,18 @@ DYNAMIC_LIBRARY=libpri.so.1.0
STATIC_OBJS=pri.o q921.o prisched.o q931.o
DYNAMIC_OBJS=pri.lo q921.lo prisched.lo q931.lo
CFLAGS=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g
INSTALL_PREFIX=
all: $(STATIC_LIBRARY) $(DYNAMIC_LIBRARY)
install: $(STATIC_LIBRARY) $(DYNAMIC_LIBRARY)
mkdir -p $(INSTALL_PREFIX)/usr/lib
mkdir -p $(INSTALL_PREFIX)/usr/include
install -m 644 libpri.h $(INSTALL_PREFIX)/usr/include
install -m 755 $(DYNAMIC_LIBRARY) $(INSTALL_PREFIX)/usr/lib
( cd $(INSTALL_PREFIX)/usr/lib ; ln -sf libpri.so.1 libpri.so )
install -m 644 $(STATIC_LIBRARY) $(INSTALL_PREFIX)/usr/lib
mkdir -p /usr/lib
mkdir -p /usr/include
install -m 644 libpri.h /usr/include
install -m 755 $(DYNAMIC_LIBRARY) /usr/lib
( cd /usr/lib ; ln -sf libpri.so.1 libpri.so )
install -m 644 $(STATIC_LIBRARY) /usr/lib
/sbin/ldconfig
pritest: pritest.o
$(CC) -o pritest pritest.o -L. -lpri -lzap
pridump: pridump.o
$(CC) -o pridump pridump.o -L. -lpri -lzap
%.lo : %.c
$(CC) -fPIC $(CFLAGS) -o $@ -c $<
@@ -60,4 +53,3 @@ $(DYNAMIC_LIBRARY): $(DYNAMIC_OBJS)
clean:
rm -f *.o *.so *.lo
rm -f testpri $(STATIC_LIBRARY) $(DYNAMIC_LIBRARY)
rm -f pritest pridump

View File

@@ -22,8 +22,8 @@
*
*/
#ifndef _LIBPRI_H
#define _LIBPRI_H
#ifndef _PRI_H
#define _PRI_H
/* Node types */
#define PRI_NETWORK 1
@@ -50,50 +50,20 @@
#define PRI_SWITCH_EUROISDN_T1 6 /* T1 EuroISDN variant (ETSI 300-102) */
/* PRI D-Channel Events */
#define PRI_EVENT_DCHAN_UP 1 /* D-channel is up */
#define PRI_EVENT_DCHAN_DOWN 2 /* D-channel is down */
#define PRI_EVENT_RESTART 3 /* B-channel is restarted */
#define PRI_EVENT_CONFIG_ERR 4 /* Configuration Error Detected */
#define PRI_EVENT_RING 5 /* Incoming call */
#define PRI_EVENT_HANGUP 6 /* Call got hung up */
#define PRI_EVENT_RINGING 7 /* Call is ringing (alerting) */
#define PRI_EVENT_ANSWER 8 /* Call has been answered */
#define PRI_EVENT_HANGUP_ACK 9 /* Call hangup has been acknowledged */
#define PRI_EVENT_RESTART_ACK 10 /* Restart complete on a given channel */
#define PRI_EVENT_FACNAME 11 /* Caller*ID Name received on Facility */
#define PRI_EVENT_INFO_RECEIVED 12 /* Additional info (keypad) received */
#define PRI_EVENT_DCHAN_UP 1 /* D-channel is up */
#define PRI_EVENT_DCHAN_DOWN 2 /* D-channel is down */
#define PRI_EVENT_RESTART 3 /* B-channel is restarted */
#define PRI_EVENT_CONFIG_ERR 4 /* Configuration Error Detected */
#define PRI_EVENT_RING 5 /* Incoming call */
#define PRI_EVENT_HANGUP 6 /* Call got hung up */
#define PRI_EVENT_RINGING 7 /* Call is ringing (alerting) */
#define PRI_EVENT_ANSWER 8 /* Call has been answered */
#define PRI_EVENT_HANGUP_ACK 9 /* Call hangup has been acknowledged */
/* Simple states */
#define PRI_STATE_DOWN 0
#define PRI_STATE_UP 1
/* Numbering plan identifier */
#define PRI_NPI_UNKNOWN 0x0
#define PRI_NPI_E163_E164 0x1
#define PRI_NPI_X121 0x3
#define PRI_NPI_F69 0x4
#define PRI_NPI_NATIONAL 0x8
#define PRI_NPI_PRIVATE 0x9
#define PRI_NPI_RESERVED 0xF
/* Type of number */
#define PRI_TON_UNKNOWN 0x0
#define PRI_TON_INTERNATIONAL 0x1
#define PRI_TON_NATIONAL 0x2
#define PRI_TON_NET_SPECIFIC 0x3
#define PRI_TON_SUBSCRIBER 0x4
#define PRI_TON_ABBREVIATED 0x6
#define PRI_TON_RESERVED 0x7
/* Redirection reasons */
#define PRI_REDIR_UNKNOWN 0x0
#define PRI_REDIR_FORWARD_ON_BUSY 0x1
#define PRI_REDIR_FORWARD_ON_NO_REPLY 0x2
#define PRI_REDIR_DEFLECTION 0x3
#define PRI_REDIR_DTE_OUT_OF_ORDER 0x9
#define PRI_REDIR_FORWARDED_BY_DTE 0xA
#define PRI_REDIR_UNCONDITIONAL 0xF
/* Dialing plan */
#define PRI_INTERNATIONAL_ISDN 0x11
#define PRI_NATIONAL_ISDN 0x21
@@ -149,12 +119,12 @@
#define PRI_CAUSE_INVALID_MSG_UNSPECIFIED 95
#define PRI_CAUSE_MANDATORY_IE_MISSING 96
#define PRI_CAUSE_MESSAGE_TYPE_NONEXIST 97
#define PRI_CAUSE_WRONG_MESSAGE 98
#define PRI_CAUSE_WRONG_MESSAGE 98
#define PRI_CAUSE_IE_NONEXIST 99
#define PRI_CAUSE_INVALID_IE_CONTENTS 100
#define PRI_CAUSE_WRONG_CALL_STATE 101
#define PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE 102
#define PRI_CAUSE_MANDATORY_IE_LENGTH_ERROR 103
#define PRI_CAUSE_MANDATORY_IE_LENGTH_ERROR 103
#define PRI_CAUSE_PROTOCOL_ERROR 111
#define PRI_CAUSE_INTERWORKING 127
@@ -167,9 +137,9 @@
#define PRI_TRANS_CAP_VIDEO 0x18
#define PRI_LAYER_1_ITU_RATE_ADAPT 0x21
#define PRI_LAYER_1_ULAW 0x22
#define PRI_LAYER_1_ALAW 0x23
#define PRI_LAYER_1_G721 0x24
#define PRI_LAYER_1_ULAW 0x22
#define PRI_LAYER_1_ALAW 0x23
#define PRI_LAYER_1_G721 0x24
#define PRI_LAYER_1_G722_G725 0x25
#define PRI_LAYER_1_G7XX_384K 0x26
#define PRI_LAYER_1_NON_ITU_ADAPT 0x27
@@ -207,26 +177,14 @@ typedef struct pri_event_answer {
q931_call *call;
} pri_event_answer;
typedef struct pri_event_facname {
int e;
char callingname[256];
char callingnum[256];
int channel;
int cref;
q931_call *call;
} pri_event_facname;
typedef struct pri_event_ring {
int e;
int channel; /* Channel requested */
int callingpres; /* Presentation of Calling CallerID */
int callingplan; /* Dialing plan of Calling entity */
char callingnum[256]; /* Calling number */
char callingname[256]; /* Calling name (if provided) */
int calledplan; /* Dialing plan of Called number */
char callednum[256]; /* Called number */
char redirectingnum[256]; /* Redirecting number */
char useruserinfo[256]; /* User->User info */
int flexible; /* Are we flexible with our channel selection? */
int cref; /* Call Reference Number */
int ctype; /* Call type (see PRI_TRANS_CAP_* */
@@ -242,22 +200,15 @@ typedef struct pri_event_hangup {
q931_call *call; /* Opaque call pointer */
} pri_event_hangup;
typedef struct pri_event_restart_ack {
int e;
int channel;
} pri_event_restart_ack;
typedef union {
int e;
pri_event_generic gen; /* Generic view */
pri_event_restart restart; /* Restart view */
pri_event_error err; /* Error view */
pri_event_facname facname; /* Caller*ID Name on Facility */
pri_event_ring ring; /* Ring */
pri_event_hangup hangup; /* Hang up */
pri_event_ringing ringing; /* Ringing */
pri_event_ringing answer; /* Answer */
pri_event_restart_ack restartack; /* Restart Acknowledge */
} pri_event;
struct pri;
@@ -308,13 +259,6 @@ extern char *pri_cause2str(int cause);
is in-band data available on the channel */
extern int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info);
/* Send a digit in overlap mode */
extern int pri_information(struct pri *pri, q931_call *call, char digit);
/* Answer the incomplete(call without called number) call on the given channel.
Set non-isdn to non-zero if you are not connecting to ISDN equipment */
extern int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn);
/* Answer the call on the given channel (ignored if you called acknowledge already).
Set non-isdn to non-zero if you are not connecting to ISDN equipment */
extern int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn);
@@ -325,8 +269,6 @@ extern int pri_release(struct pri *pri, q931_call *call, int cause);
/* Hangup / Disconnect a call */
extern int pri_disconnect(struct pri *pri, q931_call *call, int cause);
extern int pri_reset(struct pri *pri, int channel);
/* Create a new call */
extern q931_call *pri_new_call(struct pri *pri);
@@ -337,10 +279,6 @@ extern struct timeval *pri_schedule_next(struct pri *pri);
extern pri_event *pri_schedule_run(struct pri *pri);
extern int pri_call(struct pri *pri, q931_call *c, int transmode, int channel,
int exclusive, int nonisdn, char *caller, int callerplan, char *callername, int callerpres,
int exclusive, int nonisdn, char *caller, int callerplan, int callerpres,
char *called,int calledplan, int ulayer1);
/* Override message and error stuff */
extern void pri_set_message(void (*__pri_error)(char *));
extern void pri_set_error(void (*__pri_error)(char *));
#endif

87
pri.c
View File

@@ -18,7 +18,6 @@
#include <unistd.h>
#include <stdlib.h>
#include <sys/select.h>
#include <stdarg.h>
#include "libpri.h"
#include "pri_internal.h"
#include "pri_q921.h"
@@ -65,7 +64,7 @@ struct pri *pri_new(int fd, int node, int switchtype)
p->switchtype = switchtype;
p->cref = 1;
/* Start Q.921 layer */
q921_start(p, 1);
q921_start(p);
}
return p;
}
@@ -96,11 +95,9 @@ pri_event *pri_check_event(struct pri *pri)
res = read(pri->fd, buf, sizeof(buf));
if (res < 0) {
if (errno != EAGAIN)
pri_error("Read on %d failed: %s\n", pri->fd, strerror(errno));
fprintf(stderr, "Read on %d failed: %s\n", pri->fd, strerror(errno));
return NULL;
}
if (!res)
return NULL;
/* Receive the q921 packet */
e = q921_receive(pri, (q921_h *)buf, res);
return e;
@@ -181,20 +178,6 @@ int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
return q931_alerting(pri, call, channel, info);
}
int pri_information(struct pri *pri, q931_call *call, char digit)
{
if (!pri || !call)
return -1;
return q931_information(pri, call, digit);
}
int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
{
if (!pri || !call)
return -1;
return q931_setup_ack(pri, call, channel, nonisdn);
}
int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
{
if (!pri || !call)
@@ -216,13 +199,6 @@ int pri_disconnect(struct pri *pri, q931_call *call, int cause)
return q931_disconnect(pri, call, cause);
}
int pri_reset(struct pri *pri, int channel)
{
if (!pri)
return -1;
return q931_restart(pri, channel);
}
q931_call *pri_new_call(struct pri *pri)
{
if (!pri)
@@ -234,73 +210,34 @@ void pri_dump_event(struct pri *pri, pri_event *e)
{
if (!pri || !e)
return;
pri_message("Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
printf("Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
switch(e->gen.e) {
case PRI_EVENT_DCHAN_UP:
case PRI_EVENT_DCHAN_DOWN:
break;
case PRI_EVENT_CONFIG_ERR:
pri_message("Error: %s", e->err.err);
printf("Error: %s", e->err.err);
break;
case PRI_EVENT_RESTART:
pri_message("Restart on channel %d\n", e->restart.channel);
printf("Restart on channel %d\n", e->restart.channel);
case PRI_EVENT_RING:
pri_message("Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
pri_message("Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
pri_message("Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
printf("Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
printf("Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
printf("Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
break;
case PRI_EVENT_HANGUP:
pri_message("Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
printf("Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
break;
default:
pri_message("Don't know how to dump events of type %d\n", e->gen.e);
printf("Don't know how to dump events of type %d\n", e->gen.e);
}
}
int pri_call(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive,
int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
int nonisdn, char *caller, int callerplan, int callerpres, char *called,
int calledplan,int ulayer1)
{
if (!pri || !c)
return -1;
return q931_setup(pri, c, transmode, channel, exclusive, nonisdn, caller, callerplan, callername, callerpres, called, calledplan, ulayer1);
return q931_setup(pri, c, transmode, channel, exclusive, nonisdn, caller, callerplan, callerpres, called, calledplan, ulayer1);
}
static void (*__pri_error)(char *stuff);
static void (*__pri_message)(char *stuff);
void pri_set_message(void (*func)(char *stuff))
{
__pri_message = func;
}
void pri_set_error(void (*func)(char *stuff))
{
__pri_error = func;
}
void pri_message(char *fmt, ...)
{
char tmp[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(tmp, sizeof(tmp), fmt, ap);
va_end(ap);
if (__pri_message)
__pri_message(tmp);
else
fprintf(stdout, tmp);
}
void pri_error(char *fmt, ...)
{
char tmp[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(tmp, sizeof(tmp), fmt, ap);
va_end(ap);
if (__pri_error)
__pri_error(tmp);
else
fprintf(stderr, tmp);
}

View File

@@ -88,8 +88,4 @@ extern void pri_schedule_del(struct pri *pri, int ev);
extern pri_event *pri_mkerror(struct pri *pri, char *errstr);
extern void pri_message(char *fmt, ...);
extern void pri_error(char *fmt, ...);
#endif

View File

@@ -137,7 +137,7 @@ typedef struct q921_frame {
q921_i h;
} q921_frame;
#define Q921_INC(j) (j) = (((j) + 1) % 128)
#define Q921_INC(j) (j) = ((j) + 1) % 128
typedef enum q921_state {
Q921_LINK_CONNECTION_RELEASED, /* Also known as TEI_ASSIGNED */
@@ -150,7 +150,7 @@ typedef enum q921_state {
extern void q921_dump(q921_h *h, int len, int showraw, int txrx);
/* Bring up the D-channel */
extern void q921_start(struct pri *pri, int now);
extern void q921_start(struct pri *pri);
extern void q921_reset(struct pri *pri);

View File

@@ -190,7 +190,6 @@ typedef struct q931_ie {
#define Q931_LOW_LAYER_COMPAT 0x7c
#define Q931_HIGH_LAYER_COMPAT 0x7d
#define Q931_DISPLAY 0x28
#define Q931_IE_SEGMENTED_MSG 0x00
#define Q931_IE_CHANGE_STATUS 0x01
#define Q931_IE_CONNECTED_NUM 0x0C
@@ -198,6 +197,7 @@ typedef struct q931_ie {
#define Q931_IE_FACILITY 0x1c
#define Q931_IE_ENDPOINT_ID 0x26
#define Q931_IE_NOTIFY_IND 0x27
#define Q931_IE_DISPLAY 0x28
#define Q931_IE_TIME_DATE 0x29
#define Q931_IE_KEYPAD_FACILITY 0x2c
#define Q931_IE_CALL_STATUS 0x2d
@@ -214,28 +214,6 @@ typedef struct q931_ie {
#define Q931_IE_USER_USER 0x7E
#define Q931_IE_ESCAPE_FOR_EXT 0x7F
/* Call state stuff */
#define Q931_CALL_STATE_NULL 0
#define Q931_CALL_STATE_CALL_INITIATED 1
#define Q931_CALL_STATE_OVERLAP_SENDING 2
#define Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING 3
#define Q931_CALL_STATE_CALL_DELIVERED 4
#define Q931_CALL_STATE_CALL_PRESENT 6
#define Q931_CALL_STATE_CALL_RECEIVED 7
#define Q931_CALL_STATE_CONNECT_REQUEST 8
#define Q931_CALL_STATE_INCOMING_CALL_PROCEEDING 9
#define Q931_CALL_STATE_ACTIVE 10
#define Q931_CALL_STATE_DISCONNECT_REQUEST 11
#define Q931_CALL_STATE_DISCONNECT_INDICATION 12
#define Q931_CALL_STATE_SUSPEND_REQUEST 15
#define Q931_CALL_STATE_RESUME_REQUEST 17
#define Q931_CALL_STATE_RELEASE_REQUEST 19
#define Q931_CALL_STATE_OVERLAP_RECEIVING 25
#define Q931_CALL_STATE_RESTART_REQUEST 61
#define Q931_CALL_STATE_RESTART 62
/* EuroISDN */
#define Q931_SENDING_COMPLETE 0x21
@@ -245,22 +223,16 @@ extern int q931_alerting(struct pri *pri, q931_call *call, int channel, int info
extern int q931_call_proceeding(struct pri *pri, q931_call *call);
extern int q931_setup_ack(struct pri *pri, q931_call *call, int channel, int nonisdn);
extern int q931_information(struct pri *pri, q931_call *call, char digit);
extern int q931_connect(struct pri *pri, q931_call *call, int channel, int nonisdn);
extern int q931_release(struct pri *pri, q931_call *call, int cause);
extern int q931_disconnect(struct pri *pri, q931_call *call, int cause);
extern int q931_restart(struct pri *pri, int channel);
extern q931_call *q931_new_call(struct pri *pri);
extern int q931_setup(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive,
int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
int nonisdn, char *caller, int callerplan, int callerpres, char *called,
int calledplan, int ulay1);
extern void q931_dump(q931_h *h, int len, int txrx);
#endif

130
pridump.c
View File

@@ -1,130 +0,0 @@
/*
* libpri: An implementation of Primary Rate ISDN
*
* Written by Mark Spencer <markster@linux-support.net>
*
* Copyright (C) 2001, Linux Support Services, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* This program tests libpri call reception using a zaptel interface.
* Its state machines are setup for RECEIVING CALLS ONLY, so if you
* are trying to both place and receive calls you have to a bit more.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <linux/zaptel.h>
#include "libpri.h"
#include "pri_q921.h"
#include "pri_q931.h"
static int pri_open(char *dev)
{
int dfd;
struct zt_params p;
dfd = open(dev, O_RDWR);
if (dfd < 0) {
fprintf(stderr, "Failed to open dchannel '%s': %s\n", dev, strerror(errno));
return -1;
}
if (ioctl(dfd, ZT_GET_PARAMS, &p)) {
fprintf(stderr, "Unable to get parameters on '%s': %s\n", dev, strerror(errno));
return -1;
}
if ((p.sigtype != ZT_SIG_HDLCRAW) && (p.sigtype != ZT_SIG_HDLCFCS)) {
fprintf(stderr, "%s is in %d signalling, not FCS HDLC or RAW HDLC mode\n", dev, p.sigtype);
return -1;
}
return dfd;
}
static void dump_packet(char *buf, int len, int txrx)
{
q921_h *h = (q921_h *)buf;
q921_dump(h, len, 1, txrx);
if (!((h->h.data[0] & Q921_FRAMETYPE_MASK) & 0x3)) {
q931_dump((q931_h *)(h->i.data), len - 4, txrx);
}
fflush(stdout);
fflush(stderr);
}
static int pri_bridge(int d1, int d2)
{
char buf[1024];
fd_set fds;
int max;
int e;
int res;
for(;;) {
FD_ZERO(&fds);
FD_SET(d1, &fds);
FD_SET(d2, &fds);
max = d1;
if (max < d2)
max = d2;
ioctl(d1, ZT_GETEVENT, &e);
ioctl(d2, ZT_GETEVENT, &e);
res = select(max + 1, &fds, NULL, NULL, NULL);
if (res < 0) {
fprintf(stderr, "Select returned %d: %s\n", res, strerror(errno));
continue;
};
if (FD_ISSET(d1, &fds)) {
/* Copy from d1 to d2 */
res = read(d1, buf, sizeof(buf));
dump_packet(buf, res, 1);
res = write(d2, buf, res);
}
if (FD_ISSET(d2, &fds)) {
/* Copy from d2 to d1 */
res = read(d2, buf, sizeof(buf));
dump_packet(buf, res, 0);
res = write(d1, buf, res);
}
}
}
int main(int argc, char *argv[])
{
int d1, d2;
if (argc < 3) {
fprintf(stderr, "Usage: pridump <dev1> <dev2>\n");
exit(1);
}
d1 = pri_open(argv[1]);
if (d1 < 0)
exit(1);
d2 = pri_open(argv[2]);
if (d2 < 0)
exit(1);
pri_bridge(d1, d2);
return 0;
}

View File

@@ -38,7 +38,7 @@ int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), vo
if (!pri->pri_sched[x].callback)
break;
if (x == MAX_SCHED) {
pri_error("No more room in scheduler\n");
fprintf(stderr, "No more room in scheduler\n");
return -1;
}
if (x > maxsched)
@@ -98,6 +98,6 @@ pri_event *pri_schedule_run(struct pri *pri)
void pri_schedule_del(struct pri *pri,int id)
{
if ((id >= MAX_SCHED) || (id < 0))
pri_error("Asked to delete sched id %d???\n", id);
fprintf(stderr, "Asked to delete sched id %d???\n", id);
pri->pri_sched[id].callback = NULL;
}

400
pritest.c
View File

@@ -1,400 +0,0 @@
/*
* libpri: An implementation of Primary Rate ISDN
*
* Written by Mark Spencer <markster@linux-support.net>
*
* Copyright (C) 2001, Linux Support Services, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* This program tests libpri call reception using a zaptel interface.
* Its state machines are setup for RECEIVING CALLS ONLY, so if you
* are trying to both place and receive calls you have to a bit more.
*/
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <linux/zaptel.h>
#include <zap.h>
#include "libpri.h"
#define PRI_DEF_NODETYPE PRI_CPE
#define PRI_DEF_SWITCHTYPE PRI_SWITCH_NI2
#define MAX_CHAN 32
#define DCHANNEL_TIMESLOT 16
static int offset = 0;
static void do_channel(ZAP *z)
{
/* This is the part that runs on a given channel */
zap_playf(z, "raw.ulaw", 0);
}
struct pri_chan {
pid_t pid;
int needhangup;
int alreadyhungup;
q931_call *call;
} chans[MAX_CHAN];
static int str2node(char *node)
{
if (!strcasecmp(node, "cpe"))
return PRI_CPE;
if (!strcasecmp(node, "network"))
return PRI_NETWORK;
return -1;
}
static void chan_ended(int sig)
{
int status;
int x;
struct rusage rusage;
pid_t pid;
pid = wait4(-1, &status, WNOHANG, &rusage);
for (x=0;x<MAX_CHAN;x++) {
if (pid == chans[x].pid) {
printf("-- PID %d ended, channel %d\n", pid, x);
chans[x].pid = 0;
if (!chans[x].alreadyhungup) {
/* It died, we need to hangup now */
chans[x].needhangup = 1;
} else {
/* We've already been hungup, just clear it */
chans[x].alreadyhungup = 0;
chans[x].call = NULL;
}
return;
}
}
if (pid > -1) {
fprintf(stderr, "--!! Unknown PID %d exited\n", pid);
return;
}
}
static int str2switch(char *swtype)
{
if (!strcasecmp(swtype, "ni2"))
return PRI_SWITCH_NI2;
if (!strcasecmp(swtype, "dms100"))
return PRI_SWITCH_DMS100;
if (!strcasecmp(swtype, "lucent5e"))
return PRI_SWITCH_LUCENT5E;
if (!strcasecmp(swtype, "att4ess"))
return PRI_SWITCH_ATT4ESS;
if (!strcasecmp(swtype, "euroisdn"))
return PRI_SWITCH_EUROISDN_E1;
return -1;
}
static void hangup_channel(int channo)
{
if (chans[channo].pid) {
#if 0
printf("Killing channel %d (pid = %d)\n", channo, chans[channo].pid);
#endif
chans[channo].alreadyhungup = 1;
kill(chans[channo].pid, SIGTERM);
} else if (chans[channo].needhangup)
chans[channo].needhangup = 0;
}
static void launch_channel(int channo)
{
pid_t pid;
ZAP *z;
char ch[80];
/* Make sure hangup state is reset */
chans[channo].needhangup = 0;
chans[channo].alreadyhungup = 0;
pid = fork();
if (pid < 0) {
fprintf(stderr, "--!! Unable to fork\n");
chans[channo].needhangup = 1;
}
if (pid) {
printf("-- Launching process %d to handle channel %d\n", pid, channo);
chans[channo].pid = pid;
} else {
sprintf(ch, "%d", channo + offset);
z = zap_open(ch, 0);
if (z) {
do_channel(z);
exit(0);
} else {
fprintf(stderr, "--!! Unable to open channel %d\n", channo);
exit(1);
}
}
}
static int get_free_channel(int channo)
{
channo--;
if((channo>MAX_CHAN)||(channo<0)) {
fprintf(stderr, "Invalid Bchannel RANGE <%d", channo);
return 0;
};
while(chans[channo].pid) {
channo--;
}
return channo;
}
/* place here criteria for completion of destination number */
static int number_incommplete(char *number)
{
return strlen(number) < 3;
}
static void start_channel(struct pri *pri, pri_event *e)
{
int channo = e->ring.channel;
int flag = 1;
pri_event_ring *ring = &e->ring;
if(channo == -1) {
channo = e->ring.channel = get_free_channel(MAX_CHAN);
if(channo == DCHANNEL_TIMESLOT)
channo = e->ring.channel = get_free_channel(MAX_CHAN);
fprintf(stdout, "Any channel selected: %d\n", channo);
if(!channo) {
pri_release(pri, ring->call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
fprintf(stdout, "Abort call due to no avl B channels\n");
return;
}
flag = 0;
}
/* Make sure it's a valid number */
if ((channo >= MAX_CHAN) || (channo < 0)) {
fprintf(stderr, "--!! Channel %d is out of range\n", channo);
return;
}
/* Make sure nothing is there */
if (chans[channo].pid) {
fprintf(stderr, "--!! Channel %d still has a call on it, ending it...\n", channo);
hangup_channel(channo);
/* Wait for it to die */
while(chans[channo].pid)
usleep(100);
}
/* Record call number */
chans[channo].call = e->ring.call;
/* Answer the line */
if(flag) {
pri_answer(pri, chans[channo].call, channo, 1);
} else {
pri_need_more_info(pri, chans[channo].call, channo, 1);
}
/* Launch a process to handle it */
launch_channel(channo);
}
static void handle_pri_event(struct pri *pri, pri_event *e)
{
switch(e->e) {
case PRI_EVENT_DCHAN_UP:
printf("-- D-Channel is now up! :-)\n");
break;
case PRI_EVENT_DCHAN_DOWN:
printf("-- D-Channel is now down! :-(\n");
break;
case PRI_EVENT_RESTART:
printf("-- Restarting channel %d\n", e->restart.channel);
hangup_channel(e->restart.channel);
break;
case PRI_EVENT_CONFIG_ERR:
printf("-- Configuration error detected: %s\n", e->err.err);
break;
case PRI_EVENT_RING:
printf("-- Ring on channel %d (from %s to %s), answering...\n", e->ring.channel, e->ring.callingnum, e->ring.callednum);
start_channel(pri, e);
break;
case PRI_EVENT_HANGUP:
printf("-- Hanging up channel %d\n", e->hangup.channel);
hangup_channel(e->hangup.channel);
break;
case PRI_EVENT_RINGING:
case PRI_EVENT_ANSWER:
fprintf(stderr, "--!! What? We shouldn't be making any calls...\n");
break;
case PRI_EVENT_HANGUP_ACK:
/* Ignore */
break;
case PRI_EVENT_INFO_RECEIVED:
fprintf(stdout, "number is: %s\n", e->ring.callednum);
if(!number_incommplete(e->ring.callednum)) {
fprintf(stdout, "final number is: %s\n", e->ring.callednum);
pri_answer(pri, e->ring.call, 0, 1);
}
break;
default:
fprintf(stderr, "--!! Unknown PRI event %d\n", e->e);
}
}
static int run_pri(int dfd, int swtype, int node)
{
struct pri *pri;
pri_event *e;
struct timeval tv = {0,0}, *next;
fd_set rfds, efds;
int res,x;
pri = pri_new(dfd, node, swtype);
if (!pri) {
fprintf(stderr, "Unable to create PRI\n");
return -1;
}
for (;;) {
/* Run the D-Channel */
FD_ZERO(&rfds);
FD_ZERO(&efds);
FD_SET(dfd, &rfds);
FD_SET(dfd, &efds);
if ((next = pri_schedule_next(pri))) {
gettimeofday(&tv, NULL);
tv.tv_sec = next->tv_sec - tv.tv_sec;
tv.tv_usec = next->tv_usec - tv.tv_usec;
if (tv.tv_usec < 0) {
tv.tv_usec += 1000000;
tv.tv_sec -= 1;
}
if (tv.tv_sec < 0) {
tv.tv_sec = 0;
tv.tv_usec = 0;
}
}
res = select(dfd + 1, &rfds, NULL, &efds, next ? &tv : NULL);
e = NULL;
if (!res) {
e = pri_schedule_run(pri);
} else if (res > 0) {
e = pri_check_event(pri);
} else if (errno == ELAST) {
res = ioctl(dfd, ZT_GETEVENT, &x);
printf("Got Zaptel event: %d\n", x);
} else if (errno != EINTR)
fprintf(stderr, "Error (%d) on select: %s\n", ELAST, strerror(errno));
if (e) {
handle_pri_event(pri, e);
}
res = ioctl(dfd, ZT_GETEVENT, &x);
if (!res && x) {
fprintf(stderr, "Got event on PRI interface: %d\n", x);
}
/* Check for lines that need hangups */
for (x=0;x<MAX_CHAN;x++)
if (chans[x].needhangup) {
chans[x].needhangup = 0;
pri_release(pri, chans[x].call, PRI_CAUSE_NORMAL_CLEARING);
}
}
return 0;
}
int main(int argc, char *argv[])
{
int dfd;
int swtype = PRI_DEF_SWITCHTYPE;
int node = PRI_DEF_NODETYPE;
struct zt_params p;
if (argc < 2) {
fprintf(stderr, "Usage: pritest <dchannel> [swtypetype] [nodetype]\n");
exit(1);
}
dfd = open(argv[1], O_RDWR);
if (dfd < 0) {
fprintf(stderr, "Failed to open dchannel '%s': %s\n", argv[1], strerror(errno));
exit(1);
}
if (ioctl(dfd, ZT_GET_PARAMS, &p)) {
fprintf(stderr, "Unable to get parameters on '%s': %s\n", argv[1], strerror(errno));
exit(1);
}
if ((p.sigtype != ZT_SIG_HDLCRAW) && (p.sigtype != ZT_SIG_HDLCFCS)) {
fprintf(stderr, "%s is in %d signalling, not FCS HDLC or RAW HDLC mode\n", argv[1], p.sigtype);
exit(1);
}
if (argc > 2) {
swtype = str2switch(argv[2]);
if (swtype < 0) {
fprintf(stderr, "Valid switchtypes are: ni2, dms100, lucent5e, att4ess, and euroisdn\n");
exit(1);
}
}
if (argc > 3) {
node = str2node(argv[3]);
if (node < 0) {
fprintf(stderr, "Valid node types are: network and cpe\n");
exit(1);
}
}
signal(SIGCHLD, chan_ended);
if (run_pri(dfd, swtype, node))
exit(1);
exit(0);
return 0;
}

287
q921.c
View File

@@ -42,7 +42,6 @@
*/
#define Q921_INIT(hf) do { \
memset(&(hf),0,sizeof(hf)); \
(hf).h.sapi = 0; \
(hf).h.ea1 = 0; \
(hf).h.ea2 = 1; \
@@ -66,7 +65,7 @@ static int q921_transmit(struct pri *pri, q921_h *h, int len) {
int res;
#ifdef RANDOM_DROPS
if (!(random() % 3)) {
pri_message(" === Dropping Packet ===\n");
printf(" === Dropping Packet ===\n");
return 0;
}
#endif
@@ -77,12 +76,13 @@ static int q921_transmit(struct pri *pri, q921_h *h, int len) {
/* Write an extra two bytes for the FCS */
res = write(pri->fd, h, len + 2);
if (res != (len + 2)) {
pri_error("Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
fprintf(stderr, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
return -1;
}
return 0;
}
static void q921_send_ua(struct pri *pri, int pfbit)
{
q921_h h;
@@ -99,28 +99,24 @@ static void q921_send_ua(struct pri *pri, int pfbit)
h.h.c_r = 1;
break;
default:
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
return;
}
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("Sending Unnumbered Acknowledgement\n");
printf("Sending Unnumbered Acknowledgement\n");
q921_transmit(pri, &h, 3);
}
static void q921_send_sabme_now(void *vpri);
static void q921_send_sabme(void *vpri, int now)
static void q921_send_sabme(void *vpri)
{
struct pri *pri = vpri;
q921_h h;
pri_schedule_del(pri, pri->sabme_timer);
pri->sabme_timer = 0;
pri->sabme_timer = pri_schedule_event(pri, T_200, q921_send_sabme_now, pri);
if (!now)
return;
pri->sabme_timer = pri_schedule_event(pri, T_200, q921_send_sabme, pri);
Q921_INIT(h);
h.u.m3 = 3; /* M3 = 3 */
h.u.m2 = 3; /* M2 = 3 */
h.u.m3 = 3; /* M3 = 3 */
h.u.m2 = 3; /* M2 = 3 */
h.u.p_f = 1; /* Poll bit set */
h.u.ft = Q921_FRAMETYPE_U;
switch(pri->localtype) {
@@ -131,18 +127,14 @@ static void q921_send_sabme(void *vpri, int now)
h.h.c_r = 0;
break;
default:
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
return;
}
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("Sending Set Asynchronous Balanced Mode Extended\n");
printf("Sending Set Asynchronous Balanced Mode Extended\n");
q921_transmit(pri, &h, 3);
pri->q921_state = Q921_AWAITING_ESTABLISH;
}
static void q921_send_sabme_now(void *vpri)
{
q921_send_sabme(vpri, 1);
}
static int q921_ack_packet(struct pri *pri, int num)
@@ -151,22 +143,16 @@ static int q921_ack_packet(struct pri *pri, int num)
f = pri->txqueue;
while(f) {
if (f->h.n_s == num) {
/* Cancel each packet as necessary */
/* That's our packet */
if (prev)
prev->next = f->next;
else
pri->txqueue = f->next;
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- ACKing packet %d, new txqueue is %d (-1 means empty)\n", f->h.n_s, pri->txqueue ? pri->txqueue->h.n_s : -1);
/* Update v_a */
pri->v_a = num;
free(f);
/* Reset retransmission counter if we actually acked something */
pri->retrans = 0;
return 1;
}
prev = f;
f = f->next;
}
return 0;
@@ -183,66 +169,39 @@ static void q921_ack_rx(struct pri *pri, int ack)
for (x=pri->v_a; (x != pri->v_s) && (x != ack); Q921_INC(x));
if (x != ack) {
/* ACK was outside of our window --- ignore */
pri_error("ACK received outside of window, ignoring\n");
fprintf(stderr, "ACK received outside of window, ignoring\n");
return;
}
/* Cancel each packet as necessary */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- ACKing all packets from %d to (but not including) %d\n", pri->v_a, ack);
for (x=pri->v_a; x != ack; Q921_INC(x))
cnt += q921_ack_packet(pri, x);
if (!pri->txqueue) {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Since there was nothing left, stopping T200 counter\n");
printf("-- Since there wis nothing left, stopping T200 counter\n");
/* Something was ACK'd. Stop T200 counter */
pri_schedule_del(pri, pri->t200_timer);
pri->t200_timer = 0;
}
if (pri->t203_timer) {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Stopping T203 counter since we got an ACK\n");
printf("-- Stopping T203 counter since we got an ACK\n");
pri_schedule_del(pri, pri->t203_timer);
pri->t203_timer = 0;
}
if (pri->txqueue) {
/* Something left to transmit, Start the T200 counter again if we stopped it */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Something left to transmit (%d), restarting T200 counter\n", pri->txqueue->h.n_s);
printf("-- Something left to transmit, restarting T200 counter\n");
if (!pri->t200_timer)
pri->t200_timer = pri_schedule_event(pri, T_200, t200_expire, pri);
} else {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Nothing left, starting T203 counter\n");
printf("-- Nothing left, starting T203 counter\n");
/* Nothing to transmit, start the T203 counter instead */
pri->t203_timer = pri_schedule_event(pri, T_203, t203_expire, pri);
}
}
static void q921_reject(struct pri *pri)
{
q921_h h;
Q921_INIT(h);
h.s.x0 = 0; /* Always 0 */
h.s.ss = 2; /* Reject */
h.s.ft = 1; /* Frametype (01) */
h.s.n_r = pri->v_r; /* Where to start retransmission */
h.s.p_f = 1; /* XXX Should it always be set to 1? XXX */
switch(pri->localtype) {
case PRI_NETWORK:
h.h.c_r = 0;
break;
case PRI_CPE:
h.h.c_r = 1;
break;
default:
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
return;
}
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("Sending Reject (%d)\n", pri->v_r);
q921_transmit(pri, &h, 4);
}
static void q921_rr(struct pri *pri, int pbit) {
q921_h h;
Q921_INIT(h);
@@ -259,12 +218,12 @@ static void q921_rr(struct pri *pri, int pbit) {
h.h.c_r = 1;
break;
default:
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
return;
}
pri->v_na = pri->v_r; /* Make a note that we've already acked this */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("Sending Receiver Ready (%d)\n", pri->v_r);
printf("Sending Receiver Ready (%d)\n", pri->v_r);
q921_transmit(pri, &h, 4);
}
@@ -276,7 +235,7 @@ static void t200_expire(void *vpri)
if (pri->txqueue) {
/* Retransmit first packet in the queue, setting the poll bit */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- T200 counter expired, What to do...\n");
printf("-- T200 counter expired, What to do...\n");
/* Force Poll bit */
pri->txqueue->h.p_f = 1;
/* Update nr */
@@ -288,22 +247,22 @@ static void t200_expire(void *vpri)
if (pri->retrans < N_200) {
/* Reschedule t200_timer */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Retransmitting %d bytes\n", pri->txqueue->len);
printf("-- Retransmitting %d bytes\n", pri->txqueue->len);
q921_transmit(pri, (q921_h *)&pri->txqueue->h, pri->txqueue->len);
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Rescheduling retransmission (%d)\n", pri->retrans);
printf("-- Rescheduling retransmission (%d)\n", pri->retrans);
pri->t200_timer = pri_schedule_event(pri, T_200, t200_expire, pri);
} else {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Timeout occured, restarting PRI\n");
printf("-- Timeout occured, restarting PRI\n");
pri->state = Q921_LINK_CONNECTION_RELEASED;
pri->t200_timer = 0;
q921_dchannel_down(pri);
q921_start(pri, 1);
q921_start(pri);
pri->schedev = 1;
}
} else {
pri_error("T200 counter expired, nothing to send...\n");
fprintf(stderr, "T200 counter expired, nothing to send...\n");
pri->t200_timer = 0;
}
}
@@ -313,7 +272,6 @@ int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
q921_frame *f, *prev=NULL;
for (f=pri->txqueue; f; f = f->next) prev = f;
f = malloc(sizeof(q921_frame) + len + 2);
memset(f,0,sizeof(q921_frame) + len + 2);
if (f) {
Q921_INIT(f->h);
switch(pri->localtype) {
@@ -348,20 +306,20 @@ int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
q921_transmit(pri, (q921_h *)(&f->h), f->len);
if (pri->t203_timer) {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("Stopping T_203 timer\n");
printf("Stopping T_203 timer\n");
pri_schedule_del(pri, pri->t203_timer);
pri->t203_timer = 0;
}
if (!pri->t200_timer) {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("Starting T_200 timer\n");
printf("Starting T_200 timer\n");
pri->t200_timer = pri_schedule_event(pri, T_200, t200_expire, pri);
} else
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("T_200 timer already going (%d)\n", pri->t200_timer);
printf("T_200 timer already going (%d)\n", pri->t200_timer);
} else {
pri_error("!! Out of memory for Q.921 transmit\n");
fprintf(stderr, "!! Out of memory for Q.921 transmit\n");
return -1;
}
return 0;
@@ -371,7 +329,7 @@ static void t203_expire(void *vpri)
{
struct pri *pri = vpri;
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("T203 counter expired, sending RR and scheduling T203 again\n");
printf("T203 counter expired, sending RR and scheduling t203 again\n");
/* Solicit an F-bit in the other's RR */
pri->solicitfbit = 1;
q921_rr(pri, 1);
@@ -407,7 +365,7 @@ static pri_event *q921_handle_iframe(struct pri *pri, q921_i *i, int len)
/* It's within our window -- send back an RR */
q921_rr(pri, 0);
} else
q921_reject(pri);
fprintf(stderr, "XXX I need to reject (expected %d, got %d) XXX\n", pri->v_r, i->n_s);
#if 0
q931_reject(pri);
#endif
@@ -418,127 +376,80 @@ static pri_event *q921_handle_iframe(struct pri *pri, q921_i *i, int len)
void q921_dump(q921_h *h, int len, int showraw, int txrx)
{
int x;
char *type;
char direction_tag;
direction_tag = txrx ? '>' : '<';
if (showraw) {
pri_message("\n%c [", direction_tag);
printf("%c [", (txrx ? '>' : '<'));
for (x=0;x<len;x++)
pri_message("%02x ",h->raw[x]);
pri_message("]");
printf("%02x ",h->raw[x]);
printf("]\n");
}
switch (h->h.data[0] & Q921_FRAMETYPE_MASK) {
switch(h->h.data[0] & Q921_FRAMETYPE_MASK) {
case 0:
case 2:
pri_message("\n%c Informational frame:\n", direction_tag);
printf("\n Informational Frame:\n");
break;
case 1:
pri_message("\n%c Supervisory frame:\n", direction_tag);
printf("\n Supervisory frame:\n");
break;
case 3:
pri_message("\n%c Unnumbered frame:\n", direction_tag);
printf("\n Unnumbered frame:\n");
break;
}
pri_message(
printf(
"%c SAPI: %02d C/R: %d EA: %d\n"
"%c TEI: %03d EA: %d\n",
direction_tag,
(txrx ? '>' : '<'),
h->h.sapi,
h->h.c_r,
h->h.ea1,
direction_tag,
(txrx ? '>' : '<'),
h->h.tei,
h->h.ea2);
switch (h->h.data[0] & Q921_FRAMETYPE_MASK) {
switch(h->h.data[0] & Q921_FRAMETYPE_MASK) {
case 0:
case 2:
/* Informational frame */
pri_message(
printf(
"%c N(S): %03d 0: %d\n"
"%c N(R): %03d P: %d\n"
"%c %d bytes of data\n",
direction_tag,
(txrx ? '>' : '<'),
h->i.n_s,
h->i.ft,
direction_tag,
(txrx ? '>' : '<'),
h->i.n_r,
h->i.p_f,
direction_tag,
(txrx ? '>' : '<'),
len - 4);
break;
case 1:
/* Supervisory frame */
type = "???";
switch (h->s.ss) {
case 0:
type = "RR (receive ready)";
break;
case 1:
type = "RNR (receive not ready)";
break;
case 2:
type = "REJ (reject)";
break;
}
pri_message(
"%c Zero: %d S: %d 01: %d [ %s ]\n"
"%c N(R): %03d P/F: %d\n"
printf(
"%c Zeros: %d S: %d 01: %d\n"
"%c N(R): %03d P/F: %d\n"
"%c %d bytes of data\n",
direction_tag,
(txrx ? '>' : '<'),
h->s.x0,
h->s.ss,
h->s.ft,
type,
direction_tag,
(txrx ? '>' : '<'),
h->s.n_r,
h->s.p_f,
direction_tag,
(txrx ? '>' : '<'),
len - 4);
break;
case 3:
case 3:
/* Unnumbered frame */
type = "???";
if (h->u.ft == 3) {
switch (h->u.m3) {
case 0:
if (h->u.m2 == 3)
type = "DM (disconnect mode)";
else if (h->u.m2 == 0)
type = "UI (unnumbered information)";
break;
case 2:
if (h->u.m2 == 0)
type = "DISC (disconnect)";
break;
case 3:
if (h->u.m2 == 3)
type = "SABME (set asynchronous balanced mode extended)";
else if (h->u.m2 == 0)
type = "UA (unnumbered acknowledgement)";
break;
case 4:
if (h->u.m2 == 1)
type = "FRMR (frame reject)";
break;
case 5:
if (h->u.m2 == 3)
type = "XID (exchange identification note)";
break;
}
}
pri_message(
"%c M3: %d P/F: %d M2: %d 11: %d [ %s ]\n"
printf(
"%c M3: %d P/F: %d M2: %d 11: %d\n"
"%c %d bytes of data\n",
direction_tag,
(txrx ? '>' : '<'),
h->u.m3,
h->u.p_f,
h->u.m2,
h->u.ft,
type,
direction_tag,
(txrx ? '>' : '<'),
len - 3);
break;
};
@@ -598,8 +509,7 @@ void q921_reset(struct pri *pri)
pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
{
q921_frame *f;
int sendnow;
q921_frame *f;
/* Discard FCS */
len -= 2;
@@ -622,23 +532,23 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
case 0:
case 2:
if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
pri_error("!! Got I-frame while link state %d\n", pri->q921_state);
fprintf(stderr, "!! Got I-frame while link state %d\n", pri->q921_state);
return NULL;
}
/* Informational frame */
if (len < 4) {
pri_error("!! Received short I-frame (expected 4, got %d)\n", len);
fprintf(stderr, "!! Received short I-frame\n");
break;
}
return q921_handle_iframe(pri, &h->i, len);
break;
case 1:
if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
pri_error("!! Got S-frame while link down\n");
fprintf(stderr, "!! Got S-frame while link down\n");
return NULL;
}
if (len < 4) {
pri_error("!! Received short S-frame (expected 4, got %d)\n", len);
fprintf(stderr, "!! Received short S-frame\n");
break;
}
switch(h->s.ss) {
@@ -651,10 +561,10 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
/* If it's a p/f one then send back a RR in return with the p/f bit set */
if (pri->solicitfbit) {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Got RR response to our frame\n");
printf("-- Got RR response to our frame\n");
} else {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Unsolicited RR with P/F bit, responding\n");
printf("-- Unsolicited RR with P/F bit, responding\n");
q921_rr(pri, 1);
}
pri->solicitfbit = 0;
@@ -664,38 +574,36 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
case 1:
/* Receiver not ready */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Got receiver not ready\n");
printf("-- Got receiver not ready\n");
pri->busy = 1;
break;
#endif
case 2:
/* Just retransmit */
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Got reject requesting packet %d... Retransmitting.\n", h->s.n_r);
printf("-- Got reject requesting packet %d... Retransmitting.\n", h->s.n_r);
if (h->s.p_f) {
/* If it has the poll bit set, send an appropriate supervisory response */
q921_rr(pri, 1);
}
sendnow = 0;
/* Resend the proper I-frame */
for(f=pri->txqueue;f;f=f->next) {
if (sendnow || (f->h.n_s == h->s.n_r)) {
/* Matches the request, or follows in our window */
sendnow = 1;
pri_error("!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n", h->s.n_r, f->h.n_s);
f->h.n_r = pri->v_r;
q921_transmit(pri, (q921_h *)(&f->h), f->len);
if (f->h.n_s == h->s.n_r) {
/* Matches the request */
break;
}
}
if (!sendnow) {
if (f) {
/* Retransmit the requested frame */
q921_transmit(pri, (q921_h *)(&f->h), f->len);
} else {
if (pri->txqueue) {
/* This should never happen */
if (!h->s.p_f || h->s.n_r) {
pri_error("!! Got reject for frame %d, but we only have others!\n", h->s.n_r);
fprintf(stderr, "!! Got reject for frame %d, but we only have others!\n", h->s.n_r);
}
} else {
/* Hrm, we have nothing to send, but have been REJ'd. Reset v_a, v_s, etc */
pri_error("!! Got reject for frame %d, but we have nothing -- resetting!\n", h->s.n_r);
pri->v_a = h->s.n_r;
pri->v_s = h->s.n_r;
/* Reset t200 timer if it was somehow going */
@@ -711,45 +619,38 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
}
break;
default:
pri_error("!! XXX Unknown Supervisory frame ss=0x%02x,pf=%02xnr=%02x vs=%02x, va=%02x XXX\n", h->s.ss, h->s.p_f, h->s.n_r,
fprintf(stderr, "!! XXX Unknown Supervisory frame ss=0x%02x,pf=%02xnr=%02x vs=%02x, va=%02x XXX\n", h->s.ss, h->s.p_f, h->s.n_r,
pri->v_s, pri->v_a);
}
break;
case 3:
if (len < 3) {
pri_error("!! Received short unnumbered frame\n");
fprintf(stderr, "!! Received short unnumbered frame\n");
break;
}
switch(h->u.m3) {
case 0:
if (h->u.m2 == 3) {
if (h->u.p_f) {
/* Section 5.7.1 says we should restart on receiving a DM response with the f-bit set to
one, but we wait T200 first */
pri_event *ev = NULL;
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Got DM Mode from peer.\n");
/* Disconnected mode, try again after T200 */
ev = q921_dchannel_down(pri);
q921_start(pri, 0);
return ev;
printf("-- Got Unconnected Mode from peer.\n");
/* Disconnected mode */
if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED)
return q921_dchannel_down(pri);
} else {
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Ignoring unsolicited DM with p/f set to 0\n");
#if 0
printf("-- DM requesting SABME, starting.\n");
/* Requesting that we start */
q921_start(pri, 0);
#endif
q921_start(pri);
}
break;
} else if (!h->u.m2) {
pri_message("XXX Unnumbered Information not implemented XXX\n");
printf("XXX Unnumbered Information not implemented XXX\n");
}
break;
case 2:
if (pri->debug & PRI_DEBUG_Q921_STATE)
pri_message("-- Got Disconnect from peer.\n");
printf("-- Got Disconnect from peer.\n");
/* Acknowledge */
q921_send_ua(pri, h->u.p_f);
return q921_dchannel_down(pri);
@@ -757,7 +658,7 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
if (h->u.m2 == 3) {
/* SABME */
if (pri->debug & PRI_DEBUG_Q921_STATE) {
pri_message("-- Got SABME from %s peer.\n", h->h.c_r ? "network" : "cpe");
printf("-- Got SABME from %s peer.\n", h->h.c_r ? "network" : "cpe");
}
if (h->h.c_r) {
pri->remotetype = PRI_NETWORK;
@@ -779,22 +680,22 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
/* It's a UA */
if (pri->q921_state == Q921_AWAITING_ESTABLISH) {
if (pri->debug & PRI_DEBUG_Q921_STATE) {
pri_message("-- Got UA from %s peer Link up.\n", h->h.c_r ? "cpe" : "network");
printf("-- Got UA from %s peer Link up.\n", h->h.c_r ? "cpe" : "network");
}
return q921_dchannel_up(pri);
} else
pri_error("!! Got a UA, but i'm in state %d\n", pri->q921_state);
fprintf(stderr, "!! Got a UA, but i'm in state %d\n", pri->q921_state);
} else
pri_error("!! Weird frame received (m3=3, m2 = %d)\n", h->u.m2);
fprintf(stderr, "!! Weird frame received (m3=3, m2 = %d)\n", h->u.m2);
break;
case 4:
pri_error("!! Frame got rejected!\n");
fprintf(stderr, "!! Frame got rejected!\n");
break;
case 5:
pri_error("!! XID frames not supported\n");
fprintf(stderr, "!! XID frames not supported\n");
break;
default:
pri_error("!! Don't know what to do with M3=%d u-frames\n", h->u.m3);
fprintf(stderr, "!! Don't know what to do with M3=%d u-frames\n", h->u.m3);
}
break;
@@ -802,14 +703,14 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
return NULL;
}
void q921_start(struct pri *pri, int now)
void q921_start(struct pri *pri)
{
if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED) {
pri_error("!! q921_start: Not in 'Link Connection Released' state\n");
fprintf(stderr, "!! q921_start: Not in 'Link Connection Released' state\n");
return;
}
/* Reset our interface */
q921_reset(pri);
/* Do the SABME XXX Maybe we should implement T_WAIT? XXX */
q921_send_sabme(pri, now);
q921_send_sabme(pri);
}

748
q931.c

File diff suppressed because it is too large Load Diff