Example #1
0
/**
 * Format and send an HTTP get command. The return value will be 0
 * on success, -1 on failure, with errno set appropriately. The caller
 * must then retrieve the response.
 */
int http_get( int connection, const char *path, const char *host,
              TLSParameters *tls_context )
{
  static char get_command[ MAX_GET_COMMAND ];

  sprintf( get_command, "GET /%s HTTP/1.1\r\n", path );

  if ( tls_send( connection, get_command, 
       strlen( get_command ), 0, tls_context ) == -1 )
  {
    return -1;
  }

  sprintf( get_command, "Host: %s\r\n", host );
  if ( tls_send( connection, get_command, 
       strlen( get_command ), 0, tls_context ) == -1 )
  {
    return -1;
  }

  sprintf( get_command, "Connection: close\r\n\r\n" );
  if ( tls_send( connection, get_command, 
       strlen( get_command ), 0, tls_context ) == -1 )
  {
    return -1;
  }

  return 0;
}
IoT_Error_t iot_tls_write(Network *pNetwork, unsigned char *pMsg, size_t len,
			  Timer *timer, size_t *written_len)
{
	if (tls_handle) {
		*written_len = tls_send(tls_handle, pMsg, len);
		return AWS_SUCCESS;
	}
	return AWS_FAILURE;
}
Example #3
0
File: ji.c Project: placek/ji
static int
io_send(int bytes, const char *buf, void *user)
{
  int i;
  if (log_level >= 10)
    for (i = 0; i < bytes; i++)
      if (!isspace(buf[i])) {
        log_printf(10, "\n-> %c[%d] %.*s\n\n", (in_tls) ? '&' : ' ', bytes,
                   bytes, buf);
        break;
      }
  return (in_tls) ? tls_send(bytes, buf, &tls) : tcp_send(bytes, buf, user);
}
Example #4
0
NS_INTERNAL int tls_cl_hello(SSL *ssl) {
  int i = 0;
  struct tls_cl_hello hello;

  /* hello */
  hello.type = HANDSHAKE_CLIENT_HELLO;
  hello.len_hi = 0;
  hello.len = htobe16(sizeof(hello) - 4);
  hello.version = htobe16(0x0303);
  hello.random.time = htobe32(time(NULL));
  if (!kr_get_random(hello.random.opaque, sizeof(hello.random.opaque))) {
    ssl_err(ssl, SSL_ERROR_SYSCALL);
    return 0;
  }
  hello.sess_id_len = 0;
#if ALLOW_NULL_CIPHERS
  /* if we allow them, it's for testing reasons, so NULL comes first */
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_NULL_MD5);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_AES_128_CBC_SHA256);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_AES_128_CBC_SHA);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_RC4_128_SHA);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_RC4_128_MD5);
  hello.cipher_suite[i++] = htobe16(TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
#else
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_AES_128_CBC_SHA256);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_AES_128_CBC_SHA);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_RC4_128_SHA);
  hello.cipher_suite[i++] = htobe16(TLS_RSA_WITH_RC4_128_MD5);
  hello.cipher_suite[i++] = htobe16(TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
#endif
  hello.cipher_suites_len = htobe16(i * 2);
  hello.num_compressors = 1;
  hello.compressor[0] = COMPRESSOR_NULL;
  hello.ext_len = htobe16(sizeof(hello.ext_reneg));

  hello.ext_reneg.type = htobe16(EXT_RENEG_INFO);
  hello.ext_reneg.len = htobe16(1);
  hello.ext_reneg.ri_len = 0;

  if (!tls_send(ssl, TLS_HANDSHAKE, &hello, sizeof(hello))) return 0;
  SHA256_Update(&ssl->nxt->handshakes_hash, ((uint8_t *) &hello),
                sizeof(hello));

  /* store the random we generated */
  memcpy(&ssl->nxt->cl_rnd, &hello.random, sizeof(ssl->nxt->cl_rnd));

  return 1;
}
Example #5
0
int get_string_handler(worker_st *ws, unsigned http_ver)
{
int ret;
const char *data;
int len;

	oclog(ws, LOG_HTTP_DEBUG, "requested fixed string: %s", ws->req.url); 
	if (!strcmp(ws->req.url, "/2/binaries/update.txt")) {
		data = VPN_VERSION;
		len = sizeof(VPN_VERSION)-1;
	} else {
		data = XML_START;
		len = sizeof(XML_START)-1;
	}

	tls_cork(ws->session);
	ret = tls_printf(ws->session, "HTTP/1.%u 200 OK\r\n", http_ver);
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "Connection: Keep-Alive\r\n");
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "Content-Type: text/xml\r\n");
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "X-Transcend-Version: 1\r\n");
	if (ret < 0)
		return -1;

	ret = tls_printf(ws->session, "Content-Length: %d\r\n\r\n", len);
	if (ret < 0)
		return -1;
		
	ret = tls_send(ws->session, data, len);
	if (ret < 0)
		return -1;

	ret = tls_uncork(ws->session);
	if (ret < 0)
		return -1;
	
	return 0;
}
Example #6
0
int get_dl_handler(worker_st *ws, unsigned http_ver)
{
int ret;
const char *data;
int len;

	oclog(ws, LOG_HTTP_DEBUG, "requested downloader: %s", ws->req.url); 

	data = SH_SCRIPT;
	len = sizeof(SH_SCRIPT)-1;

	tls_cork(ws->session);
	ret = tls_printf(ws->session, "HTTP/1.%u 200 OK\r\n", http_ver);
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "Connection: Keep-Alive\r\n");
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "Content-Type: application/x-shellscript\r\n");
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "X-Transcend-Version: 1\r\n");
	if (ret < 0)
		return -1;

	ret = tls_printf(ws->session, "Content-Length: %d\r\n\r\n", len);
	if (ret < 0)
		return -1;
		
	ret = tls_send(ws->session, data, len);
	if (ret < 0)
		return -1;

	ret = tls_uncork(ws->session);
	if (ret < 0)
		return -1;
	
	return 0;
}
/*Helper function to send a buffer over a connection.
 */
