Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / desc / nacl_makeboundsock.c
1 /*
2  * Copyright 2008 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include "native_client/src/include/portability.h"
8 #include "native_client/src/shared/platform/nacl_global_secure_random.h"
9 #include "native_client/src/shared/platform/nacl_log.h"
10 #include "native_client/src/trusted/desc/nacl_desc_base.h"
11 #include "native_client/src/trusted/desc/nacl_desc_conn_cap.h"
12 #include "native_client/src/trusted/desc/nacl_desc_imc_bound_desc.h"
13 #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
14
15
16 int32_t NaClCommonDescMakeBoundSock(struct NaClDesc *pair[2]) {
17   int32_t                     retval;
18   struct NaClSocketAddress    sa;
19   struct NaClDescConnCap      *ccp;
20   NaClHandle                  h;
21   struct NaClDescImcBoundDesc *idp;
22
23   retval = -NACL_ABI_ENOMEM;
24   ccp = NULL;
25   idp = NULL;
26   h = NACL_INVALID_HANDLE;
27   /*
28    * create NaClDescConnCap object, invoke NaClBoundSocket, create
29    * an NaClDescImcDesc object.  put both into open file table.
30    */
31   ccp = malloc(sizeof *ccp);
32   if (NULL == ccp) {
33     goto cleanup;
34   }
35   idp = malloc(sizeof *idp);
36   if (NULL == idp) {
37     goto cleanup;
38   }
39
40   do {
41     NaClGenerateRandomPath(&sa.path[0], NACL_PATH_MAX);
42     h = NaClBoundSocket(&sa);
43     NaClLog(3, "NaClCommonDescMakeBoundSock: sa: %s, h 0x%"NACL_PRIxPTR"\n",
44             sa.path, (uintptr_t) h);
45   } while (NACL_INVALID_HANDLE == h);
46
47   if (!NaClDescConnCapCtor(ccp, &sa)) {
48     goto cleanup;
49   }
50
51   if (!NaClDescImcBoundDescCtor(idp, h)) {
52     NaClDescUnref((struct NaClDesc *) ccp);
53     goto cleanup;
54   }
55   h = NACL_INVALID_HANDLE;  /* idp took ownership */
56
57   pair[0] = (struct NaClDesc *) idp;
58   idp = NULL;
59
60   pair[1] = (struct NaClDesc *) ccp;
61   ccp = NULL;
62
63   retval = 0;
64
65  cleanup:
66   free(idp);
67   free(ccp);
68   if (NACL_INVALID_HANDLE != h) {
69     (void) NaClClose(h);
70   }
71   return retval;
72 }