File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / src / sess.c
Revision 1.2.2.1: download - view: text, annotated - select for diffs - revision graph
Sat Apr 30 22:02:59 2011 UTC (13 years, 6 months ago) by misho
Branches: sess2_1
Diff to: branchpoint 1.2: preferred, unified
added BSD license

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: sess.c,v 1.2.2.1 2011/04/30 22:02:59 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "aitsess.h"
   48: 
   49: 
   50: /*
   51:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
   52:  * @ppsVals = Array strings
   53:  * return: none
   54: */
   55: inline void sess_FreeValues(char *** __restrict ppsVals)
   56: {
   57: 	char **ptr;
   58: 
   59: 	for (ptr = *ppsVals; *ptr; ptr++)
   60: 		free(*ptr);
   61: 	free(*ppsVals);
   62: 	*ppsVals = NULL;
   63: }
   64: 
   65: /*
   66:  * sess_GetValues() Get all values from session shared memory
   67:  * @s = Session item
   68:  * @ppsVals = Return array strings
   69:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
   70: */
   71: int sess_GetValues(tagSess * __restrict s, char ***ppsVals)
   72: {
   73: 	register int i;
   74: 	char **valz, *Shared = NULL;
   75: 	char *peer, *p_brk;
   76: 
   77: 	if (!s || !ppsVals)
   78: 		return -1;
   79: 	valz = malloc(sizeof(caddr_t));
   80: 	if (!valz) {
   81: 		LOGERR;
   82: 		return -1;
   83: 	} else
   84: 		*valz = NULL;
   85: 
   86: 	// allocated memory & mirrored shared memory into this
   87: 	Shared = malloc(s->eom);
   88: 	if (!Shared) {
   89: 		LOGERR;
   90: 		free(valz);
   91: 		return -1;
   92: 	} else
   93: 		memcpy(Shared, s->addr, s->eom);
   94: 
   95: 	for (i = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
   96: 			peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
   97: 		if (!strchr(peer, '='))
   98: 			continue;
   99: 		else
  100: 			i++;
  101: 
  102: 		valz = realloc(valz, (i + 1) * sizeof(caddr_t));
  103: 		if (!valz) {
  104: 			LOGERR;
  105: 			free(Shared);
  106: 			return -1;
  107: 		} else
  108: 			valz[i] = NULL;
  109: 
  110: 		valz[i - 1] = strdup(peer);
  111: 	}
  112: 
  113: 	free(Shared);
  114: 	*ppsVals = valz;
  115: 	return i;
  116: }
  117: 
  118: /*
  119:  * sess_GetValue() Get value from session shared memory from attribute
  120:  * @s = Session item
  121:  * @csAttr = Attribute for search
  122:  * @psVal = Return string buffer
  123:  * @pnLen = Length of return string buffer, 
  124: 	// *{pnLen} input is max_size of buffer & output is really taken bytes
  125:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
  126: */
  127: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)
  128: {
  129: 	register int i;
  130: 	int def = IS_VAL;
  131: 	char *Shared = NULL;
  132: 	char *peer, *p_brk, *a_brk, *attr, *val;
  133: 
  134: 	if (!s || !csAttr || !*csAttr)
  135: 		return -1;
  136: 	if (psVal) {
  137: 		if (pnLen && *pnLen > 0)
  138: 			memset(psVal, 0, *pnLen);
  139: 		else
  140: 			return -1;
  141: 	}
  142: 
  143: 	// allocated memory & mirrored shared memory into this
  144: 	Shared = malloc(s->eom);
  145: 	if (!Shared) {
  146: 		LOGERR;
  147: 		return -1;
  148: 	} else
  149: 		memcpy(Shared, s->addr, s->eom);
  150: 
  151: 	for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
  152: 			i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
  153: 		attr = strtok_r(peer, "=\r\n", &a_brk);
  154: 		if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {
  155: 			val = strtok_r(NULL, "=\r\n", &a_brk);
  156: 			if (val && strlen(val)) {
  157: 				if (psVal)
  158: 					strlcpy(psVal, val, *pnLen);
  159: 				if (pnLen)
  160: 					*pnLen = strlen(val);
  161: 			} else
  162: 				def = IS_DEF;
  163: 
  164: 			free(Shared);
  165: 			return i | def;
  166: 		}
  167: 	}
  168: 
  169: 	free(Shared);
  170: 	return 0;
  171: }
  172: 
  173: /*
  174:  * sess_DelValue() Delete item from session shared memory
  175:  * @s = Session item
  176:  * @csAttr = Attribute for erasing
  177:  * return: 0 Ok, -1 error: in parameter
  178: */
  179: int sess_DelValue(tagSess * __restrict s, const char *csAttr)
  180: {
  181: 	register int i;
  182: 	int ret, attrlen;
  183: 	char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
  184: 	char *peer, *p_brk;
  185: 
  186: 	if (!s || !csAttr || !*csAttr)
  187: 		return -1;
  188: 	else
  189: 		attrlen = strlen(csAttr);
  190: 	Buffer = Shared = NULL;
  191: 	memset(szAttr, 0, MAX_ATTRIBUTE + 1);
  192: 	strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
  193: 	strcat(szAttr, "=");
  194: 
  195: 	Buffer = malloc(s->eom);
  196: 	if (!Buffer) {
  197: 		LOGERR;
  198: 		return -1;
  199: 	} else
  200: 		memset(Buffer, 0, s->eom);
  201: 	Shared = malloc(s->eom);
  202: 	if (!Shared) {
  203: 		LOGERR;
  204: 		free(Buffer);
  205: 		return -1;
  206: 	} else {
  207: 		DEC_SEMAPHORE(s, ret);
  208: 		memcpy(Shared, s->addr, s->eom);
  209: 	}
  210: 
  211: 	for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
  212: 			i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
  213: 		if (!strncmp(peer, csAttr, attrlen))
  214: 			if (peer[attrlen] == '=' || peer[attrlen] == *MEM_DELIM || !peer[attrlen] ||
  215: 					peer[attrlen] == '\r' || peer[attrlen] == '\n')
  216: 				continue;
  217: 
  218: 		strcat(Buffer, peer);
  219: 		strcat(Buffer, MEM_DELIM);
  220: 	}
  221: 
  222: 	memset(s->addr, 0, s->eom);
  223: 	memcpy(s->addr, Buffer, s->eom);
  224: 
  225: 	if (s->type == SHARED_MAP)
  226: 		msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
  227: 
  228: 	ADD_SEMAPHORE(s, ret);
  229: 	free(Shared);
  230: 	free(Buffer);
  231: 	return 0;
  232: }
  233: 
  234: /*
  235:  * sess_SetValue() Set item into session shared memory or update if find it
  236:  * @s = Session item
  237:  * @csAttr = Attribute
  238:  * @psVal = Value
  239:  * return: 0 nothing, -1 error: in parameter, 
  240:  	>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
  241: */
  242: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)
  243: {
  244: 	register int i;
  245: 	int upd, ret, def = IS_VAL;
  246: 	char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
  247: 	char *peer, *p_brk;
  248: 
  249: 	if (!s || !csAttr || !*csAttr)
  250: 		return -1;
  251: 	else
  252: 		Buffer = Shared = NULL;
  253: 	memset(szAttr, 0, MAX_ATTRIBUTE + 1);
  254: 	strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
  255: 	strcat(szAttr, "=");
  256: 
  257: 	Buffer = malloc(s->eom);
  258: 	if (!Buffer) {
  259: 		LOGERR;
  260: 		return -1;
  261: 	} else
  262: 		memset(Buffer, 0, s->eom);
  263: 	Shared = malloc(s->eom);
  264: 	if (!Shared) {
  265: 		LOGERR;
  266: 		free(Buffer);
  267: 		return -1;
  268: 	} else {
  269: 		DEC_SEMAPHORE(s, ret);
  270: 		memcpy(Shared, s->addr, s->eom);
  271: 	}
  272: 
  273: 	for (i = 1, upd = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
  274: 			i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
  275: 		if (!strncmp(peer, szAttr, strlen(szAttr))) {
  276: 			upd++;
  277: 			if (psVal) {
  278: 				strcat(Buffer, szAttr);
  279: 				strcat(Buffer, psVal);
  280: 				strcat(Buffer, MEM_DELIM);
  281: 			} else {
  282: 				strcat(Buffer, csAttr);
  283: 				strcat(Buffer, MEM_DELIM);
  284: 				def = IS_DEF;
  285: 			}
  286: 			continue;
  287: 		}
  288: 
  289: 		strcat(Buffer, peer);
  290: 		strcat(Buffer, MEM_DELIM);
  291: 	}
  292: 
  293: 	if (!upd) {
  294: 		if (psVal) {
  295: 			strcat(Buffer, szAttr);
  296: 			strcat(Buffer, psVal);
  297: 			strcat(Buffer, MEM_DELIM);
  298: 		} else {
  299: 			strcat(Buffer, csAttr);
  300: 			strcat(Buffer, MEM_DELIM);
  301: 			def = IS_DEF;
  302: 		}
  303: 		def |= IS_ADD;
  304: 	}
  305: 
  306: 	memset(s->addr, 0, s->eom);
  307: 	memcpy(s->addr, Buffer, s->eom);
  308: 
  309: 	if (s->type == SHARED_MAP)
  310: 		msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
  311: 
  312: 	ADD_SEMAPHORE(s, ret);
  313: 	free(Shared);
  314: 	free(Buffer);
  315: 	return upd | def;
  316: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>