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 #include <winsock2.h> 00045 #include <ws2tcpip.h> 00046 #else 00047 #include <stdio.h> 00048 #include <string.h> /* for memset() */ 00049 #include <sys/types.h> 00050 #include <sys/socket.h> 00051 #include <netdb.h> /* DNS lookup */ 00052 #include <unistd.h> /* close() */ 00053 #include <errno.h> /* errno() */ 00054 #include <netinet/in.h> /* for sockaddr_in, in BSD at least */ 00055 #include <arpa/inet.h> 00056 #include <net/if.h> 00057 #endif 00058 00059 00082 // Some minor differences between UNIX and Win32 00083 #ifdef WIN32 00084 00089 #define SOCKET unsigned int 00090 #else 00091 00097 #define SOCKET int 00098 00104 #define closesocket(a) close(a) 00105 #endif 00106 00107 00108 00109 00110 00127 #ifdef NDEBUG 00128 #define SOCK_ASSERT(msg, expr) ((void)0) 00129 #else 00130 #include <assert.h> 00131 #if (defined(WIN32) && defined(_MSC_VER)) 00132 #include <crtdbg.h> // for _CrtDbgReport 00133 // Use MessageBox(NULL, msg, "warning", MB_OK)' instead of the other calls if you want to debug a Win32 service 00134 // Remember to activate the 'allow service to interact with desktop' flag of the service 00135 #define SOCK_ASSERT(msg, expr) { _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%s\n", msg); fprintf(stderr, "%s\n", msg); assert(expr); } 00136 #else 00137 #define SOCK_ASSERT(msg, expr) { fprintf(stderr, "%s\n", msg); assert(expr); } 00138 #endif 00139 #endif 00140 00141 00142 00143 00144 /**************************************************** 00145 * * 00146 * Exported functions / definitions * 00147 * * 00148 ****************************************************/ 00149 00151 #define SOCKBUF_CHECKONLY 1 00152 00153 #define SOCKBUF_BUFFERIZE 0 00154 00156 #define SOCKOPEN_CLIENT 0 00157 00158 #define SOCKOPEN_SERVER 1 00159 00161 #define SOCK_RECEIVEALL_NO 0 00162 00163 #define SOCK_RECEIVEALL_YES 1 00164 00165 00172 #ifdef __cplusplus 00173 extern "C" { 00174 #endif 00175 00176 00177 00187 int sock_init(char *errbuf, int errbuflen); 00188 void sock_cleanup(); 00189 // It is 'public' because there are calls (like accept() ) which are not managed from inside the sockutils files 00190 void sock_geterror(const char *caller, char *errbuf, int errbufsize); 00191 int sock_initaddress(const char *address, const char *port, 00192 struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf, int errbuflen); 00193 int sock_recv(SOCKET socket, char *buffer, int size, int receiveall, char *errbuf, int errbuflen); 00194 //int sock_recv_dgram(SOCKET sock, char *buffer, int size, char *errbuf, int errbuflen); 00195 SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen); 00196 int sock_close(SOCKET sock, char *errbuf, int errbuflen); 00197 00198 int sock_send(SOCKET socket, const char *buffer, int size, char *errbuf, int errbuflen); 00199 int sock_bufferize(const char *buffer, int size, char *tempbuf, int *offset, int totsize, int checkonly, char *errbuf, int errbuflen); 00200 int sock_discard(SOCKET socket, int size, char *errbuf, int errbuflen); 00201 int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen); 00202 int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second); 00203 00204 int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen); 00205 int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, char *errbuf, int errbuflen); 00206 00207 00208 #ifdef __cplusplus 00209 } 00210 #endif 00211 00212 00225 #endif
documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.