Commit e17bf2d1 authored by Pekka Pessi's avatar Pekka Pessi
Browse files

Added su_errno.h and su_errno.c.

darcs-hash:20051223132720-65a35-c74f671124b4c71904208a2646b1ba40cd236d79.gz
parent af5260b8
......@@ -42,7 +42,7 @@ endif
BUILT_SOURCES = su_tag_ref.c
include_sofia_HEADERS = \
su_types.h su.h su_addrinfo.h su_localinfo.h \
su_types.h su.h su_errno.h su_addrinfo.h su_localinfo.h \
su_wait.h $(GHEADERS) \
su_alloc.h su_alloc_stat.h su_strlst.h su_vector.h \
su_time.h \
......@@ -56,7 +56,7 @@ include_sofia_HEADERS = \
nodist_include_sofia_HEADERS = su_configure.h
libsu_la_SOURCES = \
su.c su_addrinfo.c $(GSOURCES) \
su.c su_errno.c su_addrinfo.c $(GSOURCES) \
su_alloc.c su_alloc_lock.c su_strdup.c su_sprintf.c \
su_strlst.c su_vector.c \
su_time.c su_time0.c \
......
......@@ -133,112 +133,9 @@ int su_setblocking(su_socket_t s, int blocking)
return ioctlsocket(s, FIONBIO, &nonBlock);
}
/** Get the latest socket error. */
int su_errno(void)
{
return WSAGetLastError();
}
/** Set the socket error. */
int su_seterrno(int errcode)
{
WSASetLastError(errcode);
return -1;
}
const char *su_strerror(int errcode)
{
static struct { int no; const char *msg; } *msgp, msgs[] = {
{ 0, "Success" },
{ WSAEINTR, "Interrupted system call" },
{ WSAEBADF, "Bad file descriptor" },
{ WSAEACCES, "Permission denied" },
{ WSAEFAULT, "Bad address" },
{ WSAEINVAL, "Invalid argument" },
{ WSAEMFILE, "Too many open files" },
{ WSAEWOULDBLOCK, "Another winsock call while a "
"blocking function was in progress" },
{ WSAEINPROGRESS, "Operation now in progress" },
{ WSAEALREADY, "Operation already in progress" },
{ WSAENOTSOCK, "Socket operation on non-socket" },
{ WSAEDESTADDRREQ, "Destination address required" },
{ WSAEMSGSIZE, "Message too long" },
{ WSAEPROTOTYPE, "Protocol wrong type for socket" },
{ WSAENOPROTOOPT, "Protocol not available" },
{ WSAEPROTONOSUPPORT, "Protocol not supported" },
{ WSAESOCKTNOSUPPORT, "Socket type not supported" },
{ WSAEOPNOTSUPP, "Operation not supported" },
{ WSAEPFNOSUPPORT, "Protocol family not supported" },
{ WSAEAFNOSUPPORT, "Address family not supported" },
{ WSAEADDRINUSE, "Address already in use" },
{ WSAEADDRNOTAVAIL, "Can't assign requested address" },
{ WSAENETDOWN, "Network is down" },
{ WSAENETUNREACH, "Network is unreachable" },
{ WSAENETRESET, "Network dropped connection on reset" },
{ WSAECONNABORTED, "Software caused connection abort" },
{ WSAECONNRESET, "Connection reset by peer" },
{ WSAENOBUFS, "No buffer space available" },
{ WSAEISCONN, "Socket is already connected" },
{ WSAENOTCONN, "Socket is not connected" },
{ WSAESHUTDOWN, "Can't send after socket shutdown" },
{ WSAETOOMANYREFS, "Too many references: "
"can't splice" },
{ WSAETIMEDOUT, "Operation timed out" },
{ WSAECONNREFUSED, "Connection refused" },
{ WSAELOOP, "Too many levels of symbolic links" },
{ WSAENAMETOOLONG, "File name too long" },
{ WSAEHOSTDOWN, "Host is down" },
{ WSAEHOSTUNREACH, "No route to host" },
{ WSAENOTEMPTY, "Directory not empty" },
{ WSAEPROCLIM, "Too many processes" },
{ WSAEUSERS, "Too many users" },
{ WSAEDQUOT, "Disc quota exceeded" },
{ WSAESTALE, "Stale NFS file handle" },
{ WSAEREMOTE, "Too many levels of remote in path" },
{ WSASYSNOTREADY, "Network subsystem is unvailable" },
{ WSAVERNOTSUPPORTED, "WinSock version is not "
"supported" },
{ WSANOTINITIALISED, "Successful WSAStartup() not yet "
"performed" },
{ WSAEDISCON, "Graceful shutdown in progress" },
/* Resolver errors */
{ WSAHOST_NOT_FOUND, "No such host is known" },
{ WSATRY_AGAIN, "Host not found, or server failed" },
{ WSANO_RECOVERY, "Unexpected server error "
"encountered" },
{ WSANO_DATA, "Valid name without requested data" },
{ WSANO_ADDRESS, "No address, look for MX record" },
{ 0, NULL }
};
static char buf[64];
for (msgp = msgs; msgp->msg; msgp++) {
if (errcode == msgp->no) {
return msgp->msg;
}
}
/* This can not overflow, but in other hand, it is not thread-safe */
sprintf(buf, "winsock error %d", errcode);
return buf;
}
void su_perror(const char *s)
{
int errcode = WSAGetLastError();
su_log("%s: %s\n", s, su_strerror(errcode));
}
#endif /* SU_HAVE_WINSOCK */
void su_perror2(const char *s, int errcode)
{
su_log("%s: %s\n", s, su_strerror(errcode));
}
int su_soerror(su_socket_t s)
{
int error = 0;
......
......@@ -41,6 +41,9 @@
#ifndef SU_TYPES_H
#include "su_types.h"
#endif
#ifndef SU_ERRNO_H
#include <su_errno.h>
#endif
#include <stdio.h>
......@@ -248,17 +251,6 @@ int su_soerror(su_socket_t s);
/** Get size of message available in socket. */
int su_getmsgsize(su_socket_t s);
/** Print the latest socket error message to stderr */
void su_perror(char const *s);
/** Print the socket error message to stderr. */
void su_perror2(char const *s, int errcode);
/** Return string describing socket error code. */
char const *su_strerror(int e);
/** The latest socket error. */
int su_errno(void);
/** Set the socket error. */
int su_seterrno(int);
/** Scatter-gather send. */
int su_vsend(su_socket_t s, su_iovec_t const iov[], int iovlen, int flags,
su_sockaddr_t const *su, socklen_t sulen);
......@@ -275,10 +267,6 @@ int su_getlocalip(su_sockaddr_t *sin);
#define su_close close
#define su_ioctl ioctl
#define su_isblocking() (su_errno() == EAGAIN || su_errno() == EWOULDBLOCK)
#define su_errno() errno
#define su_seterrno(n) ((errno = (n)), -1)
#define su_perror perror
#define su_strerror(e) strerror(e)
#endif
#if SU_HAVE_WINSOCK
......
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**@CFILE su_errno.c errno compatibility
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
*
* @date Original: Thu Mar 18 19:40:51 1999 pessi
* @date Split to su_errno.c: Thu Dec 22 18:37:02 EET 2005 pessi
*/
#include "config.h"
#include <su_errno.h>
#include <su.h>
#include <string.h>
#if SU_HAVE_WINSOCK
#include <stdio.h>
/** Get the latest socket error. */
int su_errno(void)
{
return WSAGetLastError();
}
/** Set the socket error. */
int su_seterrno(int errcode)
{
WSASetLastError(errcode);
return -1;
}
const char *su_strerror(int errcode)
{
static struct { int no; const char *msg; } *msgp, msgs[] = {
{ 0, "Success" },
{ WSAEINTR, "Interrupted system call" },
{ WSAEBADF, "Bad file descriptor" },
{ WSAEACCES, "Permission denied" },
{ WSAEFAULT, "Bad address" },
{ WSAEINVAL, "Invalid argument" },
{ WSAEMFILE, "Too many open files" },
{ WSAEWOULDBLOCK, "Another winsock call while a "
"blocking function was in progress" },
{ WSAEINPROGRESS, "Operation now in progress" },
{ WSAEALREADY, "Operation already in progress" },
{ WSAENOTSOCK, "Socket operation on non-socket" },
{ WSAEDESTADDRREQ, "Destination address required" },
{ WSAEMSGSIZE, "Message too long" },
{ WSAEPROTOTYPE, "Protocol wrong type for socket" },
{ WSAENOPROTOOPT, "Protocol not available" },
{ WSAEPROTONOSUPPORT, "Protocol not supported" },
{ WSAESOCKTNOSUPPORT, "Socket type not supported" },
{ WSAEOPNOTSUPP, "Operation not supported" },
{ WSAEPFNOSUPPORT, "Protocol family not supported" },
{ WSAEAFNOSUPPORT, "Address family not supported" },
{ WSAEADDRINUSE, "Address already in use" },
{ WSAEADDRNOTAVAIL, "Can't assign requested address" },
{ WSAENETDOWN, "Network is down" },
{ WSAENETUNREACH, "Network is unreachable" },
{ WSAENETRESET, "Network dropped connection on reset" },
{ WSAECONNABORTED, "Software caused connection abort" },
{ WSAECONNRESET, "Connection reset by peer" },
{ WSAENOBUFS, "No buffer space available" },
{ WSAEISCONN, "Socket is already connected" },
{ WSAENOTCONN, "Socket is not connected" },
{ WSAESHUTDOWN, "Can't send after socket shutdown" },
{ WSAETOOMANYREFS, "Too many references: "
"can't splice" },
{ WSAETIMEDOUT, "Operation timed out" },
{ WSAECONNREFUSED, "Connection refused" },
{ WSAELOOP, "Too many levels of symbolic links" },
{ WSAENAMETOOLONG, "File name too long" },
{ WSAEHOSTDOWN, "Host is down" },
{ WSAEHOSTUNREACH, "No route to host" },
{ WSAENOTEMPTY, "Directory not empty" },
{ WSAEPROCLIM, "Too many processes" },
{ WSAEUSERS, "Too many users" },
{ WSAEDQUOT, "Disc quota exceeded" },
{ WSAESTALE, "Stale NFS file handle" },
{ WSAEREMOTE, "Too many levels of remote in path" },
{ WSASYSNOTREADY, "Network subsystem is unvailable" },
{ WSAVERNOTSUPPORTED, "WinSock version is not "
"supported" },
{ WSANOTINITIALISED, "Successful WSAStartup() not yet "
"performed" },
{ WSAEDISCON, "Graceful shutdown in progress" },
/* Resolver errors */
{ WSAHOST_NOT_FOUND, "No such host is known" },
{ WSATRY_AGAIN, "Host not found, or server failed" },
{ WSANO_RECOVERY, "Unexpected server error "
"encountered" },
{ WSANO_DATA, "Valid name without requested data" },
{ WSANO_ADDRESS, "No address, look for MX record" },
{ 0, NULL }
};
static char buf[64];
for (msgp = msgs; msgp->msg; msgp++) {
if (errcode == msgp->no) {
return msgp->msg;
}
}
/* This can not overflow, but in other hand, it is not thread-safe */
sprintf(buf, "winsock error %d", errcode);
return buf;
}
#else
const char *su_strerror(int errcode)
{
return strerror(errcode);
}
#endif /* SU_HAVE_WINSOCK */
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef SU_ERRNO_H /** Defined when su_errno.h has been included. */
#define SU_ERRNO_H
/**@file su_errno.h Errno handling
*
* Source-code compatibility with Windows (having separate errno for
* socket library and C libraries).
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
*
* @date Created: Thu Dec 22 18:16:06 EET 2005 pessi
*/
#ifndef SU_CONFIG_H
#include "su_config.h"
#endif
#include <errno.h>
/** Return string describing su error code. */
char const *su_strerror(int e);
/** The latest su error. */
int su_errno(void);
/** Set the su error. */
int su_seterrno(int);
#if !SU_HAVE_WINSOCK
#define su_errno() (errno)
#define su_seterrno(n) ((errno = (n)), -1)
#endif
#endif
......@@ -34,12 +34,13 @@
#include "config.h"
#include <su_log.h>
#include <su_errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
#include <su_log.h>
#if SU_HAVE_PTHREADS
#include <pthread.h>
#define SU_LOG_IS_INIT(log) pthread_mutex_trylock((log)->log_init)
......@@ -53,6 +54,16 @@
#define SU_LOG_UNLOCK(log)
#endif
void su_perror2(const char *s, int errcode)
{
su_log("%s: %s\n", s, su_strerror(errcode));
}
void su_perror(const char *s)
{
su_perror2(s, su_errno());
}
/** Log a message to default log.
*
* This function is a replacement for printf().
......
......@@ -45,9 +45,7 @@ typedef struct su_log_s su_log_t;
#ifndef SU_LOG_H /** Defined when su_log.h has been included. */
#define SU_LOG_H
#ifndef _STDARG_H
#include <stdarg.h>
#endif
/* Use __attribute__ to allow argument checking for su_log */
#if !defined(__GNUC__) && !defined(__attribute__)
......@@ -91,6 +89,12 @@ void su_log_init(su_log_t *log);
extern su_log_t su_log_default[];
extern su_log_t su_log_global[];
/** Log the latest su error message */
void su_perror(char const *s);
/** Log the su error message. */
void su_perror2(char const *s, int errcode);
#endif
#endif /* !defined(SU_LOG_H_NEED_SU_LOG_T) */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment