Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc

Issue 22587003: [NaCl SDK] Add UDP and TCP Sockets (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Running tests on package Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "nacl_io/kernel_proxy.h" 5 #include "nacl_io/kernel_proxy.h"
6 6
7 7
8 #include <assert.h> 8 #include <assert.h>
9 #include <errno.h> 9 #include <errno.h>
10 #include <fcntl.h> 10 #include <fcntl.h>
11 #include <limits.h> 11 #include <limits.h>
12 #include <poll.h> 12 #include <poll.h>
13 #include <pthread.h> 13 #include <pthread.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <string.h> 15 #include <string.h>
16 #include <sys/time.h> 16 #include <sys/time.h>
17 17
18 #include <iterator> 18 #include <iterator>
19 #include <string> 19 #include <string>
20 20
21 #include "nacl_io/host_resolver.h" 21 #include "nacl_io/host_resolver.h"
22 #include "nacl_io/kernel_handle.h" 22 #include "nacl_io/kernel_handle.h"
23 #include "nacl_io/kernel_wrap_real.h" 23 #include "nacl_io/kernel_wrap_real.h"
24 #include "nacl_io/mount.h" 24 #include "nacl_io/mount.h"
25 #include "nacl_io/mount_dev.h" 25 #include "nacl_io/mount_dev.h"
26 #include "nacl_io/mount_html5fs.h" 26 #include "nacl_io/mount_html5fs.h"
27 #include "nacl_io/mount_http.h" 27 #include "nacl_io/mount_http.h"
28 #include "nacl_io/mount_mem.h" 28 #include "nacl_io/mount_mem.h"
29 #include "nacl_io/mount_node.h" 29 #include "nacl_io/mount_node.h"
30 #include "nacl_io/mount_node_tcp.h"
31 #include "nacl_io/mount_node_udp.h"
30 #include "nacl_io/mount_passthrough.h" 32 #include "nacl_io/mount_passthrough.h"
31 #include "nacl_io/osmman.h" 33 #include "nacl_io/osmman.h"
32 #include "nacl_io/ossocket.h" 34 #include "nacl_io/ossocket.h"
33 #include "nacl_io/osstat.h" 35 #include "nacl_io/osstat.h"
34 #include "nacl_io/path.h" 36 #include "nacl_io/path.h"
35 #include "nacl_io/pepper_interface.h" 37 #include "nacl_io/pepper_interface.h"
36 #include "nacl_io/typed_mount_factory.h" 38 #include "nacl_io/typed_mount_factory.h"
37 #include "sdk_util/auto_lock.h" 39 #include "sdk_util/auto_lock.h"
38 #include "sdk_util/ref_object.h" 40 #include "sdk_util/ref_object.h"
39 41
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 assert(result == 0); 77 assert(result == 0);
76 78
77 // Open the first three in order to get STDIN, STDOUT, STDERR 79 // Open the first three in order to get STDIN, STDOUT, STDERR
78 open("/dev/stdin", O_RDONLY); 80 open("/dev/stdin", O_RDONLY);
79 open("/dev/stdout", O_WRONLY); 81 open("/dev/stdout", O_WRONLY);
80 open("/dev/stderr", O_WRONLY); 82 open("/dev/stderr", O_WRONLY);
81 83
82 #ifdef PROVIDES_SOCKET_API 84 #ifdef PROVIDES_SOCKET_API
83 host_resolver_.Init(ppapi_); 85 host_resolver_.Init(ppapi_);
84 #endif 86 #endif
87
88 StringMap_t args;
89 socket_mount_.reset(new MountSocket());
90 socket_mount_->Init(0, args, ppapi);
85 } 91 }
86 92
87 int KernelProxy::open_resource(const char* path) { 93 int KernelProxy::open_resource(const char* path) {
88 ScopedMount mnt; 94 ScopedMount mnt;
89 Path rel; 95 Path rel;
90 96
91 Error error = AcquireMountAndRelPath(path, &mnt, &rel); 97 Error error = AcquireMountAndRelPath(path, &mnt, &rel);
92 if (error) { 98 if (error) {
93 errno = error; 99 errno = error;
94 return -1; 100 return -1;
(...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 return -1; 903 return -1;
898 } 904 }
899 905
900 int KernelProxy::bind(int fd, const struct sockaddr* addr, socklen_t len) { 906 int KernelProxy::bind(int fd, const struct sockaddr* addr, socklen_t len) {
901 if (NULL == addr) { 907 if (NULL == addr) {
902 errno = EFAULT; 908 errno = EFAULT;
903 return -1; 909 return -1;
904 } 910 }
905 911
906 ScopedKernelHandle handle; 912 ScopedKernelHandle handle;
907 if (AcquireSocketHandle(fd, &handle) == -1) 913 if (AcquireSocketHandle(fd, &handle) == -1)
binji 2013/08/09 19:28:06 no errno set if the handle cannot be acquired? (h
noelallen1 2013/08/09 22:53:22 Set by acquire socket handle. The convention is
908 return -1; 914 return -1;
909 915
910 errno = EINVAL; 916 MountNodeSocket* sock =
911 return -1; 917 reinterpret_cast<MountNodeSocket*>(handle->node().get());
binji 2013/08/09 19:28:06 Why reinterpret_cast? If this is a downcast you ca
noelallen1 2013/08/09 22:53:22 Done.
918
919 Error err = sock->Bind(addr, len);
920 if (err != 0) {
921 errno = err;
922 return -1;
923 }
924
925 return 0;
912 } 926 }
913 927
914 int KernelProxy::connect(int fd, const struct sockaddr* addr, socklen_t len) { 928 int KernelProxy::connect(int fd, const struct sockaddr* addr, socklen_t len) {
915 if (NULL == addr) { 929 if (NULL == addr) {
916 errno = EFAULT; 930 errno = EFAULT;
917 return -1; 931 return -1;
918 } 932 }
919 933
920 ScopedKernelHandle handle; 934 ScopedKernelHandle handle;
921 if (AcquireSocketHandle(fd, &handle) == -1) 935 if (AcquireSocketHandle(fd, &handle) == -1)
922 return -1; 936 return -1;
923 937
924 errno = EACCES; 938 MountNodeSocket* sock =
925 return -1; 939 reinterpret_cast<MountNodeSocket*>(handle->node().get());
940
941 Error err = sock->Connect(addr, len);
942 if (err != 0) {
943 errno = err;
944 return -1;
945 }
946
947 return 0;
926 } 948 }
927 949
928 struct hostent* KernelProxy::gethostbyname(const char* name) { 950 struct hostent* KernelProxy::gethostbyname(const char* name) {
929 return host_resolver_.gethostbyname(name); 951 return host_resolver_.gethostbyname(name);
930 } 952 }
931 953
932 int KernelProxy::getpeername(int fd, struct sockaddr* addr, socklen_t* len) { 954 int KernelProxy::getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
933 if (NULL == addr || NULL == len) { 955 if (NULL == addr || NULL == len) {
934 errno = EFAULT; 956 errno = EFAULT;
935 return -1; 957 return -1;
936 } 958 }
937 959
938 ScopedKernelHandle handle; 960 ScopedKernelHandle handle;
939 if (AcquireSocketHandle(fd, &handle) == -1) 961 if (AcquireSocketHandle(fd, &handle) == -1)
940 return -1; 962 return -1;
941 963
942 errno = EINVAL; 964 MountNodeSocket* sock =
943 return -1; 965 reinterpret_cast<MountNodeSocket*>(handle->node().get());
966
967 Error err = sock->GetPeerName(addr, len);
968 if (err != 0) {
969 errno = err;
970 return -1;
971 }
972
973 return 0;
944 } 974 }
945 975
946 int KernelProxy::getsockname(int fd, struct sockaddr* addr, socklen_t* len) { 976 int KernelProxy::getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
947 if (NULL == addr || NULL == len) { 977 if (NULL == addr || NULL == len) {
948 errno = EFAULT; 978 errno = EFAULT;
949 return -1; 979 return -1;
950 } 980 }
951 981
952 ScopedKernelHandle handle; 982 ScopedKernelHandle handle;
953 if (AcquireSocketHandle(fd, &handle) == -1) 983 if (AcquireSocketHandle(fd, &handle) == -1)
954 return -1; 984 return -1;
955 985
956 errno = EINVAL; 986 MountNodeSocket* sock =
957 return -1; 987 reinterpret_cast<MountNodeSocket*>(handle->node().get());
988
989 Error err = sock->GetPeerName(addr, len);
binji 2013/08/09 19:28:06 GetSockName?
noelallen1 2013/08/09 22:53:22 Done.
990 if (err != 0) {
991 errno = err;
992 return -1;
993 }
994
995 return 0;
958 } 996 }
959 997
960 int KernelProxy::getsockopt(int fd, 998 int KernelProxy::getsockopt(int fd,
961 int lvl, 999 int lvl,
962 int optname, 1000 int optname,
963 void* optval, 1001 void* optval,
964 socklen_t* len) { 1002 socklen_t* len) {
965 if (NULL == optval || NULL == len) { 1003 if (NULL == optval || NULL == len) {
966 errno = EFAULT; 1004 errno = EFAULT;
967 return -1; 1005 return -1;
(...skipping 30 matching lines...) Expand all
998 int flags) { 1036 int flags) {
999 if (NULL == buf) { 1037 if (NULL == buf) {
1000 errno = EFAULT; 1038 errno = EFAULT;
1001 return -1; 1039 return -1;
1002 } 1040 }
1003 1041
1004 ScopedKernelHandle handle; 1042 ScopedKernelHandle handle;
1005 if (AcquireSocketHandle(fd, &handle) == -1) 1043 if (AcquireSocketHandle(fd, &handle) == -1)
1006 return -1; 1044 return -1;
1007 1045
1008 errno = EINVAL; 1046 MountNodeSocket* sock =
1009 return -1; 1047 reinterpret_cast<MountNodeSocket*>(handle->node().get());
1048
1049 int out_len = 0;
1050 Error err = sock->Recv(buf, len, flags, &out_len);
1051 if (err != 0) {
1052 errno = err;
1053 return -1;
1054 }
1055
1056 return static_cast<ssize_t>(out_len);
1010 } 1057 }
1011 1058
1012 ssize_t KernelProxy::recvfrom(int fd, 1059 ssize_t KernelProxy::recvfrom(int fd,
1013 void* buf, 1060 void* buf,
1014 size_t len, 1061 size_t len,
1015 int flags, 1062 int flags,
1016 struct sockaddr* addr, 1063 struct sockaddr* addr,
1017 socklen_t* addrlen) { 1064 socklen_t* addrlen) {
1018 // According to the manpage, recvfrom with a null addr is identical to recv. 1065 // According to the manpage, recvfrom with a null addr is identical to recv.
1019 if (NULL == addr) { 1066 if (NULL == addr) {
1020 return recv(fd, buf, len, flags); 1067 return recv(fd, buf, len, flags);
1021 } 1068 }
1022 1069
1023 if (NULL == buf || NULL == addrlen) { 1070 if (NULL == buf || NULL == addrlen) {
1024 errno = EFAULT; 1071 errno = EFAULT;
1025 return -1; 1072 return -1;
1026 } 1073 }
1027 1074
1028 ScopedKernelHandle handle; 1075 ScopedKernelHandle handle;
1029 if (AcquireSocketHandle(fd, &handle) == -1) 1076 if (AcquireSocketHandle(fd, &handle) == -1)
1030 return -1; 1077 return -1;
1031 1078
1032 errno = EINVAL; 1079 MountNodeSocket* sock =
1033 return -1; 1080 reinterpret_cast<MountNodeSocket*>(handle->node().get());
1081
1082 int out_len = 0;
1083 Error err = sock->RecvFrom(buf, len, flags, addr, addrlen, &out_len);
binji 2013/08/09 19:28:06 why not make the RecvFrom out parameter ssize_t he
noelallen1 2013/08/09 22:53:22 it should match Read. Read(x, y, z) = Recv(x, y,
binji 2013/08/09 23:08:27 OK
1084 if (err != 0) {
1085 errno = err;
1086 return -1;
1087 }
1088
1089 return static_cast<ssize_t>(out_len);
1034 } 1090 }
1035 1091
1036 ssize_t KernelProxy::recvmsg(int fd, struct msghdr* msg, int flags) { 1092 ssize_t KernelProxy::recvmsg(int fd, struct msghdr* msg, int flags) {
1037 if (NULL == msg ) { 1093 if (NULL == msg ) {
1038 errno = EFAULT; 1094 errno = EFAULT;
1039 return -1; 1095 return -1;
1040 } 1096 }
1041 1097
1042 ScopedKernelHandle handle; 1098 ScopedKernelHandle handle;
1043 if (AcquireSocketHandle(fd, &handle) == -1) 1099 if (AcquireSocketHandle(fd, &handle) == -1)
1044 return -1; 1100 return -1;
1045 1101
1046 errno = EOPNOTSUPP; 1102 errno = EOPNOTSUPP;
1047 return -1; 1103 return -1;
1048 } 1104 }
1049 1105
1050 ssize_t KernelProxy::send(int fd, const void* buf, size_t len, int flags) { 1106 ssize_t KernelProxy::send(int fd, const void* buf, size_t len, int flags) {
1051 if (NULL == buf) { 1107 if (NULL == buf) {
1052 errno = EFAULT; 1108 errno = EFAULT;
1053 return -1; 1109 return -1;
1054 } 1110 }
1055 1111
1056 ScopedKernelHandle handle; 1112 ScopedKernelHandle handle;
1057 if (AcquireSocketHandle(fd, &handle) == -1) 1113 if (AcquireSocketHandle(fd, &handle) == -1)
1058 return -1; 1114 return -1;
1059 1115
1060 errno = EINVAL; 1116 MountNodeSocket* sock =
1061 return -1; 1117 reinterpret_cast<MountNodeSocket*>(handle->node().get());
1118
1119 int out_len = 0;
1120 Error err = sock->Send(buf, len, flags, &out_len);
1121 if (err != 0) {
1122 errno = err;
1123 return -1;
1124 }
1125
1126 return static_cast<ssize_t>(out_len);
1062 } 1127 }
1063 1128
1064 ssize_t KernelProxy::sendto(int fd, 1129 ssize_t KernelProxy::sendto(int fd,
1065 const void* buf, 1130 const void* buf,
1066 size_t len, 1131 size_t len,
1067 int flags, 1132 int flags,
1068 const struct sockaddr* addr, 1133 const struct sockaddr* addr,
1069 socklen_t addrlen) { 1134 socklen_t addrlen) {
1070 // According to the manpage, sendto with a null addr is identical to send. 1135 // According to the manpage, sendto with a null addr is identical to send.
1071 if (NULL == addr) { 1136 if (NULL == addr) {
1072 return send(fd, buf, len, flags); 1137 return send(fd, buf, len, flags);
1073 } 1138 }
1074 1139
1075 if (NULL == buf) { 1140 if (NULL == buf) {
1076 errno = EFAULT; 1141 errno = EFAULT;
1077 return -1; 1142 return -1;
1078 } 1143 }
1079 1144
1080 ScopedKernelHandle handle; 1145 ScopedKernelHandle handle;
1081 if (AcquireSocketHandle(fd, &handle) == -1) 1146 if (AcquireSocketHandle(fd, &handle) == -1)
1082 return -1; 1147 return -1;
1083 1148
1084 errno = EINVAL; 1149 MountNodeSocket* sock =
1085 return -1; 1150 reinterpret_cast<MountNodeSocket*>(handle->node().get());
1151
1152 int out_len = 0;
1153 Error err = sock->SendTo(buf, len, flags, addr, addrlen, &out_len);
1154 if (err != 0) {
1155 errno = err;
1156 return -1;
1157 }
1158
1159 return static_cast<ssize_t>(out_len);
1086 } 1160 }
1087 1161
1088 ssize_t KernelProxy::sendmsg(int fd, const struct msghdr* msg, int flags) { 1162 ssize_t KernelProxy::sendmsg(int fd, const struct msghdr* msg, int flags) {
1089 if (NULL == msg) { 1163 if (NULL == msg) {
1090 errno = EFAULT; 1164 errno = EFAULT;
1091 return -1; 1165 return -1;
1092 } 1166 }
1093 1167
1094 ScopedKernelHandle handle; 1168 ScopedKernelHandle handle;
1095 if (AcquireSocketHandle(fd, &handle) == -1) 1169 if (AcquireSocketHandle(fd, &handle) == -1)
(...skipping 19 matching lines...) Expand all
1115 1189
1116 errno = EINVAL; 1190 errno = EINVAL;
1117 return -1; 1191 return -1;
1118 } 1192 }
1119 1193
1120 int KernelProxy::shutdown(int fd, int how) { 1194 int KernelProxy::shutdown(int fd, int how) {
1121 ScopedKernelHandle handle; 1195 ScopedKernelHandle handle;
1122 if (AcquireSocketHandle(fd, &handle) == -1) 1196 if (AcquireSocketHandle(fd, &handle) == -1)
1123 return -1; 1197 return -1;
1124 1198
1125 errno = EINVAL; 1199 MountNodeSocket* sock =
1126 return -1; 1200 reinterpret_cast<MountNodeSocket*>(handle->node().get());
1201
1202 Error err = sock->Shutdown(how);
1203 if (err != 0) {
1204 errno = err;
1205 return -1;
1206 }
1207
1208 return 0;
1127 } 1209 }
1128 1210
1129 int KernelProxy::socket(int domain, int type, int protocol) { 1211 int KernelProxy::socket(int domain, int type, int protocol) {
1130 if (AF_INET != domain && AF_INET6 != domain) { 1212 if (AF_INET != domain && AF_INET6 != domain) {
1131 errno = EAFNOSUPPORT; 1213 errno = EAFNOSUPPORT;
1132 return -1; 1214 return -1;
1133 } 1215 }
1134 1216
1135 if (SOCK_STREAM != type && SOCK_DGRAM != type) { 1217 if (SOCK_DGRAM == type) {
1136 errno = EPROTONOSUPPORT; 1218 MountNodeSocket* sock = new MountNodeUDP(socket_mount_.get());
1219 ScopedMountNode node(sock);
1220
1221 if (sock->Init(S_IREAD | S_IWRITE) == 0) {
1222 ScopedKernelHandle handle(new KernelHandle(socket_mount_, node));
1223 return AllocateFD(handle);
1224 }
1225
1226 errno = EACCES;
1137 return -1; 1227 return -1;
1138 } 1228 }
1139 1229
1140 errno = EACCES; 1230 if (SOCK_STREAM == type) {
1231 MountNodeSocket* sock = new MountNodeTCP(socket_mount_.get());
1232 ScopedMountNode node(sock);
1233
1234 if (sock->Init(S_IREAD | S_IWRITE) == 0) {
binji 2013/08/09 19:28:06 Init part here and above is identical, can you com
noelallen1 2013/08/09 22:53:22 Done.
1235 ScopedKernelHandle handle(new KernelHandle(socket_mount_, node));
1236 return AllocateFD(handle);
1237 }
1238 }
binji 2013/08/09 19:28:06 EACCES here too if sock->Init fails?
noelallen1 2013/08/09 22:53:22 Done.
1239
1240 errno = EPROTONOSUPPORT;
1141 return -1; 1241 return -1;
1142 } 1242 }
1143 1243
1144 int KernelProxy::socketpair(int domain, int type, int protocol, int* sv) { 1244 int KernelProxy::socketpair(int domain, int type, int protocol, int* sv) {
1145 if (NULL == sv) { 1245 if (NULL == sv) {
1146 errno = EFAULT; 1246 errno = EFAULT;
1147 return -1; 1247 return -1;
1148 } 1248 }
1149 1249
1150 // Catch-22: We don't support AF_UNIX, but any other AF doesn't support 1250 // Catch-22: We don't support AF_UNIX, but any other AF doesn't support
(...skipping 25 matching lines...) Expand all
1176 errno = ENOTSOCK; 1276 errno = ENOTSOCK;
1177 return -1; 1277 return -1;
1178 } 1278 }
1179 1279
1180 return 0; 1280 return 0;
1181 } 1281 }
1182 1282
1183 #endif // PROVIDES_SOCKET_API 1283 #endif // PROVIDES_SOCKET_API
1184 1284
1185 } // namespace_nacl_io 1285 } // namespace_nacl_io
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698