File: pfq_socket.c

package info (click to toggle)
pfqueue 0.5.6-8
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 1,396 kB
  • ctags: 628
  • sloc: sh: 8,904; ansic: 3,877; makefile: 77
file content (423 lines) | stat: -rwxr-xr-x 7,822 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>

#include "pfq_backend.h"
#include "pfq_service.h"
#include "../pfqmessage.h"
#include "../config.h"
#include "../pfqtcp.h"

#define PF_VERSION_20	1
#define PF_VERSION_21	2
#define PF_VERSION_22	3

pthread_mutex_t		socket_mutex = PTHREAD_MUTEX_INITIALIZER;

int CURQ;

int NUMMSG_THREAD;

struct sockaddr_in svra;
struct hostent *svr;
int    sock;

struct pfb_conf_t pfb_conf;

void strip_nl(char* b, int l) {
	int i;
	for ( i=0; i<l; i++ ) {
		if (*(b+i)=='\n')
			*(b+i) = 0;
	}
}

int w_socket ( int s, const char* b ) {
	write ( s, b, strlen(b) );
	return 0;
}

int r_socket ( int s, char *b, size_t l ) {
	int rd;
	memset ( b, 0, l );
	rd = read ( s, b, l );
	if ( rd>0 )
		strip_nl(b, l);
	else
		return -2;
	if ( !strncmp( b, "ERR", 3 ) )
		return -1;
	return 0;
}

int wr_socket ( int s, char *b, size_t l ) {
	int res;
	pthread_mutex_lock(&socket_mutex);
	w_socket ( s, b );
	res = r_socket ( s, b, l );
	pthread_mutex_unlock(&socket_mutex);
	return res;
}

const char* pfb_id() {
	return "socket";
}

int pfb_apiversion() {
	return PFQ_API_VERSION;
}

const char* pfb_version() {
	return "1.0";
}

struct pfb_conf_t *pfb_getconf() {
	return &pfb_conf;
}

struct msg_t* msg_from_id(const char* mid) {
int i;
	for ( i=0; i<NUMMSG_THREAD; i++ ) {
		if ( !strncmp(ext_queue[i].id, mid, sizeof(ext_queue[i].id) ) )
			return &ext_queue[i];
	}
	return NULL;
}

int pfb_init() {
	pfb_conf.max_char = 200;
	strcpy ( pfb_conf.command_path, "" );
	strcpy ( pfb_conf.config_path, "" );
	pfb_conf.port = 20000;
	return PFBE_OK;
}

int pfb_setup( struct msg_t *qptr1, struct be_msg_t *qptr2 ) {

	sock = socket(AF_INET, SOCK_STREAM, 0 );
	if ( sock<0 )
		return PFBE_UNUSABLE;
	svr = gethostbyname ( pfb_conf.host );
	if ( svr == NULL )
		return PFBE_UNUSABLE;
	memset ( &svra, 0, sizeof(svra) );
	svra.sin_family = AF_INET;

	memcpy ( (struct sockaddr*)&svra.sin_addr.s_addr, 
		(struct hostent*)svr->h_addr, 
		(struct hostent*)svr->h_length );
	svra.sin_port = htons( pfb_conf.port );

	if ( connect(sock, (struct sockaddr*)&svra, sizeof(svra)) <0 )
		return PFBE_UNUSABLE;

	ext_queue = qptr1;
	my_queue  = qptr2;

	pthread_mutex_unlock(&socket_mutex);
	return PFBE_OK;
}

int pfb_close() {
	w_socket ( sock, "QUIT\n" );
	shutdown ( sock, SHUT_RDWR );
	return PFBE_OK;
}

int pfb_retr_headers( const char* msgid ) {
	int res;
	struct msg_t *msg;

	msg = msg_from_id(msgid);
	if ( msg && msg->hcached )
		return PFBE_OK;

	res = pfb_retr_to(msgid);
	res|= pfb_retr_from(msgid);
	res|= pfb_retr_subj(msgid);
	res|= pfb_retr_path(msgid);
	
	res = 0;
	if ( res == PFBE_OK )
		msg->hcached = 1;
	else
		msg->hcached = 0;

	return PFBE_OK;
}

int pfb_retr_id( int n, char* b, size_t len ) {
	char buf[BUF_SIZE];
	int res;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %d\n", CMD_MSGID, n );
	res = wr_socket ( sock, buf, sizeof(buf) );

	if ( res )
		strncpy ( b, PFBE_SERROR, len );
	else
		strncpy ( b, buf+CMD_REPLY_LEN, len );

	return PFBE_OK;
}

int pfb_retr_path( const char* msgid ) {
	char buf[BUF_SIZE];
	int res;
	struct msg_t *msg;

	msg = msg_from_id(msgid);
	if ( !msg )
		return PFBE_ERROR;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %s\n", CMD_PATH, msgid );
	res = wr_socket ( sock, buf, sizeof(buf) );
	
	if ( res )
		strcpy ( msg->path, PFBE_SERROR );
	else
		strcpy ( msg->path, buf+CMD_REPLY_LEN );

	return PFBE_OK;
}

int pfb_retr_from( const char* msgid ) {
	char buf[BUF_SIZE];
	int res;
	struct msg_t *msg;

	msg = msg_from_id(msgid);
	if ( !msg )
		return PFBE_ERROR;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %s\n", CMD_FROM, msgid );
	res = wr_socket ( sock, buf, sizeof(buf) );
	
	if ( res )
		strcpy ( msg->from, PFBE_SERROR );
	else
		strcpy ( msg->from, buf+CMD_REPLY_LEN );

	return PFBE_OK;
}

