-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsample-memcache.c
197 lines (169 loc) · 4.58 KB
/
sample-memcache.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <linux/kernel.h>
#include <libmemcached/memcached.h>
#include <mysql/mysql.h>
/*
Globales, para las pruebas de Memcache y Memcache
*/
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
char *key= "keystring";
char *value= "keyvalue";
MYSQL *con;
/*
:::: Funcion callback :::
================
Es invocada cada vez que hay un paquete en la cola
*/
static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data)
{
u_int32_t id;
struct nfqnl_msg_packet_hdr *ph;
struct nfqnl_msg_packet_hw *hwph;
unsigned char *nf_packet;
char saddr[2048];
int ret;
char *valor_memcache;
size_t len_memcache;
char *key_memcache="autorizada";
uint32_t flags;
// Obtenemos las headers:
ph = nfq_get_msg_packet_hdr(nfa);
// Y el ID de paquete, que hace falta para el veredicto final:
id = ntohl(ph->packet_id);
// Obtenemos la IP origen del paquete:
// - Primero hay que obtener el paquete en si:
ret = nfq_get_payload(nfa, &nf_packet);
if ((ret <= 0))
{
printf("Error, no hay paquete que recibir - wtf \n");
return;
}
struct iphdr *iph = ((struct iphdr *) nf_packet);
inet_ntop(AF_INET, &(iph->saddr), saddr, sizeof(saddr));
// fprintf(stdout,"Recibido con origen%s\n",saddr);
valor_memcache = memcached_get(memc, key_memcache, strlen(key_memcache), &len_memcache, &flags, &rc);
if (rc == MEMCACHED_SUCCESS)
{
if (strcmp(saddr,valor_memcache) == 0)
{
free(valor_memcache);
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
else
{
free(valor_memcache);
return nfq_set_verdict(qh, id, NF_DROP, 0, NULL);
}
}
else
{
printf("Key %s no se ha podido encontrar en memcached\n",key_memcache);
return nfq_set_verdict(qh, id, NF_DROP, 0, NULL);
}
}
int main(int argc, char **argv)
{
struct nfq_handle *h;
struct nfq_q_handle *qh;
int fd;
int rv;
char buf[4096] __attribute__ ((aligned));
printf("Obteniendo el handle de la libreria: ");
h = nfq_open();
if (!h)
{
fprintf(stderr, "Ha fallado\n");
exit(1);
}
else printf(" OK !\n");
printf("Haciendo unbind (por si existe alguno de AF_INET): ");
if (nfq_unbind_pf(h, AF_INET) < 0)
{
fprintf(stderr, "error nfq_unbind_pf()\n");
exit(1);
}
else printf(" OK!\n");
printf("Vinculando nfnetlink_queue de tipo nf_queue handler para AF_INET:");
if (nfq_bind_pf(h, AF_INET) < 0)
{
fprintf(stderr, "error nfq_bind_pf()\n");
exit(1);
}
else printf(" OK!\n");
printf("Creando la vinculacion de la funcion callback con Queue 0, socket receptor: ");
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
else printf(" OK !\n");
printf("Definiendo que cantidad de paquete queremos recibir (no queremos todo para estas pruebas): ");
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "FALLO el COPY_META mode\n");
exit(1);
}
else printf("OK\n");
fd = nfq_fd(h);
printf("Realizando conexión a memcache: ");
memc= memcached_create(NULL);
servers= memcached_server_list_append(servers, "localhost", 11211, &rc);
rc= memcached_server_push(memc, servers);
if (rc == MEMCACHED_SUCCESS)
printf(" OK ! \n");
else {
printf("error conectando a memcache: %s\n",memcached_strerror(memc, rc));
exit(0);
}
printf("Realizando INIT de MySQL: ");
con = mysql_init(NULL);
if (con == NULL)
{
printf(" FAIL\n");
exit(1);
}
else printf("OK\n");
printf("Realizando Conexion a MYQSL: ");
if (mysql_real_connect(con, "localhost", "root", "FIXMEPASSWORD", "nfqueue", 0, NULL, 0) == NULL)
{
printf(" FAILED\n");
exit(1);
}
else printf(" OKI\n");
printf("Todo Listo !\n Entrando en bucle principal de recepcion ..\n");
while ((rv = recv(fd, buf, sizeof(buf), 0)))
{
// Si todo ha ido bien, gestionamos el paquete:
if (rv>=0)
nfq_handle_packet(h, buf, rv);
// es posible que tengamos packet loss porque
// nuestro codigo es lento y se llena la queue:
else if ( errno == ENOBUFS)
{
fflush(stdout);
printf("!");
}
// O "simplemente", que algo haya ido mal:
else {
printf("ERROR \n");
fflush(stdout);
break;
}
}
// Teoricamente, nunca llegaremos aqui, pero si llegamos
// Habra que liberar bien y tal:
printf("unbinding de queue 0\n");
nfq_destroy_queue(qh);
printf("cerrando library handle\n");
nfq_close(h);
exit(0);
}