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

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: Fixes 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 805 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
908 return -1; 914 return -1;
909 915
910 errno = EINVAL; 916 Error err = handle->socket_node()->Bind(addr, len);
911 return -1; 917 if (err != 0) {
918 errno = err;
919 return -1;
920 }
921
922 return 0;
912 } 923 }
913 924
914 int KernelProxy::connect(int fd, const struct sockaddr* addr, socklen_t len) { 925 int KernelProxy::connect(int fd, const struct sockaddr* addr, socklen_t len) {
915 if (NULL == addr) { 926 if (NULL == addr) {
916 errno = EFAULT; 927 errno = EFAULT;
917 return -1; 928 return -1;
918 } 929 }
919 930
920 ScopedKernelHandle handle; 931 ScopedKernelHandle handle;
921 if (AcquireSocketHandle(fd, &handle) == -1) 932 if (AcquireSocketHandle(fd, &handle) == -1)
922 return -1; 933 return -1;
923 934
924 errno = EACCES; 935 Error err = handle->socket_node()->Connect(addr, len);
925 return -1; 936 if (err != 0) {
937 errno = err;
938 return -1;
939 }
940
941 return 0;
926 } 942 }
927 943
928 struct hostent* KernelProxy::gethostbyname(const char* name) { 944 struct hostent* KernelProxy::gethostbyname(const char* name) {
929 return host_resolver_.gethostbyname(name); 945 return host_resolver_.gethostbyname(name);
930 } 946 }
931 947
932 int KernelProxy::getpeername(int fd, struct sockaddr* addr, socklen_t* len) { 948 int KernelProxy::getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
933 if (NULL == addr || NULL == len) { 949 if (NULL == addr || NULL == len) {
934 errno = EFAULT; 950 errno = EFAULT;
935 return -1; 951 return -1;
936 } 952 }
937 953
938 ScopedKernelHandle handle; 954 ScopedKernelHandle handle;
939 if (AcquireSocketHandle(fd, &handle) == -1) 955 if (AcquireSocketHandle(fd, &handle) == -1)
940 return -1; 956 return -1;
941 957
942 errno = EINVAL; 958 Error err = handle->socket_node()->GetPeerName(addr, len);
943 return -1; 959 if (err != 0) {
960 errno = err;
961 return -1;
962 }
963
964 return 0;
944 } 965 }
945 966
946 int KernelProxy::getsockname(int fd, struct sockaddr* addr, socklen_t* len) { 967 int KernelProxy::getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
947 if (NULL == addr || NULL == len) { 968 if (NULL == addr || NULL == len) {
948 errno = EFAULT; 969 errno = EFAULT;
949 return -1; 970 return -1;
950 } 971 }
951 972
952 ScopedKernelHandle handle; 973 ScopedKernelHandle handle;
953 if (AcquireSocketHandle(fd, &handle) == -1) 974 if (AcquireSocketHandle(fd, &handle) == -1)
954 return -1; 975 return -1;
955 976
956 errno = EINVAL; 977 Error err = handle->socket_node()->GetSockName(addr, len);
957 return -1; 978 if (err != 0) {
979 errno = err;
980 return -1;
981 }
982
983 return 0;
958 } 984 }
959 985
960 int KernelProxy::getsockopt(int fd, 986 int KernelProxy::getsockopt(int fd,
961 int lvl, 987 int lvl,
962 int optname, 988 int optname,
963 void* optval, 989 void* optval,
964 socklen_t* len) { 990 socklen_t* len) {
965 if (NULL == optval || NULL == len) { 991 if (NULL == optval || NULL == len) {
966 errno = EFAULT; 992 errno = EFAULT;
967 return -1; 993 return -1;
(...skipping 30 matching lines...) Expand all
998 int flags) { 1024 int flags) {
999 if (NULL == buf) { 1025 if (NULL == buf) {
1000 errno = EFAULT; 1026 errno = EFAULT;
1001 return -1; 1027 return -1;
1002 } 1028 }
1003 1029
1004 ScopedKernelHandle handle; 1030 ScopedKernelHandle handle;
1005 if (AcquireSocketHandle(fd, &handle) == -1) 1031 if (AcquireSocketHandle(fd, &handle) == -1)
1006 return -1; 1032 return -1;
1007 1033
1008 errno = EINVAL; 1034 int out_len = 0;
1009 return -1; 1035 Error err = handle->socket_node()->Recv(buf, len, flags, &out_len);
1036 if (err != 0) {
1037 errno = err;
1038 return -1;
1039 }
1040
1041 return static_cast<ssize_t>(out_len);
1010 } 1042 }
1011 1043
1012 ssize_t KernelProxy::recvfrom(int fd, 1044 ssize_t KernelProxy::recvfrom(int fd,
1013 void* buf, 1045 void* buf,
1014 size_t len, 1046 size_t len,
1015 int flags, 1047 int flags,
1016 struct sockaddr* addr, 1048 struct sockaddr* addr,
1017 socklen_t* addrlen) { 1049 socklen_t* addrlen) {
1018 // According to the manpage, recvfrom with a null addr is identical to recv. 1050 // According to the manpage, recvfrom with a null addr is identical to recv.
1019 if (NULL == addr) { 1051 if (NULL == addr) {
1020 return recv(fd, buf, len, flags); 1052 return recv(fd, buf, len, flags);
1021 } 1053 }
1022 1054
1023 if (NULL == buf || NULL == addrlen) { 1055 if (NULL == buf || NULL == addrlen) {
1024 errno = EFAULT; 1056 errno = EFAULT;
1025 return -1; 1057 return -1;
1026 } 1058 }
1027 1059
1028 ScopedKernelHandle handle; 1060 ScopedKernelHandle handle;
1029 if (AcquireSocketHandle(fd, &handle) == -1) 1061 if (AcquireSocketHandle(fd, &handle) == -1)
1030 return -1; 1062 return -1;
1031 1063
1032 errno = EINVAL; 1064 int out_len = 0;
1033 return -1; 1065 Error err = handle->socket_node()->RecvFrom(buf,
1066 len,
1067 flags,
1068 addr,
1069 addrlen,
1070 &out_len);
1071 if (err != 0) {
1072 errno = err;
1073 return -1;
1074 }
1075
1076 return static_cast<ssize_t>(out_len);
1034 } 1077 }
1035 1078
1036 ssize_t KernelProxy::recvmsg(int fd, struct msghdr* msg, int flags) { 1079 ssize_t KernelProxy::recvmsg(int fd, struct msghdr* msg, int flags) {
1037 if (NULL == msg ) { 1080 if (NULL == msg ) {
1038 errno = EFAULT; 1081 errno = EFAULT;
1039 return -1; 1082 return -1;
1040 } 1083 }
1041 1084
1042 ScopedKernelHandle handle; 1085 ScopedKernelHandle handle;
1043 if (AcquireSocketHandle(fd, &handle) == -1) 1086 if (AcquireSocketHandle(fd, &handle) == -1)
1044 return -1; 1087 return -1;
1045 1088
1046 errno = EOPNOTSUPP; 1089 errno = EOPNOTSUPP;
1047 return -1; 1090 return -1;
1048 } 1091 }
1049 1092
1050 ssize_t KernelProxy::send(int fd, const void* buf, size_t len, int flags) { 1093 ssize_t KernelProxy::send(int fd, const void* buf, size_t len, int flags) {
1051 if (NULL == buf) { 1094 if (NULL == buf) {
1052 errno = EFAULT; 1095 errno = EFAULT;
1053 return -1; 1096 return -1;
1054 } 1097 }
1055 1098
1056 ScopedKernelHandle handle; 1099 ScopedKernelHandle handle;
1057 if (AcquireSocketHandle(fd, &handle) == -1) 1100 if (AcquireSocketHandle(fd, &handle) == -1)
1058 return -1; 1101 return -1;
1059 1102
1060 errno = EINVAL; 1103 int out_len = 0;
1061 return -1; 1104 Error err = handle->socket_node()->Send(buf, len, flags, &out_len);
1105 if (err != 0) {
1106 errno = err;
1107 return -1;
1108 }
1109
1110 return static_cast<ssize_t>(out_len);
1062 } 1111 }
1063 1112
1064 ssize_t KernelProxy::sendto(int fd, 1113 ssize_t KernelProxy::sendto(int fd,
1065 const void* buf, 1114 const void* buf,
1066 size_t len, 1115 size_t len,
1067 int flags, 1116 int flags,
1068 const struct sockaddr* addr, 1117 const struct sockaddr* addr,
1069 socklen_t addrlen) { 1118 socklen_t addrlen) {
1070 // According to the manpage, sendto with a null addr is identical to send. 1119 // According to the manpage, sendto with a null addr is identical to send.
1071 if (NULL == addr) { 1120 if (NULL == addr) {
1072 return send(fd, buf, len, flags); 1121 return send(fd, buf, len, flags);
1073 } 1122 }
1074 1123
1075 if (NULL == buf) { 1124 if (NULL == buf) {
1076 errno = EFAULT; 1125 errno = EFAULT;
1077 return -1; 1126 return -1;
1078 } 1127 }
1079 1128
1080 ScopedKernelHandle handle; 1129 ScopedKernelHandle handle;
1081 if (AcquireSocketHandle(fd, &handle) == -1) 1130 if (AcquireSocketHandle(fd, &handle) == -1)
1082 return -1; 1131 return -1;
1083 1132
1084 errno = EINVAL; 1133 int out_len = 0;
1085 return -1; 1134 Error err =
1135 handle->socket_node()->SendTo(buf, len, flags, addr, addrlen, &out_len);
1136
1137 if (err != 0) {
1138 errno = err;
1139 return -1;
1140 }
1141
1142 return static_cast<ssize_t>(out_len);
1086 } 1143 }
1087 1144
1088 ssize_t KernelProxy::sendmsg(int fd, const struct msghdr* msg, int flags) { 1145 ssize_t KernelProxy::sendmsg(int fd, const struct msghdr* msg, int flags) {
1089 if (NULL == msg) { 1146 if (NULL == msg) {
1090 errno = EFAULT; 1147 errno = EFAULT;
1091 return -1; 1148 return -1;
1092 } 1149 }
1093 1150
1094 ScopedKernelHandle handle; 1151 ScopedKernelHandle handle;
1095 if (AcquireSocketHandle(fd, &handle) == -1) 1152 if (AcquireSocketHandle(fd, &handle) == -1)
(...skipping 19 matching lines...) Expand all
1115 1172
1116 errno = EINVAL; 1173 errno = EINVAL;
1117 return -1; 1174 return -1;
1118 } 1175 }
1119 1176
1120 int KernelProxy::shutdown(int fd, int how) { 1177 int KernelProxy::shutdown(int fd, int how) {
1121 ScopedKernelHandle handle; 1178 ScopedKernelHandle handle;
1122 if (AcquireSocketHandle(fd, &handle) == -1) 1179 if (AcquireSocketHandle(fd, &handle) == -1)
1123 return -1; 1180 return -1;
1124 1181
1125 errno = EINVAL; 1182 Error err = handle->socket_node()->Shutdown(how);
1126 return -1; 1183 if (err != 0) {
1184 errno = err;
1185 return -1;
1186 }
1187
1188 return 0;
1127 } 1189 }
1128 1190
1129 int KernelProxy::socket(int domain, int type, int protocol) { 1191 int KernelProxy::socket(int domain, int type, int protocol) {
1130 if (AF_INET != domain && AF_INET6 != domain) { 1192 if (AF_INET != domain && AF_INET6 != domain) {
1131 errno = EAFNOSUPPORT; 1193 errno = EAFNOSUPPORT;
1132 return -1; 1194 return -1;
1133 } 1195 }
1134 1196
1135 if (SOCK_STREAM != type && SOCK_DGRAM != type) { 1197 MountNodeSocket* sock = NULL;
1136 errno = EPROTONOSUPPORT; 1198 switch (type) {
1137 return -1; 1199 case SOCK_DGRAM:
1200 sock = new MountNodeUDP(socket_mount_.get());
1201 break;
1202
1203 case SOCK_STREAM:
1204 sock = new MountNodeTCP(socket_mount_.get());
1205 break;
1206
1207 default:
1208 errno = EPROTONOSUPPORT;
1209 return -1;
1138 } 1210 }
1139 1211
1140 errno = EACCES; 1212 ScopedMountNode node(sock);
1141 return -1; 1213 if (sock->Init(S_IREAD | S_IWRITE) == 0) {
1214 ScopedKernelHandle handle(new KernelHandle(socket_mount_, node));
1215 return AllocateFD(handle);
1216 }
1217
1218 // If we failed to init, assume we don't have access.
1219 return EACCES;
1142 } 1220 }
1143 1221
1144 int KernelProxy::socketpair(int domain, int type, int protocol, int* sv) { 1222 int KernelProxy::socketpair(int domain, int type, int protocol, int* sv) {
1145 if (NULL == sv) { 1223 if (NULL == sv) {
1146 errno = EFAULT; 1224 errno = EFAULT;
1147 return -1; 1225 return -1;
1148 } 1226 }
1149 1227
1150 // Catch-22: We don't support AF_UNIX, but any other AF doesn't support 1228 // Catch-22: We don't support AF_UNIX, but any other AF doesn't support
1151 // socket pairs. Thus, this function always fails. 1229 // socket pairs. Thus, this function always fails.
(...skipping 24 matching lines...) Expand all
1176 errno = ENOTSOCK; 1254 errno = ENOTSOCK;
1177 return -1; 1255 return -1;
1178 } 1256 }
1179 1257
1180 return 0; 1258 return 0;
1181 } 1259 }
1182 1260
1183 #endif // PROVIDES_SOCKET_API 1261 #endif // PROVIDES_SOCKET_API
1184 1262
1185 } // namespace_nacl_io 1263 } // namespace_nacl_io
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698