| 1 |
|
|---|
| 2 |
/* |
|---|
| 3 |
* +---------------------------------------------------------+ |
|---|
| 4 |
* | Easy Socket library - version 0.1 | |
|---|
| 5 |
* | (C) 1999-2000, Erich Roncarolo <erich@roncarolo.eu.org> | |
|---|
| 6 |
* +---------------------------------------------------------+ |
|---|
| 7 |
* |
|---|
| 8 |
* This file is part of Easy Socket library. |
|---|
| 9 |
* |
|---|
| 10 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 11 |
* it under the terms of the GNU Lesser General Public License as published by |
|---|
| 12 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 |
* (at your option) any later version. |
|---|
| 14 |
* |
|---|
| 15 |
* This program is distributed in the hope that it will be useful, |
|---|
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 |
* GNU Lesser General Public License for more details. |
|---|
| 19 |
* |
|---|
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
|---|
| 21 |
* along with this program; if not, write to the Free Software |
|---|
| 22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
#ifndef __EASY_SOCK__ |
|---|
| 27 |
#define __EASY_SOCK__ |
|---|
| 28 |
|
|---|
| 29 |
/* |
|---|
| 30 |
* Max lengths of every type written in chars (as string) |
|---|
| 31 |
*/ |
|---|
| 32 |
#define ES_MAX_CHAR 1 |
|---|
| 33 |
#define ES_MAX_SHORT 8 |
|---|
| 34 |
#define ES_MAX_INT 16 |
|---|
| 35 |
#define ES_MAX_LONG 16 |
|---|
| 36 |
#define ES_MAX_FLOAT 32 |
|---|
| 37 |
#define ES_MAX_DOUBLE 32 |
|---|
| 38 |
|
|---|
| 39 |
/* |
|---|
| 40 |
* Max number of digits of a string length |
|---|
| 41 |
*/ |
|---|
| 42 |
#define ES_MAX_LEN ES_MAX_INT |
|---|
| 43 |
|
|---|
| 44 |
/* |
|---|
| 45 |
* Length of string containing error messages |
|---|
| 46 |
*/ |
|---|
| 47 |
#define EASY_SOCK_ERR_MSG_LEN 50 |
|---|
| 48 |
|
|---|
| 49 |
/* |
|---|
| 50 |
* This the error number when an error occours |
|---|
| 51 |
*/ |
|---|
| 52 |
int easy_sock_err; |
|---|
| 53 |
|
|---|
| 54 |
/* |
|---|
| 55 |
* This string contains an error message when an error occours |
|---|
| 56 |
*/ |
|---|
| 57 |
char easy_sock_err_msg[EASY_SOCK_ERR_MSG_LEN]; |
|---|
| 58 |
|
|---|
| 59 |
/* |
|---|
| 60 |
* Installs an error handler. |
|---|
| 61 |
*/ |
|---|
| 62 |
void easy_error(void (*handler)(int)); |
|---|
| 63 |
|
|---|
| 64 |
/* |
|---|
| 65 |
* This is a very useful function used to connect an host on a port. |
|---|
| 66 |
* 'host' is the hostname |
|---|
| 67 |
* 'port' is the port number |
|---|
| 68 |
* return a socket descriptor |
|---|
| 69 |
*/ |
|---|
| 70 |
int easy_tcp_connect(char *host, int port); |
|---|
| 71 |
|
|---|
| 72 |
/* |
|---|
| 73 |
* This is a very useful function used to bind an host on a port. |
|---|
| 74 |
* 'host' is the hostname (if it is NULL bind any address) |
|---|
| 75 |
* 'port' is the port number |
|---|
| 76 |
* 'backlog' define the maximum length the queue of pending connections |
|---|
| 77 |
* may grow to. |
|---|
| 78 |
* return a socket descriptor |
|---|
| 79 |
*/ |
|---|
| 80 |
int easy_tcp_bind(char *host, int port, int backlog); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
/* |
|---|
| 84 |
* This is a function that read a char from a socket. |
|---|
| 85 |
*/ |
|---|
| 86 |
char read_char(int sock); |
|---|
| 87 |
|
|---|
| 88 |
/* |
|---|
| 89 |
* This is a function that read a short from a socket. |
|---|
| 90 |
*/ |
|---|
| 91 |
short read_short(int sock); |
|---|
| 92 |
|
|---|
| 93 |
/* |
|---|
| 94 |
* This is a function that read an integer from a socket. |
|---|
| 95 |
*/ |
|---|
| 96 |
int read_int(int sock); |
|---|
| 97 |
|
|---|
| 98 |
/* |
|---|
| 99 |
* This is a function that read a long from a socket. |
|---|
| 100 |
*/ |
|---|
| 101 |
long read_long(int sock); |
|---|
| 102 |
|
|---|
| 103 |
/* |
|---|
| 104 |
* This is a function that read a float from a socket. |
|---|
| 105 |
*/ |
|---|
| 106 |
float read_float(int sock); |
|---|
| 107 |
|
|---|
| 108 |
/* |
|---|
| 109 |
* This is a function that read a double from a socket. |
|---|
| 110 |
*/ |
|---|
| 111 |
double read_double(int sock); |
|---|
| 112 |
|
|---|
| 113 |
/* |
|---|
| 114 |
* This is a function that read a string from a socket. |
|---|
| 115 |
* return a new 'mallocated' string or NULL if an error occours. |
|---|
| 116 |
* |
|---|
| 117 |
* Details: this function reads first the string length (int) then |
|---|
| 118 |
* use malloc() to allocate new string so it can be read. |
|---|
| 119 |
* Remember! It's your own responsibility free() the string. |
|---|
| 120 |
*/ |
|---|
| 121 |
char* read_string(int sock); |
|---|
| 122 |
|
|---|
| 123 |
/* |
|---|
| 124 |
* This is a function that write a char to a socket. |
|---|
| 125 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 126 |
*/ |
|---|
| 127 |
int write_char(int sock, char c); |
|---|
| 128 |
|
|---|
| 129 |
/* |
|---|
| 130 |
* This is a function that write a short to a socket. |
|---|
| 131 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 132 |
*/ |
|---|
| 133 |
int write_short(int sock, short s); |
|---|
| 134 |
|
|---|
| 135 |
/* |
|---|
| 136 |
* This is a function that write an integer to a socket. |
|---|
| 137 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 138 |
*/ |
|---|
| 139 |
int write_int(int sock, int i); |
|---|
| 140 |
|
|---|
| 141 |
/* |
|---|
| 142 |
* This is a function that write a long to a socket. |
|---|
| 143 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 144 |
*/ |
|---|
| 145 |
int write_long(int sock, long l); |
|---|
| 146 |
|
|---|
| 147 |
/* |
|---|
| 148 |
* This is a function that write a float to a socket. |
|---|
| 149 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 150 |
*/ |
|---|
| 151 |
int write_float(int sock, float f); |
|---|
| 152 |
|
|---|
| 153 |
/* |
|---|
| 154 |
* This is a function that write a double to a socket. |
|---|
| 155 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 156 |
*/ |
|---|
| 157 |
int write_double(int sock, double d); |
|---|
| 158 |
|
|---|
| 159 |
/* |
|---|
| 160 |
* This is a function that write a string to a socket. |
|---|
| 161 |
* return the number of written chars, or a number < 0 if an error occours. |
|---|
| 162 |
* |
|---|
| 163 |
* Details: this function writes first the string length (int) then |
|---|
| 164 |
* the string itself, so read_string() can read it. |
|---|
| 165 |
*/ |
|---|
| 166 |
int write_string(int sock, char* string); |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
/* |
|---|
| 171 |
* This is a function that read a char from a socket. |
|---|
| 172 |
* For compatibility only: works exactly like read_char(). |
|---|
| 173 |
*/ |
|---|
| 174 |
char read_char_c(int sock); |
|---|
| 175 |
|
|---|
| 176 |
/* |
|---|
| 177 |
* This is a function that read a short (as chars) from a socket. |
|---|
| 178 |
*/ |
|---|
| 179 |
short read_short_c(int sock); |
|---|
| 180 |
|
|---|
| 181 |
/* |
|---|
| 182 |
* This is a function that read an integer (as chars) from a socket. |
|---|
| 183 |
*/ |
|---|
| 184 |
int read_int_c(int sock); |
|---|
| 185 |
|
|---|
| 186 |
/* |
|---|
| 187 |
* This is a function that read a long (as chars) from a socket. |
|---|
| 188 |
*/ |
|---|
| 189 |
long read_long_c(int sock); |
|---|
| 190 |
|
|---|
| 191 |
/* |
|---|
| 192 |
* This is a function that read a float (as chars) from a socket. |
|---|
| 193 |
*/ |
|---|
| 194 |
float read_float_c(int sock); |
|---|
| 195 |
|
|---|
| 196 |
/* |
|---|
| 197 |
* This is a function that read a double (as chars) from a socket. |
|---|
| 198 |
*/ |
|---|
| 199 |
double read_double_c(int sock); |
|---|
| 200 |
|
|---|
| 201 |
/* |
|---|
| 202 |
* This is a function that read a string from a socket. |
|---|
| 203 |
* return a new 'mallocated' string or NULL if an error occours. |
|---|
| 204 |
* |
|---|
| 205 |
* Details: this function reads first the string length (as char) then |
|---|
| 206 |
* use malloc() to allocate new string so it can be read. |
|---|
| 207 |
* Remember! It's your own responsibility free() the string. |
|---|
| 208 |
*/ |
|---|
| 209 |
char* read_string_c(int sock); |
|---|
| 210 |
|
|---|
| 211 |
/* |
|---|
| 212 |
* This is a function that write a char to a socket. |
|---|
| 213 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 214 |
* For compatibility only: works exactly like write_char(). |
|---|
| 215 |
*/ |
|---|
| 216 |
int write_char_c(int sock, char c); |
|---|
| 217 |
|
|---|
| 218 |
/* |
|---|
| 219 |
* This is a function that write a short (as chars) to a socket. |
|---|
| 220 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 221 |
*/ |
|---|
| 222 |
int write_short_c(int sock, short s); |
|---|
| 223 |
|
|---|
| 224 |
/* |
|---|
| 225 |
* This is a function that write an integer (as chars) to a socket. |
|---|
| 226 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 227 |
*/ |
|---|
| 228 |
int write_int_c(int sock, int i); |
|---|
| 229 |
|
|---|
| 230 |
/* |
|---|
| 231 |
* This is a function that write a long (as chars) to a socket. |
|---|
| 232 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 233 |
*/ |
|---|
| 234 |
int write_long_c(int sock, long l); |
|---|
| 235 |
|
|---|
| 236 |
/* |
|---|
| 237 |
* This is a function that write a float (as chars) to a socket. |
|---|
| 238 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 239 |
*/ |
|---|
| 240 |
int write_float_c(int sock, float f); |
|---|
| 241 |
|
|---|
| 242 |
/* |
|---|
| 243 |
* This is a function that write a double (as chars) to a socket. |
|---|
| 244 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 245 |
*/ |
|---|
| 246 |
int write_double_c(int sock, double d); |
|---|
| 247 |
|
|---|
| 248 |
/* |
|---|
| 249 |
* This is a function that write a string to a socket. |
|---|
| 250 |
* return the number of written chars, or a number < 0 if an error occours. |
|---|
| 251 |
* |
|---|
| 252 |
* Details: this function writes first the string length (as chars) then |
|---|
| 253 |
* the string itself, so read_string_c() can read it. |
|---|
| 254 |
*/ |
|---|
| 255 |
int write_string_c(int sock, char* string); |
|---|
| 256 |
|
|---|
| 257 |
#endif |
|---|
| 258 |
|
|---|