Pejman Moghadam / C-programming

Communicating between C and PHP through Unix domain sockets

Public domain


server.c

/*
 * gcc server.c -o server
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define BUFSIZE 1024
#define MSGSIZE 100
#define SOCK_PATH "/var/run/mysocket.sock"
#define PERM "0666"
#define WAIT 0
#define LISTEN_BACKLOG 50
#define C_MSG "Server got this message: "
#define handle_error(function) { perror(function); exit(EXIT_FAILURE); }

int main()
{
    int server_socket, accepted_socket, n;
    struct sockaddr_un my_addr, peer_addr;
    socklen_t peer_addr_size;
    char recvd_msg[BUFSIZE], out_str[BUFSIZE];
    mode_t mode = strtol(PERM, 0, 8);

    /* Creating a Unix domain socket */
    server_socket = socket(AF_UNIX, SOCK_STREAM, 0);
    if (server_socket  == -1)
            handle_error("socket");

    /* Binding the socket to a file */
    memset(&my_addr, 0, sizeof(struct sockaddr_un)); /* Clear structure */
    my_addr.sun_family = AF_UNIX;
    strncpy(my_addr.sun_path, SOCK_PATH, sizeof(my_addr.sun_path) - 1);
    unlink(my_addr.sun_path);
    if (bind(server_socket, (struct sockaddr *) &my_addr, 
                    sizeof(struct sockaddr_un)) == -1)
            handle_error("bind");

    /* Instructing the socket to listen for incoming connections */
    if (listen(server_socket, LISTEN_BACKLOG) == -1) 
            handle_error("listen");

    /* Changing permission of socket the file */
    if (chmod(SOCK_PATH, mode) == -1)
            handle_error("chmod");

    for(;;) 
    {
            /* Accepting incoming connections one at a time */
            printf("Waiting for a connection...\n");
            peer_addr_size = sizeof(struct sockaddr_un);
            accepted_socket = accept(server_socket, 
                    (struct sockaddr *) &peer_addr, &peer_addr_size);
            if (accepted_socket == -1)
                    handle_error("accept");

            printf("Connected.\n");

            /* Receiving data */
            n = recv(accepted_socket, recvd_msg, MSGSIZE, 0);
            if (n < 0) 
                    handle_error("recv");

            /* Printing received data */
            snprintf(out_str, strlen(C_MSG)+1, "%s", C_MSG);
            strncat(out_str,recvd_msg, n);
            printf("%s\n",out_str);
            fflush (stdout);

            /* Waiting just for fun */
            sleep(WAIT);

            /* Sending data */
            if (send(accepted_socket, out_str, strlen(out_str), 0) < 0)
                    handle_error("recv");

            /* Cleaning up */
            close(accepted_socket);
    }

    return 0;
}

client.php

<form method=post action=''>
Message to send: <input type="text" name="message" />
<input type="submit" value="Send" />
</form>
<br />

<?php
if(isset($_POST["message"])) {
    $snd_msg = $_POST["message"];
    $rcv_msg = "";
    $timeout = 10;
    $socket = stream_socket_client('unix:///var/run/mysocket.sock', 
                                    $errorno, $errorstr, $timeout);
    stream_set_timeout($socket, $timeout);

    echo("Sending Message...<br>\n");
    if(!fwrite($socket, $snd_msg))
            echo("Error while writing!!!<br>\n");

    echo("Receiving Message...<br>\n");
    if (!($rcv_msg = fread($socket, 1024))) 
            echo("Error while reading!!!<br>\n");
    else 
            echo($rcv_msg."<br>\n");
    echo("done<br>\n");
}
?>

Links

http://php.net/manual/en/function.stream-socket-client.php
http://php.net/manual/en/transports.unix.php
http://php.net/manual/en/function.fwrite.php
http://php.net/manual/en/function.fread.php
http://php.net/manual/en/function.fsockopen.php
http://php.net/manual/en/ref.sockets.php
http://php.net/manual/en/function.socket-write.php
http://php.net/manual/en/function.socket-connect.php

Programming UNIX Sockets in C - Frequently Asked Questions
http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq.html

LINUX SOCKET PART 18 - Advanced TCP/IP - OTHER TCP/IP INFO
http://www.tenouk.com/Module43b.html

BUFFER OVERFLOW 10 - Vulnerability & Exploit Example
http://www.tenouk.com/Bufferoverflowc/Bufferoverflow6.html

Beej's Guide to Unix IPC
http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
http://beej.us/guide/bgipc/output/html/multipage/index.html

Beej's Guide to Network Programming
http://beej.us/guide/bgnet/


The GNU C Library
http://www.delorie.com/gnu/docs/glibc/libc_toc.html
http://www.delorie.com/gnu/docs/glibc/libc_290.html

Unix socket sniffer
http://graag.blogspot.com/2007/10/unix-socket-sniffer.html

unixdump UNIX-socket sniffer
http://c-skills.blogspot.com/2009/09/unixdump-unix-socket-sniffer-available.html


Secure programming with the OpenSSL API, Part 1: Overview of the API
http://www.ibm.com/developerworks/linux/library/l-openssl/index.html
Secure programming with the OpenSSL API, Part 2: Secure handshake
http://www.ibm.com/developerworks/linux/library/l-openssl2/index.html
Secure programming with the OpenSSL API, Part 3: Providing a secure service
http://www.ibm.com/developerworks/linux/library/l-openssl3/index.html


DNS Query Code in C with linux sockets
http://www.binarytides.com/blog/dns-query-code-in-c-with-linux-sockets/

ping 
http://cboard.cprogramming.com/networking-device-communication/41635-ping-program.html
http://www.mycplus.com/source-code/c-source-code/ping/
http://ws.edu.isoc.org/materials/src/ping.c
http://www.freelancer.com/projects/C-Programming-Linux/TCP-Ping-linux.html

BY: Pejman Moghadam
TAG: php, socket
DATE: 2012-07-04 17:10:31


Pejman Moghadam / C-programming [ TXT ]