mbgtools-lx  4.2.8
mbgerror.c File Reference
#include <mbgerror.h>
#include <mbg_tgt.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>

Go to the source code of this file.

Data Structures

struct  ERRNO_CNV_ENTRY
 

Macros

#define _MBGERROR
 
#define _MBG_TGT_HAS_POSIX_ERRNO   1
 
#define _MBG_TGT_HAS_POSIX_H_ERRNO   1
 

Functions

static int lookup_tbl_errno (int srch_errno, const ERRNO_CNV_ENTRY tbl[], int dflt_val)
 Lookup some error code in a conversion table. More...
 
int mbg_errno_to_os (int err_no)
 Convert one of the MBG_ERROR_CODES to an OS-specific format. More...
 
const char * mbg_strerror (int mbg_errno)
 Return an error string associated with the MBG_ERROR_CODES. More...
 
bool mbg_cond_err_msg (int rc, const char *what)
 Check if a value is an error code and print an associated error message. More...
 
bool mbg_cond_err_msg_info (int rc, const char *what, const char *info)
 Check if a value is an general or a "not supported" error code and print an associated message. More...
 
int mbg_posix_errno_to_mbg (int posix_errno, const char *info)
 Translate a POSIX errno error code to one of the MBG_ERROR_CODES. More...
 
int mbg_posix_h_errno_to_mbg (int posix_h_errno, const char *info)
 Translate a POSIX h_errno error code to one of the MBG_ERROR_CODES. More...
 
int mbg_get_last_error (const char *info)
 Get and translate last error after non-socket function call. More...
 
int mbg_get_last_socket_error (const char *info)
 Get and translate last error after socket function call. More...
 
int mbg_get_gethostbyname_error (const char *info)
 Retrieve and convert last error after gethostbyname() More...
 

Variables

static ERRNO_CNV_ENTRY posix_errno_table []
 Mappings between Meinberg error codes and POSIX error codes. More...
 
static ERRNO_CNV_ENTRY posix_h_errno_table []
 

Macro Definition Documentation

◆ _MBG_TGT_HAS_POSIX_ERRNO

#define _MBG_TGT_HAS_POSIX_ERRNO   1

Definition at line 84 of file mbgerror.c.

◆ _MBG_TGT_HAS_POSIX_H_ERRNO

#define _MBG_TGT_HAS_POSIX_H_ERRNO   1

Definition at line 85 of file mbgerror.c.

◆ _MBGERROR

#define _MBGERROR

Definition at line 51 of file mbgerror.c.

Function Documentation

◆ lookup_tbl_errno()

static int lookup_tbl_errno ( int  srch_errno,
const ERRNO_CNV_ENTRY  tbl[],
int  dflt_val 
)
static

Lookup some error code in a conversion table.

Parameters
[in]srch_errnoThe error number to be looked up in the conversion table
[in]tblA conversion table
[in]dflt_valThe code to be returned if no table entry was found.
Returns
MBG_ERROR_CODES associated with the original error code, or dflt_val if original code not found in table.

Definition at line 601 of file mbgerror.c.

References ERRNO_CNV_ENTRY::mapped_errno, and ERRNO_CNV_ENTRY::srch_errno.

Referenced by mbg_cond_err_msg_info(), mbg_errno_to_os(), mbg_posix_errno_to_mbg(), and mbg_posix_h_errno_to_mbg().

◆ mbg_cond_err_msg()

bool mbg_cond_err_msg ( int  rc,
const char *  what 
)

Check if a value is an error code and print an associated error message.

Parameters
[in]rcA positive number including MBG_SUCCESS, or one of the MBG_ERROR_CODES
[in]whatA string indicated what failed
Returns
true if rc represented an error code, and a message has been printed, else false

Definition at line 714 of file mbgerror.c.

References mbg_cond_err_msg_info().

◆ mbg_cond_err_msg_info()

bool mbg_cond_err_msg_info ( int  rc,
const char *  what,
const char *  info 
)

Check if a value is an general or a "not supported" error code and print an associated message.

If rc contains an error code then an error message is printed, and true is returned.

If the optional parameter string info2 is not NULL then it should contain the name of a feature which has been tested before. In this case, if the error code is the specific error MBG_ERR_NOT_SUPP_BY_DEV then a "not supported" message is printed using info2.

If info2 is NULL, or the error code is not MBG_ERR_NOT_SUPP_BY_DEV then the standard error message is printed anyway.

Parameters
[in]rcA positive number including MBG_SUCCESS, or one of the MBG_ERROR_CODES
[in]whatA string indicated what failed
[in]infoAn optional informational string telling what is not supported (may be NULL).
Returns
true if rc represented an error code, and a message has been printed, else false

Definition at line 742 of file mbgerror.c.

References _mbgddmsg_2, lookup_tbl_errno(), mbg_cvi_rs232_error_to_mbg(), MBG_ERR_NOT_SUPP_BY_DEV, MBG_ERR_UNKNOWN, MBG_LOG_INFO, mbg_rc_is_error, mbg_strerror(), MBG_SUCCESS, mbg_win32_ntstatus_to_mbg(), mbg_win32_sys_err_to_mbg(), and mbg_win32_wsa_err_to_mbg().

◆ mbg_errno_to_os()

int mbg_errno_to_os ( int  err_no)

Convert one of the MBG_ERROR_CODES to an OS-specific format.

