| 1 | // This file is part of SmallBASIC |
| 2 | // |
| 3 | // Network library (byte-stream sockets) |
| 4 | // |
| 5 | // This program is distributed under the terms of the GPL v2.0 or later |
| 6 | // Download the GNU Public License (GPL) from www.gnu.org |
| 7 | // |
| 8 | // Copyright(C) 2000 Nicholas Christopoulos |
| 9 | |
| 10 | #if !defined(INET_H) |
| 11 | #define INET_H |
| 12 | |
| 13 | #ifdef HAVE_CONFIG_H |
| 14 | #include <config.h> |
| 15 | #endif |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <errno.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #if defined(__cplusplus) |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | typedef int socket_t; |
| 26 | |
| 27 | /** |
| 28 | * @ingroup net |
| 29 | * |
| 30 | * initialize network driver |
| 31 | * |
| 32 | * @return non-zero on success |
| 33 | */ |
| 34 | int net_init(void); |
| 35 | |
| 36 | /** |
| 37 | * @ingroup net |
| 38 | * |
| 39 | * deinitialize network driver |
| 40 | * |
| 41 | * @return non-zero on success |
| 42 | */ |
| 43 | int net_close(void); |
| 44 | |
| 45 | /** |
| 46 | * @ingroup net |
| 47 | * |
| 48 | * writes a NULL terminated string to a socket |
| 49 | * |
| 50 | * @param s the socket |
| 51 | * @param str the string |
| 52 | */ |
| 53 | void net_print(socket_t s, const char *str); |
| 54 | |
| 55 | /** |
| 56 | * @ingroup net |
| 57 | * |
| 58 | * writes a string to a socket using printf-style |
| 59 | * |
| 60 | * @param s the socket |
| 61 | * @param fmt the format |
| 62 | * @param ... the format's parameters |
| 63 | */ |
| 64 | void net_printf(socket_t s, const char *fmt, ...); |
| 65 | |
| 66 | /** |
| 67 | * @ingroup net |
| 68 | * |
| 69 | * writes data to a socket |
| 70 | * |
| 71 | * @param s the socket |
| 72 | * @param str the string |
| 73 | */ |
| 74 | void net_send(socket_t s, const char *str, size_t size); |
| 75 | |
| 76 | /** |
| 77 | * @ingroup net |
| 78 | * |
| 79 | * reads a string from a socket until the size > from the size of the buffer |
| 80 | * or until one characters of 'delim' string found |
| 81 | * |
| 82 | * @note character \r will ignored |
| 83 | * |
| 84 | * @param s the socket |
| 85 | * @param buf a buffer to store the string |
| 86 | * @param size the size of the buffer |
| 87 | * @param delim the characters that terminates the string |
| 88 | * @return the number of the bytes that read |
| 89 | */ |
| 90 | int net_input(socket_t s, char *buf, int size, const char *delim); |
| 91 | |
| 92 | /** |
| 93 | * @ingroup net |
| 94 | * |
| 95 | * read the specified number of bytes from the socket |
| 96 | * |
| 97 | * @param s the socket |
| 98 | * @param buf a buffer to store the string |
| 99 | * @param size the size of the buffer |
| 100 | * @return the number of the bytes that read |
| 101 | */ |
| 102 | int net_read(socket_t s, char *buf, int size); |
| 103 | |
| 104 | /** |
| 105 | * @ingroup net |
| 106 | * |
| 107 | * connect to a server |
| 108 | * |
| 109 | * @param server_name the server's IP or domain-name |
| 110 | * @param server_port the port to connect |
| 111 | * @return on success the socket; otherwise -1 |
| 112 | */ |
| 113 | socket_t net_connect(const char *server_name, int server_port); |
| 114 | |
| 115 | /** |
| 116 | * @ingroup net |
| 117 | * |
| 118 | * listen on a port number like a server |
| 119 | * |
| 120 | * @param server_port the port to listen |
| 121 | * @return on success the socket; otherwise -1 |
| 122 | */ |
| 123 | socket_t net_listen(int server_port); |
| 124 | |
| 125 | /** |
| 126 | * @ingroup net |
| 127 | * |
| 128 | * disconnect |
| 129 | * |
| 130 | * @param s the socket |
| 131 | */ |
| 132 | void net_disconnect(socket_t s); |
| 133 | |
| 134 | /** |
| 135 | * @ingroup net |
| 136 | * |
| 137 | * returns true if something is waiting in input-buffer |
| 138 | * |
| 139 | * @param s the socket |
| 140 | * @return non-zero if something is waiting in input-buffer; otherwise returns 0 |
| 141 | */ |
| 142 | int net_peek(socket_t s); |
| 143 | |
| 144 | #if defined(__cplusplus) |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #endif |
| 149 | |