Modify the packet size in transport protocol.
authorKim Gunsoo <gunsoo83.kim@samsung.com>
Wed, 9 Dec 2015 05:08:01 +0000 (14:08 +0900)
committerKim Gunsoo <gunsoo83.kim@samsung.com>
Wed, 16 Dec 2015 06:20:31 +0000 (15:20 +0900)
- increase the transport packet size. (4KB -> 256KB)

Change-Id: Ib8cf85f6e5c34417bba020de4ced11ffff56b2a1
Signed-off-by: Kim Gunsoo <gunsoo83.kim@samsung.com>
src/adb_auth_host.c
src/common_modules.h
src/sockets.c
src/transport.c
src/utils.h

index 031b8f87838ec254c17f2fe05a71dca5702963a7..f0a8643360a79b8ebe3524739f8af8cef16e41a3 100644 (file)
@@ -135,7 +135,7 @@ static void get_user_info(char *buf, size_t len) {
 static int write_public_keyfile(RSA *private_key, const char *private_key_path) {
        RSAPublicKey pkey;
        FILE *outfile = NULL;
-       char path[PATH_MAX], info[MAX_PAYLOAD];
+       char path[PATH_MAX], info[MAX_PAYLOAD_V1];
        uint8_t *encoded = NULL;
        size_t encoded_length;
        int ret = 0;
@@ -339,7 +339,7 @@ static int get_user_key(struct listnode *list) {
 
 static void get_vendor_keys(struct listnode *list) {
        const char *adb_keys_path;
-       char keys_path[MAX_PAYLOAD];
+       char keys_path[MAX_PAYLOAD_V1];
        char *path;
        char *save;
        struct stat buf;
index 45ea8635a163953a8951d34564e092b0292cef5b..5c4640afe5bf5f16e1e04f091b67463fc82cb948 100644 (file)
@@ -32,7 +32,9 @@
 #include "sdb_usb.h"
 #include "fdevent.h"
 
-#define MAX_PAYLOAD 4096
+#define MAX_PAYLOAD_V1  (4*1024)
+#define MAX_PAYLOAD_V2  (256*1024)
+#define MAX_PAYLOAD     MAX_PAYLOAD_V2
 #define CHUNK_SIZE (64*1024)
 #define DEFAULT_SDB_QEMU_PORT 26097
 #define DEFAULT_SDB_PORT 26099
@@ -188,6 +190,9 @@ struct atransport
     fdevent auth_fde;
     unsigned failed_auth_attempts;
 
+    int protocol_version;
+    size_t max_payload;
+
     //sdb specific
     LIST_NODE* node;
        unsigned req;
index 7007cd97a42184c6da3e6c873d409733e40097d2..28fa2a2f7e3dab72bae38ac0a7cfbd430dc62c99 100755 (executable)
@@ -47,6 +47,7 @@ static unsigned unhex(unsigned char*s, int len);
 static int find_transports(char **serial_out, const char *prefix);
 static void unregister_all_tcp_transports();
 static void connect_emulator(char* host, int port, char* buf, int buf_len);
+static size_t sdbsock_get_max_payload(SDB_SOCKET *s);
 
 typedef struct stinfo stinfo;
 struct stinfo {
@@ -490,7 +491,8 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s)
 //        else {
             x = p->data;
 //        }
-        size_t avail = MAX_PAYLOAD;
+        const size_t max_payload = sdbsock_get_max_payload(s);
+        size_t avail = max_payload;
         int r = 1;
 
         while(avail > 0) {
@@ -516,10 +518,10 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s)
             break;
         }
 
-        if(avail == MAX_PAYLOAD) {
+        if(avail == max_payload) {
             put_apacket(p);
         } else {
-            p->len = MAX_PAYLOAD - avail;
+            p->len = max_payload - avail;
 
             if(peer_enqueue(s, p) < 0) {
                 //local socket is already closed by peer or should not close the socket.
@@ -565,9 +567,9 @@ void connect_to_remote(SDB_SOCKET *s, const char* destination)
 {
     D("LS(%X)\n", s->local_id);
     PACKET *p = get_apacket();
-    int len = strlen(destination) + 1;
+    size_t len = strlen(destination) + 1;
 
-    if(len > (MAX_PAYLOAD-1)) {
+    if(len > (sdbsock_get_max_payload(s)-1)) {
         LOG_FATAL("destination oversized\n");
     }
 
@@ -796,7 +798,7 @@ sendfail:
     }
 #ifdef MAKE_DEBUG
     else if(!strncmp(service, "send-packet", strlen("send-packet"))) {
-        char data[MAX_PAYLOAD] = {'1', };
+        char data[MAX_PAYLOAD_V1] = {'1', };
         send_cmd(0, 0, 0x00000000, data, t);
         sendokmsg(socket->fd, "send_packet OK!");
         return 0;
@@ -1151,7 +1153,7 @@ static int smart_socket_check(SDB_SOCKET *s, PACKET **p) {
     }
     else {
         PACKET* socket_packet = s->pkt_list->data;
-        if((socket_packet->len + (*p)->len) > MAX_PAYLOAD) {
+        if((socket_packet->len + (*p)->len) > sdbsock_get_max_payload(s)) {
             LOG_ERROR("LS(%x): overflow\n", s->local_id);
             put_apacket(*p);
             return -1;
@@ -1495,3 +1497,11 @@ static void connect_emulator(char* host, int port, char* buf, int buf_len) {
 
     snprintf(buf, buf_len, "connecting to %s ...", serial);
 }
+
+static size_t sdbsock_get_max_payload(SDB_SOCKET *s) {
+    size_t max_payload = MAX_PAYLOAD;
+    if (s->transport) {
+        max_payload = min(max_payload, s->transport->max_payload);
+    }
+    return max_payload;
+}
index bdda736c078395bffe1ebc5dd64430c3cb74f606..d33ee0dcd58a0df6c583e70e79eb6f48820801bd 100755 (executable)
@@ -39,7 +39,7 @@ static void handle_packet(PACKET *p, TRANSPORT *t);
 static void parse_banner(char *banner, TRANSPORT *t);
 static void wakeup_select(T_PACKET* t_packet);
 static void encoding_packet(PACKET* p);
-static int check_header(PACKET *p);
+static int check_header(PACKET *p, TRANSPORT *t);
 static int check_data(PACKET *p);
 static void  dump_hex( const unsigned char*  ptr, size_t  len);
 
@@ -351,6 +351,20 @@ int list_transports_msg(char*  buffer, size_t  bufferlen)
     len += 4;
     return len;
 }
+static void update_version(TRANSPORT *t, int version, size_t payload)
+{
+    size_t max_payload = MAX_PAYLOAD;
+
+    /* TODO : enable the size up for USB transport */
+    if (t->type == kTransportUsb) {
+        max_payload = MAX_PAYLOAD_V1;
+    }
+
+    D("remote transport version=%x, max_payload=%d\n", version, payload);
+    t->protocol_version = min(version, A_VERSION);
+    t->max_payload = min(payload, max_payload);
+    D("update transport version. version=%x, max_payload=%d\n", t->protocol_version, t->max_payload);
+}
 
 void  update_transports(void)
 {
@@ -397,7 +411,7 @@ read_loop:
     t->connection_state = CS_WAITCNXN;
     // allow the device some time to send the connect message
     sdb_sleep_ms(1000);
-    send_cmd(A_VERSION, MAX_PAYLOAD, A_CNXN, "host::", t);
+    send_cmd(t->protocol_version, t->max_payload, A_CNXN, "host::", t);
     t->connection_state = CS_OFFLINE;
     // allow the device some time to respond to the connect message
     sdb_sleep_ms(1000);
@@ -409,7 +423,7 @@ read_loop:
         if(t->read_from_remote(t, &p->msg, sizeof(MESSAGE))) {
                break;
         }
-               if(check_header(p)) {
+               if(check_header(p, t)) {
                        break;
                }
                if(p->msg.data_length) {
@@ -479,6 +493,9 @@ void register_transport(TRANSPORT *t)
     D("T(%s), device name: '%s'\n", t->serial, t->device_name);
     sdb_thread_t transport_thread_ptr;
 
+    t->protocol_version = A_VERSION;
+    t->max_payload = MAX_PAYLOAD;
+
     //transport is updated by transport_thread, we do not have to update here.
     if(sdb_thread_create(&transport_thread_ptr, transport_thread, t)){
         LOG_FATAL("cannot create output thread\n");
@@ -771,15 +788,15 @@ int writex(int fd, const void *ptr, size_t len)
     return 0;
 }
 
-static int check_header(PACKET *p)
+static int check_header(PACKET *p, TRANSPORT *t)
 {
     if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
         LOG_ERROR("check_header(): invalid magic\n");
         return -1;
     }
 
-    if(p->msg.data_length > MAX_PAYLOAD) {
-       LOG_ERROR("check_header(): %d > MAX_PAYLOAD\n", p->msg.data_length);
+    if(p->msg.data_length > t->max_payload) {
+        LOG_ERROR("check_header(): %d > transport->max_payload(%d)\n", p->msg.data_length, t->max_payload);
         return -1;
     }
 
@@ -906,6 +923,7 @@ void wakeup_select_func(int _fd, unsigned ev, void *data) {
 //            t->connection_state = CS_OFFLINE;
 //            run_transport_close(t);
 //        }
+        update_version(t, p->msg.arg0, p->msg.arg1);
         parse_banner((char*) p->data, t);
     }
     else if(cmd == A_STAT) {
@@ -1057,7 +1075,7 @@ void send_auth_publickey(atransport *t)
     apacket *p = get_apacket();
     int ret;
 
-    ret = adb_auth_get_userkey(p->data, sizeof(p->data));
+    ret = adb_auth_get_userkey(p->data, MAX_PAYLOAD_V1);
     if (!ret) {
         D("Failed to get user public key\n");
         put_apacket(p);
index 30cc0b3f09804126f8026b4f25b33690e4728b73..0fa3a45e7aabfe822316b82b265e2e841b7c4b3f 100755 (executable)
@@ -150,4 +150,17 @@ int sdb_port_listen(uint32_t inet, int port, int type);
 #define DEFAULT_DEVICENAME "<unknown>"
 #define SAFE_FREE(x) if ((x) != NULL) { free(x); x=NULL; }
 
+#undef min
+#define min(a,b)            \
+({ __typeof__ (a) _a = (a); \
+__typeof__ (b) _b = (b);    \
+_a > _b ? _b : _a; })
+
+#undef max
+#define max(a,b)            \
+({ __typeof__ (a) _a = (a); \
+__typeof__ (b) _b = (b);    \
+_a > _b ? _a : _b; })
+
+
 #endif /* _SDB_UTILS_H */