00001 /* Net.h some stuff to wrap around net communications. 00002 * 00003 * This file is copyright 2002 Jim Kent, but license is hereby 00004 * granted for all use - public, private or commercial. */ 00005 00006 00007 #ifndef NET_H 00008 #define NET_H 00009 00010 #ifndef LINEFILE_H 00011 #include "linefile.h" 00012 #endif /* LINEFILE_H */ 00013 00014 #ifndef DYSTRING_H 00015 #include "dystring.h" 00016 #endif /* DYSTRING_H */ 00017 00018 int netConnect(char *hostName, int port); 00019 /* Start connection with a server having resolved port. */ 00020 00021 int netMustConnect(char *hostName, int port); 00022 /* Start connection with server or die. */ 00023 00024 int netMustConnectTo(char *hostName, char *portName); 00025 /* Start connection with a server and a port that needs to be converted to integer */ 00026 00027 int netAcceptingSocket(int port, int queueSize); 00028 /* Create a socket for to accept connections. */ 00029 00030 int netAcceptingSocketFrom(int port, int queueSize, char *host); 00031 /* Create a socket that can accept connections from a 00032 * IP address on the current machine if the current machine 00033 * has multiple IP addresses. */ 00034 00035 int netAccept(int sd); 00036 /* Accept incoming connection from socket descriptor. */ 00037 00038 int netAcceptFrom(int sd, unsigned char subnet[4]); 00039 /* Wait for incoming connection from socket descriptor 00040 * from IP address in subnet. Subnet is something 00041 * returned from netParseDottedQuad. */ 00042 00043 FILE *netFileFromSocket(int socket); 00044 /* Wrap a FILE around socket. This should be fclose'd 00045 * and separately the socket close'd. */ 00046 00047 void netBlockBrokenPipes(); 00048 /* Make it so a broken pipe doesn't kill us. */ 00049 00050 int netReadAll(int sd, void *vBuf, size_t size); 00051 /* Read given number of bytes into buffer. 00052 * Don't give up on first read! */ 00053 00054 int netMustReadAll(int sd, void *vBuf, size_t size); 00055 /* Read given number of bytes into buffer or die. 00056 * Don't give up if first read is short! */ 00057 00058 boolean netSendString(int sd, char *s); 00059 /* Send a string down a socket - length byte first. */ 00060 00061 boolean netSendLongString(int sd, char *s); 00062 /* Send a string down a socket - up to 64k characters. */ 00063 00064 boolean netSendHugeString(int sd, char *s); 00065 /* Send a string down a socket - up to 4G characters. */ 00066 00067 char *netRecieveString(int sd, char buf[256]); 00068 /* Read string into buf and return it. If buf is NULL 00069 * an internal buffer will be used. Abort if any problem. */ 00070 00071 char *netRecieveLongString(int sd); 00072 /* Read string up to 64k and return it. freeMem 00073 * the result when done. Abort if any problem*/ 00074 00075 char *netRecieveHugeString(int sd); 00076 /* Read string up to 4G and return it. freeMem 00077 * the result when done. Abort if any problem*/ 00078 00079 char *netGetString(int sd, char buf[256]); 00080 /* Read string into buf and return it. If buf is NULL 00081 * an internal buffer will be used. Print warning message 00082 * and return NULL if any problem. */ 00083 00084 char *netGetLongString(int sd); 00085 /* Read string up to 64k and return it. freeMem 00086 * the result when done. Print warning message and 00087 * return NULL if any problem. */ 00088 00089 char *netGetHugeString(int sd); 00090 /* Read string up to 4 gig and return it. freeMem 00091 * the result when done. Print warning message and 00092 * return NULL if any problem. */ 00093 00094 void netCatchPipes(); 00095 /* Set up to catch broken pipe signals. */ 00096 00097 boolean netPipeIsBroken(); 00098 /* Return TRUE if pipe is broken */ 00099 00100 void netClearPipeFlag(); 00101 /* Clear broken pipe flag. */ 00102 00103 void netParseSubnet(char *in, unsigned char out[4]); 00104 /* Parse subnet, which is a prefix of a normal dotted quad form. 00105 * Out will contain 255's for the don't care bits. */ 00106 00107 struct netParsedUrl 00108 /* A parsed URL. */ 00109 { 00110 char protocol[16]; /* Protocol - http or ftp, etc. */ 00111 char user[128]; /* User name (optional) */ 00112 char password[128]; /* Password (optional) */ 00113 char host[128]; /* Name of host computer - www.yahoo.com, etc. */ 00114 char port[16]; /* Port, usually 80 or 8080. */ 00115 char file[1024]; /* Remote file name/query string, starts with '/' */ 00116 }; 00117 00118 void netParseUrl(char *url, struct netParsedUrl *parsed); 00119 /* Parse a URL into components. A full URL is made up as so: 00120 * http://hostName:port/file 00121 * This is set up so that the http:// and the port are optional. 00122 */ 00123 00124 int netUrlOpen(char *url); 00125 /* Return unix low-level file handle for url. 00126 * Just close(result) when done. */ 00127 00128 struct hash; 00129 00130 int netUrlHead(char *url, struct hash *hash); 00131 /* Go get head and return status. Return negative number if 00132 * can't get head. If hash is non-null, fill it with header 00133 * lines, including hopefully Content-Type: */ 00134 00135 struct lineFile *netLineFileOpen(char *url); 00136 /* Return a lineFile attached to url. This one 00137 * will skip any headers. Free this with 00138 * lineFileClose(). */ 00139 00140 struct lineFile *netLineFileMayOpen(char *url); 00141 /* Same as netLineFileOpen, but warns and returns 00142 * null rather than aborting on problems. */ 00143 00144 struct lineFile *netLineFileMayOpenCatchError(char *url); 00145 /* Same as netLineFileMayOpen, but returns NULL 00146 * if there's a problem without printing error. */ 00147 00148 struct dyString *netSlurpFile(int sd); 00149 /* Slurp file into dynamic string and return. */ 00150 00151 struct dyString *netSlurpUrl(char *url); 00152 /* Go grab all of URL and return it as dynamic string. */ 00153 00154 struct lineFile *netHttpLineFileMayOpen(char *url, struct netParsedUrl **npu); 00155 /* Parse URL and open an HTTP socket for it but don't send a request yet. */ 00156 00157 void netHttpGet(struct lineFile *lf, struct netParsedUrl *npu, 00158 boolean keepAlive); 00159 /* Send a GET request, possibly with Keep-Alive. */ 00160 00161 int netOpenHttpExt(char *url, char *method, boolean end); 00162 /* Return a file handle that will read the url. If end is not 00163 * set then can send cookies and other info to returned file */ 00164 00165 int netHttpConnect(char *url, char *method, char *protocol, char *agent); 00166 /* Parse URL, connect to associated server on port, 00167 * and send most of the request to the server. If 00168 * specified in the url send user name and password 00169 * too. This does not send the final \r\n to finish 00170 * off the request, so that you can send cookies. 00171 * Typically the "method" will be "GET" or "POST" 00172 * and the agent will be the name of your program or 00173 * library. Protocol is usually HTTP/1.0. */ 00174 00175 int netHttpGetMultiple(char *url, struct slName *queries, void *userData, 00176 void (*responseCB)(void *userData, char *req, 00177 char *hdr, struct dyString *body)); 00178 /* Given an URL which is the base of all requests to be made, and a 00179 * linked list of queries to be appended to that base and sent in as 00180 * requests, send the requests as a batch and read the HTTP response 00181 * headers and bodies. If not all the requests get responses (i.e. if 00182 * the server is ignoring Keep-Alive or is imposing a limit), try again 00183 * until we can't connect or until all requests have been served. 00184 * For each HTTP response, do a callback. */ 00185 00186 boolean netSkipHttpHeaderLines(int sd, char *url); 00187 /* Skip http header lines. Return FALSE if there's a problem. 00188 The input is a standard sd or fd descriptor. 00189 This is meant to be able work even with a re-passable stream handle, 00190 e.g. can pass it to the pipes routines, which means we can't 00191 attach a linefile since filling its buffer reads in more than just the http header. 00192 */ 00193 00194 #endif /* NET_H */ 00195
1.5.2