summaryrefslogtreecommitdiff
path: root/legacy/eet/src/lib/eet_connection.c
blob: 4f141c16f3d982adb4263d6179bc83e514ecbdab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* ifdef HAVE_CONFIG_H */

#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined __GNUC__
# define alloca __builtin_alloca
#elif defined _AIX
# define alloca __alloca
#elif defined _MSC_VER
# include <malloc.h>
# define alloca _alloca
#else /* ifdef HAVE_ALLOCA_H */
# include <stddef.h>
# ifdef  __cplusplus
extern "C"
# endif /* ifdef  __cplusplus */
void *    alloca (size_t);
#endif /* ifdef HAVE_ALLOCA_H */

#include <string.h>
#include <stdlib.h>

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif /* ifdef HAVE_NETINET_IN_H */

#ifdef _WIN32
# include <winsock2.h>
#endif /* ifdef _WIN32 */

#include <Eina.h>

#include "Eet.h"
#include "Eet_private.h"

#define MAGIC_EET_DATA_PACKET 0x4270ACE1

typedef struct _Eet_Message   Eet_Message;
struct _Eet_Message
{
   int magic;
   int size;
};

struct _Eet_Connection
{
   Eet_Read_Cb  *eet_read_cb;
   Eet_Write_Cb *eet_write_cb;
   void         *user_data;

   size_t        allocated;
   size_t        size;
   size_t        received;

   void         *buffer;
};

EAPI Eet_Connection *
eet_connection_new(Eet_Read_Cb  *eet_read_cb,
                   Eet_Write_Cb *eet_write_cb,
                   const void   *user_data)
{
   Eet_Connection *conn;

   if (!eet_read_cb || !eet_write_cb)
      return NULL;

   conn = calloc(1, sizeof (Eet_Connection));
   if (!conn)
      return NULL;

   conn->eet_read_cb = eet_read_cb;
   conn->eet_write_cb = eet_write_cb;
   conn->user_data = (void *)user_data;

   return conn;
} /* eet_connection_new */

EAPI int
eet_connection_received(Eet_Connection *conn,
                        const void     *data,
                        size_t          size)
{
   if ((!conn) || (!data) || (!size))
      return size;

   do {
        size_t copy_size;

        if (conn->size == 0)
          {
             const Eet_Message *msg;
             size_t packet_size;

             if (size < sizeof (Eet_Message))
                break;

             msg = data;
             /* Check the magic */
             if (ntohl(msg->magic) != MAGIC_EET_DATA_PACKET)
                break;

             packet_size = ntohl(msg->size);
             /* Message should always be under 64K */
             if (packet_size > 64 * 1024)
                break;

             data = (void *)(msg + 1);
             size -= sizeof (msg);
             if ((size_t)packet_size <= size)
               {
                  /* Not a partial receive, go the quick way. */
                  if (!conn->eet_read_cb(data, packet_size, conn->user_data))
                     break;

                  data = (void *)((char *)data + packet_size);
                  size -= packet_size;

                  conn->received = 0;
                  continue;
               }

             conn->size = packet_size;
             if (conn->allocated < conn->size)
               {
                  void *tmp;

                  tmp = realloc(conn->buffer, conn->size);
                  if (!tmp)
                     break;

                  conn->buffer = tmp;
                  conn->allocated = conn->size;
               }
          }

        /* Partial receive */
        copy_size =
           (conn->size - conn->received >=
            size) ? size : conn->size - conn->received;
        memcpy((char *)conn->buffer + conn->received, data, copy_size);

        conn->received += copy_size;
        data = (void *)((char *)data + copy_size);
        size -= copy_size;

        if (conn->received == conn->size)
          {
             size_t data_size;

             data_size = conn->size;
             conn->size = 0;
             conn->received = 0;

             /* Completed a packet. */
             if (!conn->eet_read_cb(conn->buffer, data_size, conn->user_data))
               {
                  /* Something goes wrong. Stop now. */
                  size += data_size;
                  break;
               }
          }
     } while (size > 0);

   return size;
} /* eet_connection_received */

static Eina_Bool
_eet_connection_raw_send(Eet_Connection *conn,
                         void           *data,
                         int             data_size)
{
   Eet_Message *message;

   /* Message should never be above 64K */
   if (data_size > 64 * 1024)
      return EINA_FALSE;

   message = alloca(data_size + sizeof (Eet_Message));
   message->magic = htonl(MAGIC_EET_DATA_PACKET);
   message->size = htonl(data_size);

   memcpy(message + 1, data, data_size);

   conn->eet_write_cb(message,
                      data_size + sizeof (Eet_Message),
                      conn->user_data);
   return EINA_TRUE;
} /* _eet_connection_raw_send */

EAPI Eina_Bool
eet_connection_send(Eet_Connection      *conn,
                    Eet_Data_Descriptor *edd,
                    const void          *data_in,
                    const char          *cipher_key)
{
   void *flat_data;
   int data_size;
   Eina_Bool ret = EINA_FALSE;

   flat_data = eet_data_descriptor_encode_cipher(edd,
                                                 data_in,
                                                 cipher_key,
                                                 &data_size);
   if (!flat_data)
      return EINA_FALSE;

   if (_eet_connection_raw_send(conn, flat_data, data_size))
      ret = EINA_TRUE;

   free(flat_data);
   return ret;
} /* eet_connection_send */

EAPI Eina_Bool
eet_connection_node_send(Eet_Connection *conn,
                         Eet_Node       *node,
                         const char     *cipher_key)
{
   void *data;
   int data_size;
   Eina_Bool ret = EINA_FALSE;

   data = eet_data_node_encode_cipher(node, cipher_key, &data_size);
   if (!data)
      return EINA_FALSE;

   if (_eet_connection_raw_send(conn, data, data_size))
      ret = EINA_TRUE;

   free(data);
   return ret;
} /* eet_connection_node_send */

EAPI void *
eet_connection_close(Eet_Connection *conn,
                     Eina_Bool      *on_going)
{
   void *user_data;

   if (!conn)
      return NULL;

   if (on_going)
      *on_going = conn->received == 0 ? EINA_FALSE : EINA_TRUE;

   user_data = conn->user_data;

   free(conn->buffer);
   free(conn);

   return user_data;
} /* eet_connection_close */