/*
 *   capisnoop.so - pre-loadable shared object to trace main CAPI 2.0 calls
 *   Copyright (C) 2004  Enrik Berkhan <Enrik.Berkhan@inka.de>
 *
 *   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
 */

#include <sys/types.h>
#include <linux/capi.h>
#include <capi20.h>

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

static void *libcapi20 = 0;
static int (*orig_register)(unsigned MaxLogicalConnection,
		            unsigned MaxBDataBlocks,
			    unsigned MaxBDataLen,
			    unsigned *ApplIDp); 
static unsigned (*orig_release)(unsigned ApplID);
static unsigned (*orig_put_message)(unsigned ApplID, unsigned char *Msg);
static unsigned (*orig_get_message)(unsigned ApplID, unsigned char **Buf);

void init() __attribute__((constructor));
void fini() __attribute__((destructor));

static FILE *
getout() {
	return stdout;
}

void
init()
{
	char *error;
	char *capiname;

	fprintf(stderr, "CAPISNOOP: installed for %d\n", getpid());

	setlinebuf(stdout);

	capiname = getenv("CAPINAME");
	if (!capiname)
		capiname = "libcapi20.so.2";

	libcapi20 = dlopen(capiname, RTLD_LAZY|RTLD_GLOBAL);
	if (!libcapi20) {
		fprintf(stderr, "%s\n", dlerror());
		exit(1);
	}
	dlerror();
	*(void **)(&orig_register) = dlsym(libcapi20, "capi20_register");
	if ((error = dlerror())) {
		fprintf(stderr, "%s\n", dlerror());
		exit(1);
	}
	*(void **)(&orig_release) = dlsym(libcapi20, "capi20_release");
	if ((error = dlerror())) {
		fprintf(stderr, "%s\n", dlerror());
		exit(1);
	}
	*(void **)(&orig_put_message) = dlsym(libcapi20, "capi20_put_message");
	if ((error = dlerror())) {
		fprintf(stderr, "%s\n", dlerror());
		exit(1);
	}
	*(void **)(&orig_get_message) = dlsym(libcapi20, "capi20_get_message");
	if ((error = dlerror())) {
		fprintf(stderr, "%s\n", dlerror());
		exit(1);
	}
}

void
fini()
{
	if (libcapi20)
		dlclose(libcapi20);
}

unsigned
capi20_register(unsigned MaxLogicalConnection,
		         unsigned MaxBDataBlocks,
			 unsigned MaxBDataLen,
			 unsigned *ApplIDp)
{
	unsigned ret = orig_register(MaxLogicalConnection, MaxBDataBlocks,
			             MaxBDataLen, ApplIDp);
	fprintf(getout(), "CAPISNOOP: capi20_register(%d, %d, %d, %d) = %d\n",
	        MaxLogicalConnection, MaxBDataBlocks,
		MaxBDataLen, *ApplIDp, ret);
	return ret;
}

unsigned
capi20_release(unsigned ApplID)
{
	unsigned ret = orig_release(ApplID);
	fprintf(getout(), "CAPISNOOP: capi20_release(%d) = %d\n", ApplID, ret);
	return ret;
}

unsigned
capi20_put_message (unsigned ApplID, unsigned char *Msg)
{
	unsigned ret = orig_put_message (ApplID, Msg);
	fprintf(getout(), "CAPISNOOP: capi20_put_message(%d,\n%s\n) = %d\n",
	        ApplID, capi20_message2str(Msg), ret);
	return ret;
}

unsigned
capi20_get_message (unsigned ApplID, unsigned char **Buf)
{
	unsigned ret = orig_get_message (ApplID, Buf);
	if (ret != 0x1104)
	fprintf(getout(), "CAPISNOOP: capi20_get_message(%d,\n%s\n) = %d\n",
                ApplID, capi20_message2str(*Buf), ret);
        return ret;
}
