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 00039 // Definition for Microsoft Visual Studio 00040 #if _MSC_VER > 1000 00041 #pragma once 00042 #endif 00043 00044 #ifdef WIN32 00045 #include <winsock2.h> 00046 #include <ws2tcpip.h> 00047 #else 00048 #include <sys/types.h> 00049 #include <sys/socket.h> 00050 #include <netdb.h> /* DNS lookup */ 00051 #include <unistd.h> /* close() */ 00052 #endif 00053 00054 00055 00056 // Some minor differences between UNIX and Win32 00057 #ifdef WIN32 00058 00063 #define SOCKET unsigned int 00064 #else 00065 00071 #define SOCKET int 00072 00078 #define closesocket(a) close(a) 00079 #endif 00080 00081 00082 00083 00084 00085 // All this stuff is private 00107 #ifdef NDEBUG 00108 #define SOCK_ASSERT(msg, expr) ((void)0) 00109 #else 00110 #include <assert.h> 00111 #if (defined(WIN32) && defined(_MSC_VER)) 00112 #include <crtdbg.h> // for _CrtDbgReport 00113 // Use MessageBox(NULL, msg, "warning", MB_OK)' instead of the other calls if you want to debug a Win32 service 00114 // Remember to activate the 'allow service to interact with desktop' flag of the service 00115 #define SOCK_ASSERT(msg, expr) { _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%s\n", msg); fprintf(stderr, "%s\n", msg); assert(expr); } 00116 #else 00117 #define SOCK_ASSERT(msg, expr) { fprintf(stderr, "%s\n", msg); assert(expr); } 00118 #endif 00119 #endif 00120 00121 00122 00123 00124 /**************************************************** 00125 * * 00126 * Exported functions / definitions * 00127 * * 00128 ****************************************************/ 00129 00130 #define SOCKBUF_CHECKONLY 1 00131 #define SOCKBUF_BUFFERIZE 0 00133 #define SOCKOPEN_CLIENT 0 00134 #define SOCKOPEN_SERVER 1 00136 #define SOCK_ERRBUF_SIZE 256 // end of private documentation 00141 00142 00143 #ifdef __cplusplus 00144 extern "C" { 00145 #endif 00146 00147 00148 void sock_geterror(const char *caller, char *string, int size); 00149 int sock_init(char *errbuf); 00150 void sock_cleanup(); 00151 int sock_validaddr(const char *address, const char *port, 00152 struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf); 00153 int sock_recv(SOCKET socket, char *buffer, int size, char *errbuf); 00154 int sock_recv_dgram(SOCKET sock, char *buffer, int size, char *errbuf); 00155 int sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf); 00156 int sock_close(SOCKET sock, char *errbuf); 00157 00158 int sock_send(SOCKET socket, const char *buffer, int size, char *errbuf); 00159 int sock_bufferize(const char *buffer, int size, char *tempbuf, int *offset, int totsize, int checkonly, char *errbuf); 00160 int sock_discard(SOCKET socket, int size, char *errbuf); 00161 int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf); 00162 int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second); 00163 00164 00165 #ifdef __cplusplus 00166 } 00167 #endif 00168 00169 00170 #endif 00171
documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.