/*
 * File: socket.c
 * PLI socket connection library
 * :set tabstop=4
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include "veriuser.h"
#include "req.h"
#include "req_cmd.h"

int socket_open(p_ipc_socket psock)
{
	int fd;
	struct sockaddr_in addr;
	int on;

	if (psock == NULL) return -1;
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		printf("ERROR: Cannot open %s socket\n", psock->name);
		return -1;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	addr.sin_port = htons(psock->portn);

	on = 1;
	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) < 0) {
		printf("ERROR: %s SO_REUSEADDR failed (errno %d)\n", psock->name, errno);
		return -1;
	}

	if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		printf("ERROR: %s bind failed (errno %d)\n", psock->name, errno);
		close(fd);
		return -1;
	}

	if (listen(fd, 1) < 0) {
		printf("ERROR: %s listen failed (errno %d)\n", psock->name, errno);
		close(fd);
		return -1;
	}

	return fd;
}

int sock_read(int fd, char *buf, int nbytes)
{
	int nleft, nread;
	
	bzero(buf, nbytes);
	nleft = nbytes;

	while (nleft > 0) {	
		nread = recv(fd, buf, nleft, 0);
		if (nread < 0) {
            if (errno == EINTR) {
                nread = 0;
            } else {
                printf("ERROR: socket recv failed errno=%d\n", errno);
                return -1;
            }
		}
		nleft -= nread;
		buf += nread;
	}

	return( (nleft > 0) ? -1 : 0);
} 

int sock_write(int fd, char *buf, int nbytes)
{
	int nleft, nwritten;

	nleft = nbytes;
	while (nleft > 0) {
		nwritten = send(fd, buf, nleft, 0);
		if (nwritten <= 0) {
			printf("ERROR: socket write failed, errno=%d\n", errno);
			return -1;
		}
		nleft -= nwritten;
		buf += nwritten;
	}

	return( (nleft > 0) ? -1 : 0);
}

unsigned char *socket_read(p_ipc_socket p_sock)
{
	unsigned char *pdata =  NULL;
	ipc_pkth  pkth;

	sock_read(p_sock->fd, (char *)(&pkth), sizeof(pkth));
	pkth.magic = ntohl(pkth.magic);
	pkth.length = ntohl(pkth.length);
	pkth.cmd = ntohl(pkth.cmd);
	pkth.cmd_id = ntohl(pkth.cmd_id);

	if (pkth.magic != IPC_PKT_MAGIC) {
		printf("ERROR: Invalid cmd packet cmd_id=%d\n", pkth.cmd_id);
		return pdata;
	}	

	  /* Make extra word to make life easier as the following:
	     (1) Command without argument
	     (2) Combine xz and non-xz value together for single data input
	  */
	pdata = (unsigned char *) malloc(pkth.length + sizeof(int));
	if (pdata == NULL) {
		printf("ERROR: cannot alloc mem to recv req %d", pkth.cmd_id);
		return pdata;
	}
	
	 /* prepare output array, 1st argument set to 0 */
	memcpy(pdata, &pkth, sizeof(pkth));
	memset(pdata+sizeof(pkth), 0, sizeof(int));
	memset(pdata+pkth.length, 0, sizeof(int));

	if (pkth.length > sizeof(pkth)) {
		sock_read(p_sock->fd, ((char *) pdata) + sizeof(pkth), 
			pkth.length-sizeof(pkth));
		/* Do not handle endianess for data part,
		   Command should handle it if necessary 
		*/
		/*
		for (i=sizeof(pkth); i<pkth.length; i+=sizeof(int)) 
			pdata[i/sizeof(int)] = ntohl(pdata[i/sizeof(int)]);
		 */
	}
	
	return pdata;
}

