+/*********************************************************************
+ *
+ * Function : convert_ipv4_address_to_bytes
+ *
+ * Description : Converts an IPv4 address from string to bytes.
+ *
+ * Parameters :
+ * 1 : address = The IPv4 address string to convert.
+ * 2 : buf = The buffer to write the bytes to.
+ * Must be at least four bytes long.
+ *
+ * Returns : JB_ERR_OK on success, JB_ERR_PARSE otherwise.
+ *
+ *********************************************************************/
+static jb_err convert_ipv4_address_to_bytes(const char *address, char *buf)
+{
+ int i;
+ const char *p = address;
+
+ for (i = 0; i < 4; i++)
+ {
+ unsigned byte;
+ if (1 != sscanf(p, "%u", &byte))
+ {
+ return JB_ERR_PARSE;
+ }
+ if (byte > 255)
+ {
+ return JB_ERR_PARSE;
+ }
+ buf[i] = (char)byte;
+ if (i < 3)
+ {
+ p = strstr(p, ".");
+ if (p == NULL)
+ {
+ return JB_ERR_PARSE;
+ }
+ p++;
+ }
+ }
+
+ return JB_ERR_OK;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : read_socks_reply
+ *
+ * Description : Read from a socket connected to a socks server.
+ *
+ * Parameters :
+ * 1 : sfd = file descriptor of the socket to read
+ * 2 : buf = pointer to buffer where data will be written
+ * Must be >= len bytes long.
+ * 3 : len = maximum number of bytes to read
+ * 4 : timeout = Number of seconds to wait.
+ *
+ * Returns : On success, the number of bytes read is returned (zero
+ * indicates end of file), and the file position is advanced
+ * by this number. It is not an error if this number is
+ * smaller than the number of bytes requested; this may hap-
+ * pen for example because fewer bytes are actually available
+ * right now (maybe because we were close to end-of-file, or
+ * because we are reading from a pipe, or from a terminal,
+ * or because read() was interrupted by a signal). On error,
+ * -1 is returned, and errno is set appropriately. In this
+ * case it is left unspecified whether the file position (if
+ * any) changes.
+ *
+ *********************************************************************/
+static int read_socks_reply(jb_socket sfd, char *buf, int len, int timeout)
+{
+ if (!data_is_available(sfd, timeout))
+ {
+ if (socket_is_still_alive(sfd))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "The socks connection timed out after %d seconds.", timeout);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_ERROR, "The socks server hung "
+ "up the connection without sending a response.");
+ }
+ return -1;
+ }
+
+ return read_socket(sfd, buf, len);
+
+}
+
+