int httpd_send(int conn, const char *buf, int len)
{
	int num;
	do {
#ifdef CONFIG_ENABLE_HTTPS
		if (httpd_is_https_active())
			num = tls_send(httpd_tls_handle, buf, len);
		else
#endif /* ENABLE_HTTPS */
			num = send(conn, buf, len, 0);
		if (num < 0) {
			httpd_e("send() failed: %d",
			      net_get_sock_error(conn));
			return -WM_FAIL;
		}
		len -= num;
		buf += len;
	} while (len > 0);


	return WM_SUCCESS;
}
Example #8
0
int get_empty_handler(worker_st *ws, unsigned http_ver)
{
int ret;

	tls_cork(ws->session);
	ret = tls_printf(ws->session, "HTTP/1.%u 200 OK\r\n", http_ver);
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "Connection: Keep-Alive\r\n");
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "Content-Type: text/html\r\n");
	if (ret < 0)
		return -1;

	ret = tls_printf(ws->session, "Content-Length: %u\r\n", (unsigned int)sizeof(empty_msg)-1);
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "X-Transcend-Version: 1\r\n");
	if (ret < 0)
		return -1;

	ret = tls_puts(ws->session, "\r\n");
	if (ret < 0)
		return -1;

	ret = tls_send(ws->session, empty_msg, sizeof(empty_msg)-1);
	if (ret < 0)
		return -1;
	
	ret = tls_uncork(ws->session);
	if (ret < 0)
		return -1;
	
	return 0;
}
Example #9
0
void write_line_mud(const wchar_t *line)
{
    int len, OriginalLen;
    char buff[BUFFER_SIZE*8], coded[BUFFER_SIZE*4];
    int ret = 0;

    if ( !MUDSocket ) {
        tintin_puts(rs::rs(1182) );
    } else {
        OriginalLen = len = wcslen(line);

		wcscpy(strLastCommand, line);

		int count;
		if (MudCodePageUsed == 1200) {
			count = len * 2;
			memcpy(coded, line, count);
		} else if (MudCodePageUsed == 1201) {
			count = len * 2;
			utf16le_to_utf16be((wchar_t*)coded, line, len);
		} else {
			count = WideCharToMultiByte(MudCodePageUsed, 0, &line[ret], len, coded, sizeof(coded) - 1, NULL, NULL);
		}
        
		if ( bIACSendSingle ) 
			memcpy (buff, coded, count);
		else {
			char* ptr = buff;
			int nIACs = 0;
			for ( int i = 0; i < count; i ++ ) {
				if ( coded[i] == (char)0xff ) {
					*ptr++ = (char)0xff;
					nIACs ++;
				}
				*ptr++ = coded[i];
			}
			count += nIACs;

		}
		if ( buff[count-1] != 0xA ) {
			buff[count] = 0xA;
			buff[count+1] = 0;
			count ++;
		}

		int sent = tls_send(MUDSocket, buff, count);

		if (sent < 0)
			ret = sent;
		else
			ret = len;
    }

//* en
	wchar_t daaString[BUFFER_SIZE+2];
	int daalen = 0;
	if (bDaaMessage) {
		daaString[daalen++] = L'<';
		for(int i = 0; i < wcslen(line) && i < BUFFER_SIZE; i++)
			daaString[daalen++]=L'*';
		daaString[daalen++] = L'>';
	}
	daaString[daalen] = 0;

    if (bDisplayInput && !(SocketFlags & SOCKECHO) && wcslen(line) > 0) 
	{
        std::wstring str;
		str = USER_INPUT_MARK;
        str += L"\x1B[0;33m";
		str += bDaaMessage ? daaString : line;
        str += L"\x1B[0m";
        tintin_puts2(str.c_str());

		if (hLogFile) {
			log(processLine(bDaaMessage ? daaString : line));
			log(L"\n");
		}
		add_line_to_scrollbuffer(bDaaMessage ? daaString : line);
    }    

//* en
	bDaaMessage = FALSE;
//* /en


#ifdef _DEBUG_LOG
    // --------   Write external log
    if (hExLog ) {
        char exLogText[128];
        DWORD Written;
        swprintf(exLogText , L"\r\n#SEND got %d bytes sent %d bytes#\r\n" , len, ret );
        WriteFile(hExLog , exLogText , wcslen(exLogText) , &Written, NULL);
        WriteFile(hExLog , buff , len , &Written, NULL);
    }
#endif
    if (  ret < 0 ) {
        tintin_puts2(rs::rs(1183) );
    }
}
Example #10
0
/* Send a char buffer to the client. Traffic throttling is handled here.
 */
