File: radius.c

package info (click to toggle)
php-radius 1.4.0~b1-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 480 kB
  • sloc: ansic: 3,010; php: 897; xml: 382; makefile: 7
file content (862 lines) | stat: -rw-r--r-- 19,792 bytes parent folder | download | duplicates (2)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
/*
Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

1. Redistributions of source code must retain the above copyright 
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 
   notice, this list of conditions and the following disclaimer in the 
   documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products 
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This code cannot simply be copied and put under the GNU Public License or 
any other GPL-like (LGPL, GPL2) License.

    $Id$
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "php_network.h"
#include "ext/standard/info.h"
#include "php_radius.h"
#include "radlib.h"
#include "radlib_private.h"

#ifndef PHP_WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include "pecl-compat/compat.h"

void _radius_close(zend_resource *res TSRMLS_DC);

static int _init_options(struct rad_attr_options *out, int options, int tag);

#define RADIUS_FETCH_RESOURCE(radh, zv) \
	radh = (struct rad_handle *)compat_zend_fetch_resource(zv, "rad_handle", le_radius TSRMLS_CC); \
	if (!radh) { \
		RETURN_FALSE; \
	}

/* If you declare any globals in php_radius.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(radius)
*/

/* True global resources - no need for thread safety here */
static int le_radius;

/* {{{ radius_functions[]
 *
 * Every user visible function must have an entry in radius_functions[].
 */
zend_function_entry radius_functions[] = {
	PHP_FE(radius_auth_open,    NULL)
	PHP_FE(radius_acct_open,    NULL)
	PHP_FE(radius_close,        NULL)
	PHP_FE(radius_strerror,     NULL)
	PHP_FE(radius_config,       NULL)
	PHP_FE(radius_add_server,	NULL)
	PHP_FE(radius_create_request,	NULL)
	PHP_FE(radius_put_string,	NULL)
	PHP_FE(radius_put_int,	NULL)
	PHP_FE(radius_put_attr,	NULL)
	PHP_FE(radius_put_addr,	NULL)
	PHP_FE(radius_put_vendor_string,	NULL)
	PHP_FE(radius_put_vendor_int,	NULL)
	PHP_FE(radius_put_vendor_attr,	NULL)
	PHP_FE(radius_put_vendor_addr,	NULL)
	PHP_FE(radius_send_request,	NULL)
	PHP_FE(radius_get_attr,	NULL)
	PHP_FE(radius_get_tagged_attr_data, NULL)
	PHP_FE(radius_get_tagged_attr_tag, NULL)
	PHP_FE(radius_get_vendor_attr,	NULL)
	PHP_FE(radius_cvt_addr,	NULL)
	PHP_FE(radius_cvt_int,	NULL)
	PHP_FE(radius_cvt_string,	NULL)
	PHP_FE(radius_salt_encrypt_attr,	NULL)
	PHP_FE(radius_request_authenticator,	NULL)
	PHP_FE(radius_server_secret,	NULL)
	PHP_FE(radius_demangle,	NULL)    
	PHP_FE(radius_demangle_mppe_key,	NULL)    
	{NULL, NULL, NULL}	/* Must be the last line in radius_functions[] */
};
/* }}} */

/* {{{ radius_module_entry
 */
zend_module_entry radius_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"radius",
	radius_functions,
	PHP_MINIT(radius),
	PHP_MSHUTDOWN(radius),
	NULL,
	NULL,
	PHP_MINFO(radius),
#if ZEND_MODULE_API_NO >= 20010901
	PHP_RADIUS_VERSION,
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_RADIUS
ZEND_GET_MODULE(radius)
#endif

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(radius)
{
	le_radius = zend_register_list_destructors_ex(_radius_close, NULL, "rad_handle", module_number);
#include "radius_init_const.h"
	REGISTER_LONG_CONSTANT("RADIUS_MPPE_KEY_LEN", MPPE_KEY_LEN, CONST_PERSISTENT);    
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(radius)
{
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(radius)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "radius support", "enabled");
	php_info_print_table_row(2, "version", PHP_RADIUS_VERSION);
	php_info_print_table_end();
}
/* }}} */

/* {{{ proto resource radius_auth_open(string arg) */
PHP_FUNCTION(radius_auth_open)
{
	struct rad_handle *radh = rad_auth_open();

	if (radh != NULL) {
		compat_zend_register_resource(return_value, radh, le_radius TSRMLS_CC);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto resource radius_acct_open(string arg) */
PHP_FUNCTION(radius_acct_open)
{
	struct rad_handle *radh = rad_acct_open();

	if (radh != NULL) {
		compat_zend_register_resource(return_value, radh, le_radius TSRMLS_CC);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool radius_close(radh) */
PHP_FUNCTION(radius_close)
{
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_radh) == FAILURE) {
		return;
	}

	/* Fetch the resource to verify it. */
	RADIUS_FETCH_RESOURCE(radh, z_radh);
	compat_zend_delete_resource(z_radh TSRMLS_CC);
	RETURN_TRUE;
}
/* }}} */

/* {{{ proto string radius_strerror(radh) */
PHP_FUNCTION(radius_strerror)
{
	char *msg;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_radh) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);
	msg = (char *)rad_strerror(radh);
	RETURN_STRINGL(msg, strlen(msg));
}
/* }}} */

/* {{{ proto bool radius_config(desc, configfile) */
PHP_FUNCTION(radius_config)
{
	char *filename;
	COMPAT_ARG_SIZE_T filename_len;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_radh, &filename, &filename_len) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (rad_config(radh, filename) == -1) {
		RETURN_FALSE;
	} else {
		RETURN_TRUE;
	}
}
/* }}} */

/* {{{ proto bool radius_add_server(desc, hostname, port, secret, timeout, maxtries) */
PHP_FUNCTION(radius_add_server)
{
	char *hostname, *secret;
	COMPAT_ARG_SIZE_T hostname_len, secret_len;
	long  port, timeout, maxtries;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rslsll", &z_radh,
		&hostname, &hostname_len,
		&port,
		&secret, &secret_len,
		&timeout, &maxtries) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (rad_add_server(radh, hostname, port, secret, timeout, maxtries) == -1) {
		RETURN_FALSE;
	} else {
		RETURN_TRUE;
	}
}
/* }}} */

/* {{{ proto bool radius_create_request(desc, code) */
PHP_FUNCTION(radius_create_request)
{
	long code;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &z_radh, &code) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (rad_create_request(radh, code) == -1) {
		RETURN_FALSE;
	} else {
		RETURN_TRUE;
	}
}
/* }}} */

