| 1 | /* |
| 2 | ** io.h |
| 3 | ** |
| 4 | ** Copyright (c) 2004-2016 Ludovic Marcotte |
| 5 | ** Copyright (C) 2019-2020 Riccardo Mottola |
| 6 | ** |
| 7 | ** Author: Ludovic Marcotte <[email protected]> |
| 8 | ** Riccardo Mottola <[email protected]> |
| 9 | ** |
| 10 | ** This library is free software; you can redistribute it and/or |
| 11 | ** modify it under the terms of the GNU Lesser General Public |
| 12 | ** License as published by the Free Software Foundation; either |
| 13 | ** version 2.1 of the License, or (at your option) any later version. |
| 14 | ** |
| 15 | ** This library 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 GNU |
| 18 | ** Lesser General Public License for more details. |
| 19 | ** |
| 20 | ** You should have received a copy of the GNU General Public License |
| 21 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #ifndef _Pantomime_H_io |
| 25 | #define _Pantomime_H_io |
| 26 | |
| 27 | #include <sys/types.h> |
| 28 | #include <stdint.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | @class NSData; |
| 32 | @class NSString; |
| 33 | |
| 34 | /*! |
| 35 | @function read_block |
| 36 | @discussion This function is used to read <i>count</i> bytes |
| 37 | from <i>fd</i> and store them in <i>buf</i>. This |
| 38 | method blocks until it read all bytes or if |
| 39 | an error different from EINTR occurs. |
| 40 | @param fd The file descriptor to read bytes from. |
| 41 | @param buf The buffer where to store the read bytes. |
| 42 | @param count The number of bytes to read. |
| 43 | @result The number of bytes that have been read. |
| 44 | */ |
| 45 | ssize_t read_block(int fd, void *buf, size_t count); |
| 46 | |
| 47 | /*! |
| 48 | @function safe_close |
| 49 | @discussion This function is used to safely close a file descriptor. |
| 50 | This function will block until the file descriptor |
| 51 | is close, or if the error is different from EINTR. |
| 52 | @param fd The file descriptor to close. |
| 53 | @result Returns 0 on success, -1 if an error occurs. |
| 54 | */ |
| 55 | int safe_close(int fd); |
| 56 | |
| 57 | /*! |
| 58 | @function safe_read |
| 59 | @discussion This function is used to read <i>count</i> bytes |
| 60 | from <i>fd</i> and store them in <i>buf</i>. This |
| 61 | method might not block when reading if there are |
| 62 | no bytes available to be read. |
| 63 | @param fd The file descriptor to read bytes from. |
| 64 | @param buf The buffer where to store the read bytes. |
| 65 | @param count The number of bytes to read. |
| 66 | @result The number of bytes that have been read. |
| 67 | */ |
| 68 | ssize_t safe_read(int fd, void *buf, size_t count); |
| 69 | |
| 70 | /*! |
| 71 | @function safe_recv |
| 72 | @discussion This function is used to read <i>count</i> bytes |
| 73 | from <i>fd</i> and store them in <i>buf</i>. This |
| 74 | method might not block when reading if there are |
| 75 | no bytes available to be read. Options can be |
| 76 | passed through <i>flags</i>. |
| 77 | @param fd The file descriptor to read bytes from. |
| 78 | @param buf The buffer where to store the read bytes. |
| 79 | @param count The number of bytes to read. |
| 80 | @param flags The flags to use. |
| 81 | @result The number of bytes that have been read. |
| 82 | */ |
| 83 | ssize_t safe_recv(int fd, void *buf, size_t count, int flags); |
| 84 | |
| 85 | /*! |
| 86 | @function read_data_memory |
| 87 | @discussion This function is used to read a string from <i>m</i> |
| 88 | and return it as a NSData objectand adjust the <i>count</i> on how |
| 89 | long the read data is. The data must NOT be longer than 65535 bytes. |
| 90 | @param m The buffer to read from. |
| 91 | @param count The lenght of the data returned |
| 92 | @result The read NSData* (autoreleased) |
| 93 | */ |
| 94 | |
| 95 | NSData *read_data_memory(unsigned char *m, uint16_t *count); |
| 96 | |
| 97 | /*! |
| 98 | @function read_string_memory |
| 99 | @discussion This function is used to read a string from <i>m</i> |
| 100 | and adjust the <i>count</i> on how long the string is. |
| 101 | The string must NOT be longer than 65535 bytes. |
| 102 | @param m The buffer to read from. |
| 103 | @param count The lenght of the string stored in <i>buf</i> |
| 104 | @result The allocated string (autoreleased) |
| 105 | */ |
| 106 | NSString *read_string_memory(unsigned char *m, uint16_t *count); |
| 107 | |
| 108 | /*! |
| 109 | @function read_uint32_memory |
| 110 | @discussion This function is used to read an unsigned int from |
| 111 | the memory in network byte-order. |
| 112 | @param m The buffer to read from. |
| 113 | @result The unsigned integer read from memory. |
| 114 | */ |
| 115 | uint32_t read_uint32_memory(unsigned char *m); |
| 116 | |
| 117 | /*! |
| 118 | @function read_uint16 |
| 119 | @discussion This function is used to read an uint16 from |
| 120 | the file descriptor in network byte-order. |
| 121 | @param fd The file descriptor to read from. |
| 122 | @result The unsigned short read from the file descriptor. |
| 123 | */ |
| 124 | uint16_t read_uint16(int fd); |
| 125 | |
| 126 | /*! |
| 127 | @function write_uint16 |
| 128 | @discussion This function is used to write the specified |
| 129 | uint16 <i>value</i> to the file descriptor |
| 130 | </i>fd</i>. The written value is in network byte-order. |
| 131 | @param fd The file descriptor to write to. |
| 132 | @param value The unsigned value to write. |
| 133 | */ |
| 134 | void write_uint16(int fd, uint16_t value); |
| 135 | |
| 136 | /*! |
| 137 | @function read_string |
| 138 | @discussion This function is used to read a string from a |
| 139 | file descriptor, store it into a buffer and adjust |
| 140 | the number of bytes that has been read. |
| 141 | @param fd The file descriptor to read from. |
| 142 | @param buf The buf to write to. |
| 143 | @param count The number of bytes that have been declared as string length. |
| 144 | @result actual bytes of string read |
| 145 | */ |
| 146 | ssize_t read_string(int fd, char *buf, uint16_t *count); |
| 147 | |
| 148 | /*! |
| 149 | @function write_string |
| 150 | @discussion This function is used to string a string to a |
| 151 | file descriptor. |
| 152 | @param fd The file descriptor to write to. |
| 153 | @param buf The buf that needs to be written. |
| 154 | @param count The number of bytes that we have to write. |
| 155 | */ |
| 156 | void write_string(int fd, unsigned char *s, size_t len); |
| 157 | |
| 158 | /*! |
| 159 | @function write_string |
| 160 | @discussion This function is used to write NSData as a string to a |
| 161 | file descriptor. The whole length is written |
| 162 | @param fd The file descriptor to write to. |
| 163 | @param d The NSData that needs to be written. |
| 164 | */ |
| 165 | void write_data(int fd, NSData *d); |
| 166 | |
| 167 | /*! |
| 168 | @function read_uint32 |
| 169 | @discussion This function is used to read an uint32 from |
| 170 | the file descriptor in network byte-order. |
| 171 | @param fd The file descriptor to read from. |
| 172 | @result The unsigned int read from the file descriptor. |
| 173 | */ |
| 174 | int read_uint32(int fd, uint32_t *val); |
| 175 | |
| 176 | /*! |
| 177 | @function write_uint32 |
| 178 | @discussion This function is used to write the specified |
| 179 | uint32 <i>value</i> to the file descriptor |
| 180 | </i>fd</i>. The written value is in network byte-order. |
| 181 | @param fd The file descriptor to write to. |
| 182 | @param value The unsigned value to write. |
| 183 | */ |
| 184 | void write_uint32(int fd, uint32_t value); |
| 185 | |
| 186 | #endif // _Pantomime_H_io |