2 // This is an example client script for talking to the service.
4 error_reporting(E_ALL);
6 // command for executing this via a socket file.
7 #$socket_path = "/var/www/sockets/autocreate_ldap_accounts_in_postgresql/example/example_users.socket";
8 #$socket_family = AF_UNIX;
10 #$socket_protocol = 0;
12 // command for executing via a network socket.
13 $socket_path = "example.com";
14 $socket_family = AF_INET;
16 $socket_protocol = SOL_TCP;
18 $socket_type = SOCK_STREAM;
20 $packet_size_target = 63;
21 $packet_size_client = 1;
24 // open a client socket.
25 $socket = socket_create($socket_family, $socket_type, $socket_protocol);
27 if ($socket === FALSE) {
28 print("Something went wrong with socket_create().\n");
29 socket_close($socket);
33 // connect to the socket.
34 $connected = socket_connect($socket, $socket_path, $socket_port);
36 if ($connected === FALSE) {
37 print("Something went wrong with socket_connect().\n");
38 socket_close($socket);
43 // build packet for requesting that the user 'example' should be created.
44 $test_name = 'example';
45 $test_name_length = strlen($test_name);
46 $test_name_difference = $packet_size_target - $test_name_length;
48 if ($test_name_difference > 0) {
49 // the packet expects a packet to be NULL terminated or at most $packet_size_target.
50 $test_packet = pack('a' . $test_name_length . 'x' . $test_name_difference, $test_name);
53 $test_packet = pack('a' . $test_name_length, $test_name);
56 print("Packet looks like: '$test_packet'\n");
57 $written = socket_write($socket, $test_packet, $packet_size_target);
59 if ($written === FALSE) {
60 print("Something went wrong with socket_write().\n");
61 socket_close($socket);
64 elseif ($written == 0) {
65 print("Nothing was written to the socket using socket_write().\n");
66 socket_close($socket);
71 // read the return result from the target socket.
72 $response = socket_read($socket, $packet_size_client);
74 if (!is_string($response) || strlen($response) == 0) {
75 print("Something went wrong with socket_read() and did not get a valid return from the socket.\n");
76 socket_close($socket);
80 // an integer is expected to be returned by the socket.
81 $response_packet = unpack('C', $response);
82 $response_value = (int) $response_packet[1];
84 print("Target Socket Replied with = " . print_r($response_value, TRUE) . "\n");
86 // response codes as defined in the c source file:
87 // 0 = no problems detected.
88 // 1 = invalid user name, bad characters, or name too long.
89 // 2 = failed to connect to the ldap server and could not query the ldap name.
90 // 3 = user name not found in ldap database.
91 // 4 = failed to connect to the database.
92 // 5 = error returned while executing the SQL command.
93 // 6 = error occured while reading input from the user (such as via recv()).
94 // 7 = error occured while writing input from the user (such as via send()).
95 // 8 = the received packet is invalid, such as wrong length.
96 // 10 = connection timed out when reading or writing.
97 // 11 = the connection is being forced closed.
98 // 12 = the connection is closing because the service is quitting.
101 socket_close($socket);