Parameters
[in]err_noOne of the MBG_ERROR_CODES.
See also
MBG_ERROR_CODES
Returns
An OS-specific error code

Definition at line 626 of file mbgerror.c.

References lookup_tbl_errno(), and MBG_SUCCESS.

◆ mbg_get_gethostbyname_error()

int mbg_get_gethostbyname_error ( const char *  info)

Retrieve and convert last error after gethostbyname()

This function is specific to retrieve and translate error codes returned by gethostbyname() and gethostbyaddr(). In case of error these functions don't set errno but h_errno on POSIX systems, but under Windows the error code can be retrieved by WSAGetLastError() as usual.

The functions gethostbyname() and gethostbyaddr() are obsolete, and getaddressinfo() should be used preferably.

Parameters
[in]infoAn optional informational text string, or NULL
Returns
One of the MBG_ERROR_CODES

Definition at line 1090 of file mbgerror.c.

References MBG_ERR_GENERIC, MBG_ERR_NO_MEM, MBG_ERR_UNKNOWN, mbg_get_last_error(), mbg_posix_h_errno_to_mbg(), mbg_win32_wsa_err_to_mbg(), and mbg_zlib_error_to_mbg().

◆ mbg_get_last_error()

int mbg_get_last_error ( const char *  info)

Get and translate last error after non-socket function call.

Retrieve the "last error" code after a non-socket function has been called and translate to one of the MBG_ERROR_CODES.

On POSIX systems the "last error" code is always stored in errno, but e.g. under Windows the "last error" code after a socket function has to be retrieved by calling WSAGetLastError(), whereas the "last error" code from non-socket POSIX-like functions has to be retrieved by calling GetLastError().

Parameters
[in]infoAn optional informational text string, or NULL
Returns
One of the MBG_ERROR_CODES

Definition at line 993 of file mbgerror.c.

References mbg_posix_errno_to_mbg(), and mbg_win32_sys_err_to_mbg().

◆ mbg_get_last_socket_error()

int mbg_get_last_socket_error ( const char *  info)

Get and translate last error after socket function call.

Retrieve the "last error" code after a socket function has been called and translate to one of the MBG_ERROR_CODES.

On POSIX systems the "last error" code is always stored in errno, but e.g. under Windows the "last error" code after a socket function has to be retrieved by calling WSAGetLastError, whereas the "last error" code from non-socket POSIX-like functions is stored in errno as usual.

Parameters
[in]infoAn optional informational text string, or NULL
Returns
One of the MBG_ERROR_CODES

Definition at line 1038 of file mbgerror.c.

References MBG_ERR_GENERIC, MBG_ERR_UNKNOWN, mbg_posix_errno_to_mbg(), and mbg_win32_wsa_err_to_mbg().

◆ mbg_posix_errno_to_mbg()

int mbg_posix_errno_to_mbg ( int  posix_errno,
const char *  info 
)

Translate a POSIX errno error code to one of the MBG_ERROR_CODES.

Parameters
[in]posix_errnoA POSIX error code as usually defined in errno.h
[in]infoAn optional informational text string, or NULL
Returns
One of the MBG_ERROR_CODES

Definition at line 920 of file mbgerror.c.

References lookup_tbl_errno(), and MBG_ERR_UNKNOWN.

◆ mbg_posix_h_errno_to_mbg()

int mbg_posix_h_errno_to_mbg ( int  posix_h_errno,
const char *  info 
)

Translate a POSIX h_errno error code to one of the MBG_ERROR_CODES.

This function is specific to translate error codes returned by gethostbyname() and gethostbyaddr(). In case of error these functions don't set errno but h_errno to a specific value.

The functions gethostbyname() and gethostbyaddr() are obsolete, and getaddressinfo() should be used preferably.

Parameters
[in]posix_h_errnoAn error code as usually defined in netdb.h
[in]infoAn optional informational text string, or NULL
Returns
One of the MBG_ERROR_CODES

Definition at line 956 of file mbgerror.c.

References lookup_tbl_errno(), and MBG_ERR_UNKNOWN.

◆ mbg_strerror()

const char* mbg_strerror ( int  mbg_errno)

Return an error string associated with the MBG_ERROR_CODES.

Parameters
[in]mbg_errnoOne of the MBG_ERROR_CODES
Returns
A constant string describing the error, or NULL for unknown error codes

Definition at line 685 of file mbgerror.c.

References MBG_CODE_NAME_TABLE_ENTRY::code, MBG_ERR_STR_TABLE_ENG, and MBG_CODE_NAME_TABLE_ENTRY::name.

Variable Documentation

◆ posix_errno_table

ERRNO_CNV_ENTRY posix_errno_table[]
static

Mappings between Meinberg error codes and POSIX error codes.

Always refer to the symbolic names only. The numeric codes listed in the comments below are just for a quick reference, and may vary depending on the OS type and version.

Linux: /usr/include/asm-generic/errno.h
FreeBSD/NetBSD: /usr/include/sys/errno.h

See also
mbg_posix_errno_to_mbg
POSIX spec at http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_03

Definition at line 173 of file mbgerror.c.

◆ posix_h_errno_table

ERRNO_CNV_ENTRY posix_h_errno_table[]
static
Initial value:
=
{
{ HOST_NOT_FOUND, MBG_ERR_HOST_NOT_FOUND },
{ 0, 0 }
}
#define MBG_ERR_HOST_NOT_FOUND
host not found
Definition: mbgerror.h:357

Definition at line 234 of file mbgerror.c.