static int send_to_client(t_session *session, const char *buffer, int size) {
	int bytes_sent = 0, total_sent = 0, can_send, rest;
	time_t new_time;

	/* Send buffer to browser.
	 */
	if (session->socket_open == false) {
		return -1;
	} else if ((buffer == NULL) || (size <= 0)) {
		return 0;
	}

	if (session->directory != NULL) {
		if (session->directory->session_speed > 0) {
			session->throttle = session->directory->session_speed;
		}
	}

	do {
		rest = size - total_sent;
		if (session->throttle > 0) {
			do {
				new_time = time(NULL);
				if (session->throttle_timer < new_time) {
					session->bytecounter = 0;
					session->throttle_timer = new_time;
				}
				can_send = session->throttle - session->bytecounter;
				if (can_send <= 0) {
					usleep(10000);
				}
			} while (can_send <= 0);
			if (can_send > rest) {
				can_send = rest;
			}
		} else {
			can_send = rest;
		}

#ifdef ENABLE_TLS
		if (session->binding->use_tls) {
			if ((bytes_sent = tls_send(&(session->tls_context), (char*)(buffer + total_sent), can_send)) <= 0) {
				bytes_sent = -1;
			}
		} else
#endif
			if ((bytes_sent = send(session->client_socket, (char*)(buffer + total_sent), can_send, 0)) <= 0) {
				bytes_sent = -1;
			}

		/* Handle read result
		 */
		if (bytes_sent == -1) {
			if (errno != EINTR) {
				if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
					if (session->config->log_timeouts) {
						log_error(session, "send timeout");
					}
				} else if ((errno != EPIPE) && (errno != ECONNRESET)) {
					log_error(session, "error while sending response");
				}
				close_socket(session);
				session->keep_alive = false;
				session->error_cause = ec_SOCKET_WRITE_ERROR;
				return -1;
			}
		} else {
			total_sent += bytes_sent;
			session->bytecounter += bytes_sent;
		}
	} while (total_sent < size);
#ifdef ENABLE_TOMAHAWK
	increment_transfer(TRANSFER_SEND, total_sent);
#endif

	return 0;
}