Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

sockutils.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2002 - 2003
00003  * NetGroup, Politecnico di Torino (Italy)
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions 
00008  * are met:
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright 
00011  * notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright 
00013  * notice, this list of conditions and the following disclaimer in the 
00014  * documentation and/or other materials provided with the distribution. 
00015  * 3. Neither the name of the Politecnico di Torino nor the names of its 
00016  * contributors may be used to endorse or promote products derived from 
00017  * this software without specific prior written permission. 
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00023  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  * 
00031  */
00032 
00033 
00034 #ifndef __SOCKUTILS_H__
00035 #define __SOCKUTILS_H__
00036 
00037 
00038 #if _MSC_VER > 1000
00039 #pragma once
00040 #endif
00041 
00042 
00043 #ifdef WIN32
00044 /* Prevents a compiler warning in case this was already defined (to avoid that */
00045 /* windows.h includes winsock.h */
00046 #ifdef _WINSOCKAPI_
00047 #undef _WINSOCKAPI_
00048 #endif
00049 #include <winsock2.h>
00050 #include <ws2tcpip.h>
00051 #else
00052 #include <stdio.h>
00053 #include <string.h> /* for memset() */
00054 #include <sys/types.h>
00055 #include <sys/socket.h>
00056 #include <netdb.h>  /* DNS lookup */
00057 #include <unistd.h> /* close() */
00058 #include <errno.h>  /* errno() */
00059 #include <netinet/in.h> /* for sockaddr_in, in BSD at least */
00060 #include <arpa/inet.h>
00061 #include <net/if.h>
00062 #endif
00063 
00064 
00087 // Some minor differences between UNIX and Win32
00088 #ifdef WIN32
00089 
00094     #define SOCKET unsigned int 
00095 #else
00096 
00102     #define SOCKET int
00103 
00109     #define closesocket(a) close(a) 
00110 #endif
00111 
00112 
00113 
00114 
00115 
00132 #ifdef NDEBUG
00133 #define SOCK_ASSERT(msg, expr) ((void)0)
00134 #else
00135     #include <assert.h>
00136     #if (defined(WIN32) && defined(_MSC_VER))
00137         #include <crtdbg.h>             // for _CrtDbgReport
00138         // Use MessageBox(NULL, msg, "warning", MB_OK)' instead of the other calls if you want to debug a Win32 service
00139         // Remember to activate the 'allow service to interact with desktop' flag of the service
00140         #define SOCK_ASSERT(msg, expr) { _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%s\n", msg); fprintf(stderr, "%s\n", msg); assert(expr); }
00141     #else
00142         #define SOCK_ASSERT(msg, expr) { fprintf(stderr, "%s\n", msg); assert(expr); }
00143     #endif
00144 #endif
00145 
00146 
00147 
00148 
00149 /****************************************************
00150  *                                                  *
00151  * Exported functions / definitions                 *
00152  *                                                  *
00153  ****************************************************/
00154 
00156 #define SOCKBUF_CHECKONLY 1 
00157 
00158 #define SOCKBUF_BUFFERIZE 0
00159 
00161 #define SOCKOPEN_CLIENT 0
00162 
00163 #define SOCKOPEN_SERVER 1
00164 
00166 #define SOCK_RECEIVEALL_NO 0
00167 
00168 #define SOCK_RECEIVEALL_YES 1
00169 
00170 
00177 #ifdef __cplusplus
00178 extern "C" {
00179 #endif
00180 
00181 
00182 
00192 int sock_init(char *errbuf, int errbuflen);
00193 void sock_cleanup();
00194 // It is 'public' because there are calls (like accept() ) which are not managed from inside the sockutils files
00195 void sock_geterror(const char *caller, char *errbuf, int errbufsize);
00196 int sock_initaddress(const char *address, const char *port,
00197                         struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf, int errbuflen);
00198 int sock_recv(SOCKET socket, char *buffer, int size, int receiveall, char *errbuf, int errbuflen);
00199 SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen);
00200 int sock_close(SOCKET sock, char *errbuf, int errbuflen);
00201 
00202 int sock_send(SOCKET socket, const char *buffer, int size, char *errbuf, int errbuflen);
00203 int sock_bufferize(const char *buffer, int size, char *tempbuf, int *offset, int totsize, int checkonly, char *errbuf, int errbuflen);
00204 int sock_discard(SOCKET socket, int size, char *errbuf, int errbuflen);
00205 int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen);
00206 int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second);
00207 
00208 int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen);
00209 
00210 int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen);
00211 int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, int addr_family, char *errbuf, int errbuflen);
00212 
00213 
00214 #ifdef __cplusplus
00215 }
00216 #endif
00217 
00218 
00231 #endif 

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.