1 /*********************************************************************** 2 * network.h: network related utility functions 3 *********************************************************************** 4 * Copyright (C) 2007 metro <me_t_ro@yahoo.com> 5 * 6 * This file is part of msdl, media stream downloader 7 * 8 * network non-related stuff should go msdllib.h 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU 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 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, write to the Free Software 22 * Foundation, Inc., 23 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. 24 * 25 ***********************************************************************/ 26 27 28 29 #ifndef __NETWORK_H__ 30 #define __NETWORK_H__ 31 32 #include <inttypes.h> 33 #include "url.h" 34 35 36 /* 37 * default ports. MMSH is MMS over HTTP, so HTTP port is used. 38 */ 39 enum { 40 HTTP_PORT = 80, 41 HTTP_PROXY_PORT = 8080, 42 MMS_PORT = 1755, 43 RTSP_PORT = 554, 44 FTP_PORT = 21, 45 }; 46 47 48 49 /* 50 * protocol type listing 51 */ 52 enum { 53 UNKNOWN_PROTOCOL = 0, /* unknown protocol */ 54 MMST, /* mms over tcp */ 55 MMSH, /* mms over http */ 56 HTTP, /* http */ 57 RTSP, /* rtsp (real time streaming protocol) */ 58 FTP, /* ftp */ 59 RTSP_REAL, /* rtsp - real /helix */ 60 RTSP_WMS, /* rtsp - windows media server */ 61 }; 62 63 64 65 /* 66 * buffer sizes. don't touch BUF_SIZE !!! 67 */ 68 enum { 69 BUF_SIZE = 1024000, /* buffer for netsock->buffer, write_buffer */ 70 BUFSIZE_4_DL = 10240, /* 10kb */ 71 BUFSIZE_1K = 1024, 72 }; 73 74 75 76 enum { 77 INT_MAX_BANDWIDTH = 0x7fffffff, /* INT MAX as maximum bandwidth */ 78 }; 79 80 81 82 enum { 83 SERVER_CONNECT_TIMEOUT = 12, /* 12 sec */ 84 GET_DATA_TIMEOUT = 180, /* 3 Minutes */ 85 XRECV_TIMEOUT = 180, 86 }; 87 88 89 struct serverinfo_t { 90 char *connect_host; /* points to host or proxy_host,the host which msdl directory connects to */ 91 int connect_port; 92 93 char *host; 94 int port; 95 96 char *proxy_host; 97 int proxy_port; 98 }; 99 100 101 102 struct netsock_t { 103 int sock; /* socket to get stream from */ 104 105 uint8_t *buffer; /* buffer for data which read from network */ 106 uint32_t buffer_size; /* size of malloc() [physical size of buffer] */ 107 uint32_t data_len; /* how many bytes to be read from now */ 108 uint32_t buffer_pos; /* read by here so far */ 109 }; 110 111 112 struct resumeinfo_t { 113 int resume_req_success; /* seek request for resume succeeded */ 114 uint64_t resume_start_offset; /* start writing to file from here */ 115 }; 116 117 118 119 /* 120 * specifies stream. 121 */ 122 struct stream_t { 123 struct serverinfo_t *serverinfo; /* connection information */ /* new */ 124 struct netsock_t *netsock; /* socket and network buffer */ /* new */ 125 struct stream_ctrl_t *stream_ctrl; /* status of the stream */ /* new */ 126 struct resumeinfo_t *resumeinfo; /* information for resuming */ /* new */ 127 char *localfile; /* name of local file */ /* new */ 128 129 struct download_opts_t *dlopts; /* download options */ /* pointer */ 130 struct url_t *url; /* url to download. */ /* pointer */ 131 132 133 134 /* downloading functions */ 135 int (*start)(struct stream_t *); /* stream starter */ 136 int (*read)(struct stream_t *, uint8_t *,size_t); /* stream reader */ 137 void (*close)(struct stream_t *); /* stream closer */ 138 }; 139 140 141 142 /* 143 * streaming status which goes to steram_ctrl->status 144 */ 145 enum { 146 STREAMING_HANDSHAKING, /* handshaking, doing setup */ 147 STREAMING_DOWNLOADING, /* downloading stream */ 148 STREAMING_FINISHED, /* end of steam packet received, etc. */ 149 STREAMING_REWIND, /* rewind before write data */ 150 STREAMING_RESUME_BUFFERING, /* buffering to seek to undownloaded point */ 151 STREAMING_NO_NEED_TO_DOWNLOAD, /* file already downloaded */ 152 STREAMING_OTHER_PROTOCOL, /* use other protocol to download this url */ 153 STREAMING_REDIRECTED, /* has to download from different location */ 154 }; 155 156 157 158 /* 159 * protocol unspecific datas. 160 */ 161 struct stream_ctrl_t { 162 int protocol; /* downloading protocol */ 163 164 int packet_length; /* length of each packet for this stream. */ 165 166 uint64_t file_size; /* size of downloading file */ 167 168 int total_packets; /* number of packets to be received. */ 169 int packet_count; /* how many packet received */ 170 171 uint8_t *write_buffer; /* data to write to file (buffer) */ 172 uint32_t write_buffer_size; /* malloc()ed size */ 173 uint32_t write_data_len; /* how many bytes to be written from now */ 174 uint32_t write_pos; /* written by here so far */ 175 176 unsigned int bandwidth; /* bandwidth */ 177 int status; /* playing status */ 178 int retry_protocol; /* protocol to try again */ 179 char *retry_urlstr; /* url to try again */ 180 181 union { 182 void *data; /* protocol specific datas goes here */ 183 struct mmst_ctrl_t *mmst_ctrl; /* easy access for mmst_ctrl_t */ 184 struct mmsh_ctrl_t *mmsh_ctrl; /* same as above. */ 185 struct http_ctrl_t *http_ctrl; /* same as above. */ 186 struct rtsp_ctrl_t *rtsp_ctrl; /* same */ 187 struct ftp_ctrl_t *ftp_ctrl; /* same */ 188 }; 189 }; 190 191 192 193 struct stream_t *new_stream_t(void); 194 void free_stream_t(struct stream_t *st); 195 196 struct stream_t *streaming_init_common(); 197 void streaming_close_common(struct stream_t *stream); 198 199 struct netsock_t *new_netsock_t(void); 200 void free_netsock_t(struct netsock_t *ns); 201 202 struct serverinfo_t *new_serverinfo_t(void); 203 void free_serverinfo_t(struct serverinfo_t *si); 204 205 struct stream_ctrl_t *new_stream_ctrl_t(void); 206 void free_stream_ctrl_t(struct stream_ctrl_t *sc); 207 208 struct resumeinfo_t *new_resumeinfo_t(void); 209 void free_resumeinfo_t(struct resumeinfo_t *ri); 210 211 void set_serverinfo(struct serverinfo_t *serverinfo, 212 char *target_host,int target_port, 213 char *proxy_host,int proxy_port,int protocol_default_port); 214 void set_serverinfo_by_proxy_string(struct serverinfo_t *serverinfo, 215 char *host,int port,char *proxy_string, 216 int protocol_default_port,int proxy_default_port); 217 218 char *create_time_str_from_many_formats(const char *str,char **reason_ret); 219 int speed_valid_and_guess(const char *str,int *guessed_speed,char **reason_ret); 220 char *make_byterange_from_filesize(uint64_t filesize); 221 int protocol_type_from_string(char *protocol); 222 223 int sock_check_data(int sock,const double timeout); 224 225 int server_connect(const char *servername,const int port); 226 int server_connect_with_timeout(const char *servername,const int port,const double timeout); 227 228 int waiting_socket(int family,int port); 229 int accept_connection(int wait_sock); 230 231 int xrecv(int sock,void *buf,size_t count); 232 int xsend(int sock,void *buf,size_t count); 233 234 int stream_check_data(struct stream_t *stream,const double timeout); 235 int get_data(int sock, void *buf, size_t count); 236 int read_data(struct stream_t *stream, void *buffer, size_t size); 237 int recv_data(struct stream_t *stream, void *buffer, size_t max); 238 int stream_data_push_back(struct stream_t *stream,void *buffer,int size); 239 240 241 #endif /* __NETWORK_H__ */ 242