/* {{{ proto bool radius_put_string(desc, type, str, options, tag) */
PHP_FUNCTION(radius_put_string)
{
	char *str;
	COMPAT_ARG_SIZE_T str_len;
	long type, options = 0, tag = 0;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls|ll", &z_radh, &type, &str, &str_len, &options, &tag)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_string(radh, type, str, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_put_int(desc, type, int, options, tag) */
PHP_FUNCTION(radius_put_int)
{
	long type, val, options = 0, tag = 0;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll|ll", &z_radh, &type, &val, &options, &tag)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_int(radh, type, val, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_put_attr(desc, type, data, options, tag) */
PHP_FUNCTION(radius_put_attr)
{
	long type, options = 0, tag = 0;
	COMPAT_ARG_SIZE_T len;
	char *data;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls|ll", &z_radh, &type, &data, &len, &options, &tag)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_attr(radh, type, data, len, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;

}
/* }}} */

/* {{{ proto bool radius_put_addr(desc, type, addr, options, tag) */
PHP_FUNCTION(radius_put_addr)
{
	COMPAT_ARG_SIZE_T addrlen;
	long type, options = 0, tag = 0;
	char	*addr;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;
	struct in_addr intern_addr;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls|ll", &z_radh, &type, &addr, &addrlen, &options, &tag)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (inet_aton(addr, &intern_addr) == 0) {
		zend_error(E_ERROR, "Error converting Address");
		RETURN_FALSE;
	}

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_addr(radh, type, intern_addr, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_put_vendor_string(desc, vendor, type, str, options, tag) */
PHP_FUNCTION(radius_put_vendor_string)
{
	char *str;
	COMPAT_ARG_SIZE_T str_len;
	long type, vendor, options = 0, tag = 0;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls|ll", &z_radh, &vendor, &type, &str, &str_len, &options, &tag)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_vendor_string(radh, vendor, type, str, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_put_vendor_int(desc, vendor, type, int, options, tag) */
PHP_FUNCTION(radius_put_vendor_int)
{
	long type, vendor, val, options = 0, tag = 0;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll|ll", &z_radh, &vendor, &type, &val, &options, &tag)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_vendor_int(radh, vendor, type, val, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_put_vendor_attr(desc, vendor, type, data, options, tag) */
PHP_FUNCTION(radius_put_vendor_attr)
{
	long type, vendor, options = 0, tag = 0;
	COMPAT_ARG_SIZE_T len;
	char *data;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls|ll", &z_radh, &vendor, &type,
		&data, &len, &options, &tag) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_vendor_attr(radh, vendor, type, data, len, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_put_vendor_addr(desc, vendor, type, addr) */
PHP_FUNCTION(radius_put_vendor_addr)
{
	long type, vendor, options = 0, tag = 0;
	COMPAT_ARG_SIZE_T addrlen;
	char	*addr;
	struct rad_attr_options attr_options;
	struct rad_handle *radh;
	zval *z_radh;
	struct in_addr intern_addr;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls|ll", &z_radh, &vendor,
		&type, &addr, &addrlen, &options, &tag) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (inet_aton(addr, &intern_addr) == 0) {
		zend_error(E_ERROR, "Error converting Address");
		RETURN_FALSE;
	}

	if (_init_options(&attr_options, options, tag) == -1) {
		RETURN_FALSE;
	} else if (rad_put_vendor_addr(radh, vendor, type, intern_addr, &attr_options) == -1) {
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool radius_send_request(desc) */
PHP_FUNCTION(radius_send_request)
{
	struct rad_handle *radh;
	zval *z_radh;
	int res;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_radh)
		== FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	res = rad_send_request(radh);
	if (res == -1) {
		RETURN_FALSE;
	} else {
		RETURN_LONG(res);
	}
}
/* }}} */

/* {{{ proto string radius_get_attr(desc) */
PHP_FUNCTION(radius_get_attr)
{
	struct rad_handle *radh;
	int res;
	const void *data;
	size_t len;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_radh) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	res = rad_get_attr(radh, &data, &len);
	if (res == -1) {
		RETURN_FALSE;
	} else {
		if (res > 0) {

			array_init(return_value);
			add_assoc_long(return_value, "attr", res);
			add_assoc_stringl(return_value, "data", (char *) data, len);
			return;
		}
		RETURN_LONG(res);
	}
}
/* }}} */

/* {{{ proto string radius_get_tagged_attr_data(string attr) */
PHP_FUNCTION(radius_get_tagged_attr_data)
{
	const char *attr;
	COMPAT_ARG_SIZE_T len;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &attr, &len) == FAILURE) {
		return;
	}

	if (len < 1) {
		zend_error(E_NOTICE, "Empty attributes cannot have tags");
		RETURN_FALSE;
	} else if (len == 1) {
		RETURN_EMPTY_STRING();
	}

	RETURN_STRINGL(attr + 1, len - 1);
}
/* }}} */

/* {{{ proto string radius_get_tagged_attr_tag(string attr) */
PHP_FUNCTION(radius_get_tagged_attr_tag)
{
	const char *attr;
	COMPAT_ARG_SIZE_T len;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &attr, &len) == FAILURE) {
		return;
	}

	if (len < 1) {
		zend_error(E_NOTICE, "Empty attributes cannot have tags");
		RETURN_FALSE;
	}

	RETURN_LONG((long) *attr);
}
/* }}} */

/* {{{ proto string radius_get_vendor_attr(data) */
PHP_FUNCTION(radius_get_vendor_attr)
{
	const void *data, *raw;
	COMPAT_ARG_SIZE_T len;
	u_int32_t vendor;
	unsigned char type;
	size_t data_len;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw, &len) == FAILURE) {
		return;
	}

	if (rad_get_vendor_attr(&vendor, &type, &data, &data_len, raw, len) == -1) {
		RETURN_FALSE;
	} else {

		array_init(return_value);
		add_assoc_long(return_value, "attr", type);
		add_assoc_long(return_value, "vendor", vendor);
		add_assoc_stringl(return_value, "data", (char *) data, data_len);
		return;
	}
}
/* }}} */

/* {{{ proto string radius_cvt_addr(data) */
PHP_FUNCTION(radius_cvt_addr)
{
	const void *data;
	char *addr_dot;
	COMPAT_ARG_SIZE_T len;
	struct in_addr addr;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &len) == FAILURE) {
		return;
	}

	addr = rad_cvt_addr(data);
	addr_dot = inet_ntoa(addr);
	RETURN_STRINGL(addr_dot, strlen(addr_dot));
}
/* }}} */

/* {{{ proto int radius_cvt_int(data) */
PHP_FUNCTION(radius_cvt_int)
{
	const void *data;
	COMPAT_ARG_SIZE_T len;
	int val;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &len)
		== FAILURE) {
		return;
	}

	val = rad_cvt_int(data);
	RETURN_LONG(val);
}
/* }}} */

