[gnutls-help] Data transfer - Sending Receiving buffer

udhayakumar udhayakumar at fatpipeinc.com
Fri Nov 18 05:24:41 CET 2022


Hi team,

             i have Faced issue on  Data transfer on buffers.  just like 
proxy . in Both end create and  establish the Connection. while Data 
transferring website has  Loading  infinity.  but i Got content 
partially. can any one help this!

i copied code here can any one help , or suggestion from the Data 
transfer both end.

---------------------------------------------------------------------------------------------------------------------------------

int
handle_epollin(tproxy_conn_t *conn) {

         int numbytes,i;
         int fd_in, fd_out, ret;
         char *data_buf;
         void *buff = malloc(80 * MEBYTE);
         void *sndbuff = malloc(2 * MEBYTE);
         const char *method, *path;
         int pret, minor_version;
         struct phr_header headers[100];
         size_t buflen = 0, prevbuflen = 0,method_len, path_len, 
num_headers;

         unsigned int server_session = 0;

         //Easy way to determin which socket is ready for reading
         //TODO: Optimize. This one allows me quick lookup for conn, but
         //I need to make a system call to determin which socket
         //FILE *stream;
         size_t bufferd_size = gnutls_record_check_pending(conn -> 
serv_session);
         if(bufferd_size)
                 log_trace("data contains on server Record %lu", 
bufferd_size);
         else
                 log_trace("data contains on server Record %lu", 
bufferd_size);

         bufferd_size = gnutls_record_check_pending(conn -> cli_session);
         if(bufferd_size)
                 log_trace("data contains on client Record %lu", 
bufferd_size);
         else
                 log_trace("data contains on client Record %lu", 
bufferd_size);

         if(ioctl(conn->local_fd, FIONREAD, &numbytes) != -1
                         && numbytes > 0){

                 server_session = 1;
                 log_trace("IN:[Server Socket is READY for Reading]");
                 fd_in = conn->local_fd;
                 fd_out = conn->remote_fd;

         } else {
                 server_session = 0;
                 log_trace("IN:[client Socket is READY for 
Reading(origin server)]");
                 fd_in = conn->remote_fd;
                 fd_out = conn->local_fd;
         }

         //Optimize with SPLICE_F_MORE later
         /*numbytes = splice(fd_in, NULL, conn->splice_pipe[1], NULL,
           SPLICE_LEN, SPLICE_F_MOVE);*/

         log_trace("Hostname : %s", conn -> hostname);

         int direction = gnutls_record_get_direction(conn -> serv_session);
         if(direction)
                 log_trace("[gnutls serv write Data Direction ]");
         else
                 log_trace("[gnutls serv read Data Direction is ");

         direction = gnutls_record_get_direction(conn -> cli_session);
         if(direction)
                 log_trace("[origin Client write Data Direction ]");
         else
      log_trace("[origin Client write Data Direction ]");

         if(server_session) {

                 log_trace("check : Entering Server Session");
                 memset(buff,'\0', MAX_BUF);

                 LOOP_CHECK(ret, gnutls_record_recv(conn -> 
serv_session, buff , MAX_BUF));
                 if (ret == 0) {
                         log_error("\n- Peer has closed the GnuTLS 
connection");

                 } else if (ret < 0 && gnutls_error_is_fatal(ret) == 0) {

                         log_error( "*** Warning: 
%s\n",gnutls_strerror(ret));
                 } else if (ret < 0) {
                         log_error("\n*** Received corrupted "
                                         "data(%d). Closing the 
connection.\n", ret);
                         return -1;
                 } else if (ret > 0) {

                 if(read_done == false) {
                         read_done = true;
                         numbytes = ret;
                         prevbuflen = buflen;
                         buflen += ret;
                         num_headers = sizeof(headers) / sizeof(headers[0]);
                         pret = phr_parse_request(buff, buflen, &method, 
&method_len, &path, &path_len,
                                         &minor_version, headers, 
&num_headers, prevbuflen);

                         if (pret > 0)
                                 log_trace("parsering success"); /* 
successfully parsed the request */
                         else if (pret == -1) {
                                 log_error("PArse the Request 
incompleted!");
                                 //TODO: expermental  Need to receive 
again by check checking tls records
                                 return 0; /* request is incomplete, 
continue the loop */
                         }

                         if(pret == -2);
                         if (buflen == sizeof(buff)){
                                 log_error(" Error: To long the Buffer 
Failed to Parse the Request");
                                 return 0;
                         }


                         log_debug("request is %d bytes long", pret);
                         log_debug("**method is %.*s", (int)method_len, 
method);
                         log_debug("**path is %.*s", (int)path_len, path);
                         log_debug("**HTTP version is 1.%d", minor_version);
                         log_debug("**headers:");
                         for (i = 0; i != num_headers; ++i) {
                                 log_debug("**%.*s: %.*s", 
(int)headers[i].name_len, headers[i].name,
(int)headers[i].value_len, headers[i].value);
                         }

                         //"GET / HTTP/1.1\r\nHost:www.google.org\r\n\r\n"
                         memset(sndbuff,'\0', MAX_BUF);
                         //sprintf(sndbuff,"%s %s 
HTTP/1.%d\r\nHost:%s\r\n\n",method,path,minor_version,conn -> hostname);
                         sprintf(sndbuff,"%s %s 
HTTP/1.%d\r\nHost:%s\r\n\n","GET","/",1,conn -> hostname);

                         log_debug(" ---- Sndbuff 
contains:%s-----",(char *)sndbuff);
                         /*if(numbytes > 0){
                           numbytes = splice(conn->splice_pipe[0], NULL, 
fd_out, NULL,
                           numbytes, SPLICE_F_MOVE);*/
                         //numbytes = splice_all(conn -> pipes[0], bp, 
SPLICE_LEN);
                         log_debug("IN: Handle EPOLL IN inside client 
session");

                         LOOP_CHECK(ret, gnutls_record_send(conn -> 
cli_session, sndbuff, strlen(buff)));
                         memset(buff,'\0', MAX_BUF);
                         }
                         else {
                                 LOOP_CHECK(ret, gnutls_record_send(conn 
-> cli_session, buff, strlen(buff)));
                                 memset(buff,'\0', MAX_BUF);
                         }
                 }

                 } else {
                         memset(buff,'\0', MAX_BUF);
                         LOOP_CHECK(ret, gnutls_record_recv(conn -> 
cli_session, buff , MAX_BUF));

                         if (ret == 0) {
                                 log_debug("\n- Peer has closed the 
GnuTLS connection");
                         } else if (ret < 0
                                         && gnutls_error_is_fatal(ret) 
== 0) {
                                 log_error("*** Warning: %s",
gnutls_strerror(ret));
                         } else if (ret < 0) {
                                 log_error("\n*** Received corrupted "
                                                 "data(%d). Closing the 
connection.\n", ret);
                                 return -1;
                         } else if (ret > 0) {
                                 numbytes = ret;
                                 /*if(numbytes > 0){
                                   numbytes = 
splice(conn->splice_pipe[0], NULL, fd_out, NULL,
                                   numbytes, SPLICE_F_MOVE);*/

                         LOOP_CHECK(ret, gnutls_record_send(conn -> 
serv_session, buff, strlen(buff)));
                         //ret = gnutls_record_send(conn -> 
serv_session, buff, strlen(buff));
                         memset(buff,'\0', MAX_BUF);

                         if (ret == 0) {
                                 log_debug("\n- Peer has closed the 
GnuTLS connection");
                         } else if (ret < 0
                                         && gnutls_error_is_fatal(ret) 
== 0) {
                                 log_error("*** Warning: %s\n",
gnutls_strerror(ret));
                         } else if (ret < 0) {
                                 log_error("*** Received corrupted "
                                                 "data(%d). Closing the 
connection.\n", ret);
                                 return -1;
                         }
                 }
         }
         return numbytes;
         free(buff);
}

-- 
Thanks,
udhayakumar

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gnupg.org/pipermail/gnutls-help/attachments/20221118/0c224682/attachment-0001.html>


More information about the Gnutls-help mailing list