/*	$CoreSDI: attr_tcp.c,v 1.7 2001/10/05 19:38:31 claudio Exp $	*/

/*
 *   Copyright (c) 2000, 2001, Core SDI S.A., Argentina
 *   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. Neither name of the Core SDI S.A. nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *  
 *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 */

/*
 * Tcp attribute module - Compatibility with msyslog-1.xx
 * Author: Claudio Castiglia
 */

#include <sys/param.h>

#ifdef __linux__
#include <netinet/in.h>		/* in_addr_t */
#endif

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sysdep.h"
#include "resource.h"
#include "packet.h"
#include "modtypes.h"
#include "iaargs.h"
#include "log.h"

#define ATTR_TCP	"attr tcp: "

typedef struct _tcp_ctx {
	char	 info[BUFSIZ];
} TCP_CTX;


/*
 * _strcpy():
 *	Copy specified arguemnt into the info string.
 */
void
_strcpy(TCP_CTX *tc, const char *name, const char *arg)
{
	char *p;

	if (name == NULL)
		name = "invalid";

	p = tc->info + strlen(tc->info);
	snprintf(p, sizeof(tc->info) - strlen(tc->info), "\t%s\t%s\n",
	    name, arg);
}


/*
 * _get_logset_name():
 *	Return logset name (tcp attribute knows nothing about logset name).
 */
static void
_get_logset_name(ATTRCON *context, struct attrargs_ret *args)
{
	args->size = 0;
}


/*
 * _info():
 *	Return tcp information coded as a string:
 *	"tcp
 *	      [ host <host> ]
 *	      [	port <port> ]
 *	      [ retry <retry seconds> ]
 *	      [ buffer <buffer_size> ]"
 */
static void
_info(ATTRCON *context, struct attrargs_ret *args)
{
	TCP_CTX	*tc;

	tc = (TCP_CTX *) (context + 1);
	snprintf(args->data, args->size, "tcp\n%s", tc->info);
}


/*
 * init():
 *	Initialize tcp module.
 */
ATTRCON *
init(struct attrargs_init *args)
{
	static char	*options[] = {
		"-h", "host",
		"-p", "port",
		"-m", "retry time",
		"-s", "buffer size",
		NULL
	};
	ATTRCON		*context;
	TCP_CTX		*tc;
	char		**p;
	int		 i;

	log_debug(ATTR_TCP "Initializing.");

	if (args->argc < 1) {
		errno = EINVAL;
		return (NULL);
	}

	context = (ATTRCON *) calloc(1, sizeof(ATTRCON) + sizeof(TCP_CTX));
	if (context == NULL) {
		log_err(ATTR_TCP "Can't create context: %s.", strerror(errno));
		return (NULL);
	}

	tc = (TCP_CTX *) (context + 1);

	for (i = 1; i < args->argc - 1; i++) {
		for (p = options; *p != NULL; p += 2)
			if (!strcmp(args->argv[i], *p)) {
				_strcpy(tc, *(p + 1), args->argv[++i]);
				break;
			}
		if (*p == NULL)
			_strcpy(tc, NULL, args->argv[++i]);
	}

	return (context);
}


/*
 * proc_entry()
 */
int
proc_entry(int opcode, ATTRCON *context, void *args)
{
	switch(opcode) {
	case ATTR_GET_LOGSET_NAME:
		_get_logset_name(context, args);
		return (0);
	case ATTR_INFO:
		_info(context, args);
	case ATTR_FREEZE:
	case ATTR_GET:
	case ATTR_ZAP:
	case ATTR_ROTATE:
	case ATTR_SIGN:
		return (0);
	default:
		errno = EINVAL;
	}

	log_err(ATTR_TCP "Invalid '%d' command.", opcode);
	return (-1);
}

