File: tunnelManager.c

package info (click to toggle)
dcap 2.47.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,388 kB
  • ctags: 1,474
  • sloc: ansic: 14,235; makefile: 395; python: 75; sh: 58
file content (150 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *   DCAP - dCache Access Protocol client interface
 *
 *   Copyright (C) 2000,2004 DESY Hamburg DMG-Division.
 *
 *   AUTHOR: Tigran Mkrtchayn (tigran.mkrtchyan@desy.de)
 *
 *   This program can be distributed under the terms of the GNU LGPL.
 *   See the file COPYING.LIB
 *
 */
 
 
/*
 * $Id: tunnelManager.c,v 1.11 2004-11-01 19:33:30 tigran Exp $
 */

#ifndef WIN32
#   include <dlfcn.h>
#   include <unistd.h>
#else
#    include "win32_libdl.h"
#endif

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

#include "dcap.h"
#include "sysdep.h"
#include "debug_level.h"
#include "ioTunnel.h"
#include "tunnelManager.h"

typedef struct {
	int sock;
	ioTunnel *tunnel;
} tunnelPair;

static MUTEX(gLock);

static tunnelPair *tunnelMap;
static unsigned int qLen = 0; /* number of elements in the memory*/


int setTunnelPair(int sock, ioTunnel *tunnel)
{
	tunnelPair *tmp;
	m_lock(&gLock);
	
	tmp = realloc(tunnelMap, sizeof(tunnelPair)*(qLen +1));
	if(tmp == NULL) {
		m_unlock(&gLock);
		return -1;
	}
	
	tunnelMap = tmp;
	tunnelMap[qLen].sock = sock;
	tunnelMap[qLen].tunnel = tunnel;
	
	++qLen;
	
	m_unlock(&gLock);
	return 0;
}

ioTunnel * getTunnelPair(int sock)
{

	unsigned int i;
	ioTunnel *en;
	
	m_lock(&gLock);
	for(i = 0; i < qLen; i++) {
		if(tunnelMap[i].sock == sock) {		
			en =  tunnelMap[i].tunnel;
			m_unlock(&gLock);
			return en;
		}
	}
	m_unlock(&gLock);
	return NULL;
}


ioTunnel *addIoPlugin(const char *libname)
{
	void *handle;
	ioTunnel *tunnel;
	char *fullpath;

	if(libname == NULL) {
		dc_debug(DC_ERROR, "Bad tunnel name");
		return NULL;
	}

	/* magick library name, do nothing */
	if( strcmp(libname, "null") == 0 ) {
		return NULL;
	}

	handle = dlopen( libname, RTLD_NOW);
		
	if(handle == NULL) {
		fullpath = malloc(strlen(TUNNELLIBDIR) + strlen(libname) + 2);
		strcpy(fullpath, TUNNELLIBDIR);
		strcat(fullpath, "/");
		strcat(fullpath, libname);
		handle = dlopen(fullpath, RTLD_NOW);
		free(fullpath);
	}

	if(handle == NULL) {		
		goto fail;
	}

	tunnel = (ioTunnel *)malloc(sizeof(ioTunnel));
	if(tunnel == NULL) {
		dc_debug(DC_ERROR, "Failed to allocate memory for tunnel");
		dlclose(handle);
		return NULL;
	}

	
	tunnel->eRead = (ssize_t (*)(int , void *, size_t ))dlsym(handle, "eRead");
	if(tunnel->eRead == NULL) goto fail;
	
	tunnel->eWrite = (ssize_t (*)(int ,const void *, size_t ))dlsym(handle, "eWrite");
	if(tunnel->eWrite == NULL) goto fail;
	
	tunnel->eInit = (int(*)(int))dlsym(handle, "eInit");	
	if(tunnel->eInit == NULL) goto fail;
	
	tunnel->eDestroy = (int(*)(int))dlsym(handle, "eDestroy");
	if(tunnel->eDestroy == NULL) goto fail;
		

	dc_debug(DC_INFO, "Activating IO tunnel. Provider: [%s].", libname);
		
	return tunnel;
	
fail:
	dc_debug(DC_ERROR, "Failed to add IO tunnel (%s). Provider: [%s].", dlerror(), libname);

	if(handle != NULL ) {
		dlclose(handle);
	}
	return NULL;	
	
}