/* {{{ proto string radius_cvt_string(data) */
PHP_FUNCTION(radius_cvt_string)
{
	const void *data;
	char *val;
	COMPAT_ARG_SIZE_T len;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &len)
		== FAILURE) {
		return;
	}

	val = rad_cvt_string(data, len);
	if (val == NULL) RETURN_FALSE;
	RETVAL_STRINGL(val, strlen(val));
	free(val);
	return;
}
/* }}} */

/* {{{ proto string radius_salt_encrypt_attr(resource radh, string data) */
PHP_FUNCTION(radius_salt_encrypt_attr)
{
	char *data;
	COMPAT_ARG_SIZE_T len;
	struct rad_handle *radh;
	struct rad_salted_value salted;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_radh, &data, &len) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	if (rad_salt_value(radh, data, len, &salted) == -1) {
		zend_error(E_WARNING, "%s", rad_strerror(radh));
		RETURN_FALSE;
	} else if (salted.len == 0) {
		RETURN_EMPTY_STRING();
	}

	RETVAL_STRINGL(salted.data, salted.len);
	efree(salted.data);
}
/* }}} */

/* {{{ proto string radius_request_authenticator(radh) */
PHP_FUNCTION(radius_request_authenticator)
{
	struct rad_handle *radh;
	ssize_t res;
	char buf[LEN_AUTH];
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_radh) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	res = rad_request_authenticator(radh, buf, sizeof buf);
	if (res == -1) {
		RETURN_FALSE;
	} else {
		RETURN_STRINGL(buf, res);
	}
}
/* }}} */

/* {{{ proto string radius_server_secret(radh) */
PHP_FUNCTION(radius_server_secret)
{
	char *secret;
	struct rad_handle *radh;
	zval *z_radh;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_radh) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);
	secret = (char *)rad_server_secret(radh);

	if (secret) {
		RETURN_STRINGL(secret, strlen(secret));
	}

	RETURN_FALSE;
}
/* }}} */

/* {{{ proto string radius_demangle(radh, mangled) */
PHP_FUNCTION(radius_demangle)
{
	struct rad_handle *radh;
	zval *z_radh;
	const void *mangled;
	unsigned char *buf;
	COMPAT_ARG_SIZE_T len;
	int res;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_radh, &mangled, &len) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	buf = emalloc(len);
	res = rad_demangle(radh, mangled, len, buf);

	if (res == -1) {
		efree(buf);
		RETURN_FALSE;
	} else {
		RETVAL_STRINGL((char *) buf, len);
		efree(buf);
		return;
	}
}
/* }}} */

/* {{{ proto string radius_demangle_mppe_key(radh, mangled) */
PHP_FUNCTION(radius_demangle_mppe_key)
{
	struct rad_handle *radh;
	zval *z_radh;
	const void *mangled;
	unsigned char *buf;
	size_t dlen;
	COMPAT_ARG_SIZE_T len;
	int res;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_radh, &mangled, &len) == FAILURE) {
		return;
	}

	RADIUS_FETCH_RESOURCE(radh, z_radh);

	buf = emalloc(len);
	res = rad_demangle_mppe_key(radh, mangled, len, buf, &dlen);
	if (res == -1) {
		efree(buf);
		RETURN_FALSE;
	} else {
		RETVAL_STRINGL((char *) buf, dlen);
		efree(buf);
		return;
	}
}
/* }}} */

/* {{{ _init_options() */
int _init_options(struct rad_attr_options *out, int options, int tag) {
	memset(out, 0, sizeof(struct rad_attr_options));

	if (options & RADIUS_OPTION_SALT) {
		out->options |= RAD_OPTION_SALT;
	}

	if (options & RADIUS_OPTION_TAGGED) {
		if (tag < 0 || tag > 255) {
			zend_error(E_NOTICE, "Tag must be between 0 and 255");
			return -1;
		}

		out->options |= RAD_OPTION_TAG;
		out->tag = tag;
	}

	return 0;
}
/* }}} */

/* {{{ _radius_close() */
void _radius_close(zend_resource *res TSRMLS_DC)
{
	struct rad_handle *radh = (struct rad_handle *)res->ptr;
	rad_close(radh);
	res->ptr = NULL;
}
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=8 ts=8 fdm=marker
 * vim<600: noet sw=8 ts=8
 */