-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamebased.h
157 lines (123 loc) · 3.57 KB
/
namebased.h
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
151
152
153
154
155
156
157
/**
* @file namebased.h
* @brief Name-based API - experimental API extensions for name-based sockets.
* @author Javier Ubillos
*/
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <linux/net.h>
#include <linux/socket.h>
#ifdef DEBUG
#define DBG_P( ... ) (printf("%s:%d:%s() > " , __FILE__, __LINE__, __FUNCTION__), printf(__VA_ARGS__) )
#else
#define DBG_P //
#endif
#ifndef _NAMEBASED_H
#define _NAMEBASED_H
typedef struct _nameorientedcon {
int fd;
char* local_name;
char* dest_name;
u_int16_t port;
int transport;
char* service;
struct sockaddr *saddr;
struct sockaddr *daddr;
} nameorientedcon;
#define CONNECTION nameorientedcon
/**
* Allocates a "nameorientedcon" struct, all zero:ed out.
* @returns pointer to struct.
*/
#define ALLOC_SOCKET() (nameorientedcon*) calloc(1,sizeof(nameorientedcon));
/**
* Defines a local name (sets names to 1 and removes all other names)
*/
#define SET_LOCAL_NAME(conn, name) \
conn->local_name = (char*) calloc(1, strlen( name )); \
strcpy( conn->local_name, name)
#define GET_LOCAL_NAME(conn) conn->local_name
#define SET_DEST_NAME(conn, name) \
conn->dest_name = (char*) calloc(1, strlen( name )); \
strcpy( conn->dest_name, name)
#define GET_DEST_NAME(conn) conn->dest_name
#define GET_ADDRESSES(conn) conn->saddr
#define SET_TRANSPORT(conn, tp) conn->transport = tp
#define GET_SERVICE(conn) conn->service
#define SET_SERVICE(conn, srv) namebased_set_service(conn, srv)
int namebased_set_service(nameorientedcon *conn, char* srv)
{
struct servent *ret;
char* tp = 0;
setservent(0);
if(SOCK_STREAM == conn->transport) tp = "tcp";
if(SOCK_DGRAM == conn->transport) tp = "udp";
ret = getservbyname( srv, tp );
endservent();
if(ret) {
conn->service = ret->s_name;
conn->port = ret->s_port;
} else {
conn->service = 0;
conn->port = 0;
}
}
#define CONNECT(conn) namebased_connect(conn)
int namebased_connect(nameorientedcon *conn)
{
if( 0 < namestack_module_loaded() )
return namebased_connect_af_name(conn);
else
return namebased_connect_legacy(conn);
}
int namebased_connect_legacy(nameorientedcon *conn) {
char inetbuf[INET6_ADDRSTRLEN];
struct addrinfo *ainf, hints;
int fd=0, ret=0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
if( conn->transport )
hints.ai_socktype = conn->transport;
memset(&inetbuf, 0, sizeof(inetbuf));
getaddrinfo( conn->dest_name, conn->service, &hints, &ainf );
while( ainf ) {
DBG_P("socket( %d, %d, %d);\n", ainf->ai_family, conn->transport, 0);
fd = socket( ainf->ai_family, conn->transport, 0);
if(0 <= fd) {
DBG_P("socket returned fd: %d\n", fd);
inet_ntop(AF_INET, ainf->ai_addr->sa_data +2, inetbuf);
ret = connect(fd, ainf->ai_addr, ainf->ai_addrlen);
DBG_P("connect returned: %d\n", ret);
if(0 <= ret) {
conn->daddr = ainf->ai_addr;
conn->fd = fd;
break;
}
}
ainf = ainf->ai_next;
DBG_P("Next ainf\n");
}
}
int namebased_connect_af_name() {
perror("AF_NAME not yet implemented");
}
#define SEND(conn, data, size) write(conn->fd, data, size)
#define CLOSE(conn) close(conn->fd); conn->fd = 0;
#define NAMESTACK_MODULE_NAME "namestack"
int namestack_module_loaded(void) {
int i;
FILE* f;
char line[256], mod_name[256];
if ((f = fopen("/proc/modules", "r")) >= 0 ) {
while (( fgets(line, 256, f)) > 0) {
sscanf(line, "%s", mod_name );
if ( 0 == strcmp(NAMESTACK_MODULE_NAME, mod_name) )
return 1;
}
fclose(f);
return 0;
}
return -1; // Could not open /proc/modules
}
#endif