/* 
    The committers of the libsocket project, all rights reserved
    (c) 2012, dermesser <lbo@spheniscida.de>

    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.

    THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE  // accept4()
#endif
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>  // read()/write()
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>  // UNIX domain sockets

#include "libconfsocket.h"

int unix_socket_set_path(struct sockaddr_un* addr, const char* pathname)
{
    memset(&addr->sun_path, 0, sizeof(addr->sun_path));
    if (pathname[0] != 0) {
        strncpy(addr->sun_path, pathname, sizeof(addr->sun_path) - 1);
    } else {
        // Abstract socket address.
        // Make sure this is not a nulled memory region.
        if (pathname[1] == 0) {
            errno = EINVAL;
            debug_verbose("unix_socket_set_path: Socket address is too many 0s\n");
            return -1;
        }
        size_t max_len = 1 + strlen(&pathname[1]);
        if (max_len > sizeof(addr->sun_path) - 1) {
            debug_verbose("unix_socket_set_path: Abstract socket address is too long\n");
            errno = ENAMETOOLONG;
            return -2;
        }
        memcpy(addr->sun_path, pathname, max_len);
    }
    return sizeof(addr->sun_path);
}

int unix_socket_bind(int sfd, const char* pathname)
{
    if (pathname) unlink(pathname);
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(struct sockaddr_un));
    strncpy(addr.sun_path, pathname, sizeof(addr.sun_path) - 1);
    addr.sun_family = AF_UNIX;
    return bind(sfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
}

int unix_socket_accept(int sfd, int flags)
{
#ifndef LIBSOCKET_ANDROID
    return accept4(sfd, 0, 0, flags);
#else
    return accept(sfd, 0, 0);
#endif
}

int unix_socket_connect(int sfd, const char* pathname)
{
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(struct sockaddr_un));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, pathname, sizeof(addr.sun_path) - 1);
    return connect(sfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
}

ssize_t unix_socket_recvfrom(int sfd, void* buff, size_t size, char* from, size_t from_size, int flags)
{
    socklen_t socksize = sizeof(struct sockaddr_un);
    struct sockaddr_un addr;
    memset(buff, 0, size);
    int bytes = recvfrom(sfd, buff, size, flags, (struct sockaddr*)&addr, &socksize);
    if (from != NULL && from_size > 0) {
        memset(from, 0, from_size);
        memcpy(from, addr.sun_path, from_size < sizeof(addr.sun_path) ? from_size : sizeof(addr.sun_path));
    }
    return bytes;
}

ssize_t unix_socket_sendto(int sfd, const void* buff, size_t size, const char* pathname, int flags)
{
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(struct sockaddr_un));
    strncpy(addr.sun_path, pathname, sizeof(addr.sun_path) - 1);
    addr.sun_family = AF_UNIX;
    return sendto(sfd, buff, size, flags, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
}