int pfb_retr_to( const char* msgid ) {
	char buf[BUF_SIZE];
	int res;
	struct msg_t *msg;

	msg = msg_from_id(msgid);
	if ( !msg )
		return PFBE_ERROR;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %s\n", CMD_TO, msgid );
	res = wr_socket ( sock, buf, sizeof(buf) );
	
	if ( res )
		strcpy ( msg->to, PFBE_SERROR );
	else
		strcpy ( msg->to, buf+CMD_REPLY_LEN );

	return PFBE_OK;

}

int pfb_retr_subj( const char* msgid ) {
	char buf[BUF_SIZE];
	int res;
	struct msg_t *msg;

	msg = msg_from_id(msgid);
	if ( !msg )
		return PFBE_ERROR;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %s\n", CMD_SUBJ, msgid );
	res = wr_socket ( sock, buf, sizeof(buf) );
	
	if ( res )
		strcpy ( msg->subj, PFBE_SERROR );
	else
		strcpy ( msg->subj, buf+CMD_REPLY_LEN );

	return PFBE_OK;
}

int pfb_retr_status ( const char* msgid ) {
	char buf[BUF_SIZE];
	int res;
	struct msg_t *msg;

	msg = msg_from_id(msgid);
	if ( !msg )
		return PFBE_ERROR;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %s\n", CMD_STATUS, msgid );
	res = wr_socket ( sock, buf, sizeof(buf) );
	
	if ( res )
		strcpy ( msg->stat, PFBE_SERROR );
	else
		strcpy ( msg->stat, buf+CMD_REPLY_LEN );

	return PFBE_OK;
}

int pfb_retr_body( const char* msgid, char *buffer, size_t buflen ) {
	int res;
	struct msg_t *msg;
	char *buf2;

	buf2 = (char*)malloc(buflen);

	msg = msg_from_id(msgid);
	if ( !msg )
		return PFBE_ERROR;
	
	memset ( buf2, 0, buflen );
	sprintf ( buf2, "%s %s\n", CMD_BODY, msgid );
	res = wr_socket ( sock, buf2, buflen );

	sprintf ( buffer, "%s\n", buf2+CMD_REPLY_LEN+7 );

	free ( buf2 );

	return strlen(buf2);
}

int pfb_num_msg() {
	char buf[BUF_SIZE];
	int res;
	
	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s\n", CMD_NUMMSG );
	res = wr_socket ( sock, buf, sizeof(buf) );

	if ( res )
		return 0;
	else
		return ( atoi(buf+CMD_REPLY_LEN) );
}

int pfb_queue_count() {
	char buf[BUF_SIZE];
	int res;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s\n", CMD_NUMQ );
	res = wr_socket ( sock, buf, sizeof(buf) );
	printf ( "res: %d\n", res );
	
	if ( res )
		return 0;
	else
		return atoi(buf+CMD_REPLY_LEN);
}

char* pfb_queue_name(int q) {
	static char buf[BUF_SIZE];
	int res;

	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %d\n", CMD_QNAME, q );
	res = wr_socket ( sock, buf, sizeof(buf) );
	
	if ( res )
		return 0;
	else
		return buf+CMD_REPLY_LEN;
}

int pfb_fill_queue() {
	char buf[255];
	int i, j;
	struct be_msg_t *msg;

	j = pfb_num_msg();
	for ( i=0; i<j; i++ ) {
		msg = &my_queue[i];
		pfb_retr_id ( i, buf, sizeof(buf) );
		memcpy ( msg->id, buf, sizeof(msg->id) );
		msg->changed = strncmp( msg->id, ext_queue[i].id, strlen(msg->id) );
	}
	NUMMSG_THREAD = j;
	return j;
}

int pfb_action(int act, const char* msg) {
	char b[BUF_SIZE];
	char b2[BUF_SIZE];

	switch ( act ) {
	case MSG_DELETE:
		sprintf ( b, CMD_MSGDEL );
		break;
	case MSG_HOLD:
		sprintf ( b, CMD_MSGHOLD );
		break;
	case MSG_RELEASE:
		sprintf ( b, CMD_MSGREL );
		break;
	case MSG_REQUEUE:
		sprintf ( b, CMD_MSGREQ );
		break;
	default:
		return 1;
	}
	sprintf ( b2, "%s %s\n", b, msg );
	wr_socket ( sock, b2, sizeof(b2) );

	return PFBE_OK;
}

int pfb_message_delete( const char* msg ) {
	return pfb_action ( MSG_DELETE, msg );
}

int pfb_message_hold( const char* msg ) {
	return pfb_action ( MSG_HOLD, msg );
}

int pfb_message_requeue( const char* msg ) {
	return pfb_action ( MSG_REQUEUE, msg );
}

int pfb_message_release( const char* msg ) {
	return pfb_action ( MSG_RELEASE, msg );
}

int pfb_set_queue ( int q ) {
	char buf[BUF_SIZE];
	int res;
	
	memset ( buf, 0, sizeof(buf) );
	sprintf ( buf, "%s %d\n", CMD_SETQ, q );
	res = wr_socket ( sock, buf, sizeof(buf) );

	return PFBE_OK;
}

void pfb_use_envelope ( int u ) {
	pfb_using_envelope = u;
}

int pfb_get_caps() {
	return pfb_caps;
}