head 1.10;
access;
symbols
LMTP2NNTP_1_0_0:1.9
LMTP2NNTP_0_9_7:1.9
LMTP2NNTP_0_9_6:1.9
LMTP2NNTP_0_9_5:1.9
LMTP2NNTP_0_9_4:1.7
LMTP2NNTP_0_9_3:1.6
LMTP2NNTP_0_9_2:1.4
LMTP2NNTP_0_9_1:1.3
LMTP2NNTP_0_9_0:1.3;
locks; strict;
comment @ * @;
1.10
date 2001.10.02.13.36.33; author rse; state dead;
branches;
next 1.9;
1.9
date 2001.09.07.10.26.08; author thl; state Exp;
branches;
next 1.8;
1.8
date 2001.09.04.09.46.06; author rse; state Exp;
branches;
next 1.7;
1.7
date 2001.08.30.13.57.21; author thl; state Exp;
branches;
next 1.6;
1.6
date 2001.08.28.11.31.22; author thl; state Exp;
branches;
next 1.5;
1.5
date 2001.08.27.14.49.32; author thl; state Exp;
branches;
next 1.4;
1.4
date 2001.08.23.09.12.30; author rse; state Exp;
branches;
next 1.3;
1.3
date 2001.08.20.11.28.29; author rse; state Exp;
branches;
next 1.2;
1.2
date 2001.08.12.13.44.10; author rse; state Exp;
branches;
next 1.1;
1.1
date 2001.08.07.09.04.31; author thl; state Exp;
branches;
next ;
desc
@@
1.10
log
@use the brand-new OSSP SA library (first cut)
@
text
@/*
** Copyright (c) 2001 The OSSP Project
** Copyright (c) 2001 Cable & Wireless Deutschland
**
** This file is part of OSSP lmtp2nntp, an LMTP speaking local
** mailer which forwards mails as Usenet news articles via NNTP.
** It can be found at http://www.ossp.org/pkg/lmtp2nntp/.
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version
** 2.0 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this file; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact the OSSP project .
**
** sa.c: Socket Address library
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(HAVE_DMALLOC_H) && defined(DMALLOC)
#include "dmalloc.h"
#endif
#include "sa.h"
#if !defined(AF_LOCAL) && defined(AF_UNIX)
#define AF_LOCAL AF_UNIX
#endif
#ifndef HAVE_INET_PTON
#ifdef HAVE_INET_ATON
static int inet_pton(int family, const char *strptr, void *addrptr)
{
struct in_addr in_val;
if (family == AF_INET) {
if (inet_aton(strptr, &in_val)) {
memcpy(addrptr, &in_val, sizeof(struct in_addr));
return 1;
}
return 0;
}
errno = EAFNOSUPPORT;
return -1;
}
#else
#error "neither inet_pton nor inet_aton available"
#endif
#endif
#ifndef NUL
#define NUL '\0'
#endif
sa_t *sa_create(int sa_type, ...)
{
va_list ap;
sa_t *rc;
int nPort;
socklen_t sl;
struct sockaddr *sa;
struct sockaddr_in sa4;
#ifdef AF_INET6
struct sockaddr_in6 sa6;
#endif
struct sockaddr_un sau;
struct hostent *he;
struct servent *se;
struct protoent *pe;
int bNumeric;
int i;
char *cpPath;
int nPath;
char *cpProto;
int nProto;
char *cpHost;
char *cpPort;
va_start(ap, sa_type);
sa = NULL;
sl = 0;
if (sa_type == SA_UNIX) {
#if defined(AF_LOCAL)
if ((cpPath = va_arg(ap, char *)) == NULL)
return NULL;
if ((nPath = strlen(cpPath)) >= (sizeof(sau.sun_path)-1))
return NULL;
nProto = 0;
memset(&sau, 0, sizeof(sau));
sau.sun_family = AF_LOCAL;
memcpy(sau.sun_path, cpPath, nPath + 1);
sa = (struct sockaddr *)&sau;
sl = sizeof(sau);
#else
return NULL;
#endif
}
else if (sa_type == SA_IP) {
if ((cpProto = va_arg(ap, char *)) == NULL)
return NULL;
if ((cpHost = va_arg(ap, char *)) == NULL)
return NULL;
if ((cpPort = va_arg(ap, char *)) == NULL)
return NULL;
/* resolve protocol */
if ((pe = getprotobyname(cpProto)) == NULL)
return NULL;
nProto = pe->p_proto;
/* resolve port */
bNumeric = 1;
for (i = 0; cpPort[i] != NUL; i++) {
if (!isdigit((int)cpPort[i])) {
bNumeric = 0;
break;
}
}
if (bNumeric)
nPort = atoi(cpPort);
else {
if ((se = getservbyname(cpPort, cpProto)) == NULL)
return NULL;
nPort = ntohs(se->s_port);
}
/* mandatory initialization */
memset(&sa4, 0, sizeof(sa4));
#ifdef AF_INET6
memset(&sa6, 0, sizeof(sa6));
#endif
/* resolve host */
if (inet_pton(AF_INET, cpHost, &sa4.sin_addr.s_addr) == 1) {
sa4.sin_family = AF_INET;
sa4.sin_port = htons(nPort);
sa = (struct sockaddr *)&sa4;
sl = sizeof(sa4);
}
#ifdef AF_INET6
else if (inet_pton(AF_INET6, cpHost, &sa6.sin6_addr.s6_addr) == 1) {
sa6.sin6_family = AF_INET6;
sa6.sin6_port = htons(nPort);
sa = (struct sockaddr *)&sa6;
sl = sizeof(sa6);
}
#endif
else if ((he = gethostbyname(cpHost)) != NULL) {
if (he->h_addrtype == AF_INET) {
sa4.sin_family = AF_INET;
sa4.sin_port = htons(nPort);
memcpy(&sa4.sin_addr.s_addr, he->h_addr_list[0], sizeof(sa4.sin_addr.s_addr));
sa = (struct sockaddr *)&sa4;
sl = sizeof(sa4);
}
#ifdef AF_INET6
else if (he->h_addrtype == AF_INET6) {
sa6.sin6_family = AF_INET6;
sa6.sin6_port = htons(nPort);
memcpy(&sa6.sin6_addr.s6_addr, he->h_addr_list[0], sizeof(sa6.sin6_addr.s6_addr));
sa = (struct sockaddr *)&sa6;
sl = sizeof(sa6);
}
#endif
}
}
else
return NULL;
va_end(ap);
if (sa == NULL)
return NULL;
if ((rc = (sa_t *)malloc(sizeof(sa_t))) == NULL)
return NULL;
if ((rc->sa_buf = (struct sockaddr *)malloc(sl)) == NULL) {
free(rc);
return NULL;
}
memcpy(rc->sa_buf, sa, sl);
rc->sa_len = sl;
rc->sa_proto = nProto;
return rc;
}
void sa_destroy(sa_t *sa)
{
if (sa == NULL)
return;
if (sa->sa_buf != NULL)
free(sa->sa_buf);
free(sa);
return;
}
@
1.9
log
@fix mandatory sockaddr_in initialization
@
text
@@
1.8
log
@Finally apply GNU General Public License (GPL) to OSSP lmtp2nntp.
@
text
@d113 1
d151 5
@
1.7
log
@get rid of warnings by properly casting char to int for isdigit()
@
text
@d2 24
a25 28
* SA - Socket Address Library
* Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
* Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
*
* This file is part of OSSP lmtp2nntp, an LMTP speaking local
* mailer which forwards mails as Usenet news articles via NNTP.
* It can be found at http://www.ossp.com/pkg/lmtp2nntp/.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose with or without fee is hereby granted, provided that
* the above copyright notice and this permission notice appear in all
* copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
* 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.
*
* sa.c: implementation
*/
@
1.6
log
@reduced dmalloc() complaints about non-free()d resources during one a "lmtp
post arg cw.de.sd.apps.dev.test" run to zero regarding own code; A memoryleak
in str library 0.9.5 was detected which was reported to and fixed by RSE;
@
text
@d141 1
a141 1
if (!isdigit(cpPort[i])) {
@
1.5
log
@now using NUL instead of '\0'
@
text
@d215 1
a216 1
free(sa);
@
1.4
log
@Add DMalloc support
@
text
@d79 4
d140 1
a140 1
for (i = 0; cpPort[i] != '\0'; i++) {
@
1.3
log
@Autoconf phase 4
@
text
@d45 1
d47 5
@
1.2
log
@cleanup sa library
@
text
@d45 1
d48 25
d81 1
d83 1
d101 1
d111 3
d151 1
d158 1
d167 1
d175 1
@
1.1
log
@sock is now sa (sockaddr) only
@
text
@d2 1
a2 2
* sa.c: Socket Address library (implementation)
*
d27 2
a158 10
/* create socket
* if ((s = socket(rc->sa_buf->sa_family, SOCK_STREAM, rc->sa_proto)) == -1)
* return NULL;
*/
/* connect socket to remote host/port
*
* if (connect(s, rc->sa_buf, rc->sa_len) < 0)
* return -1;
*/
@