+#if defined(TARGET_MIPS)
+ switch(type) {
+ case TARGET_SOCK_DGRAM:
+ type = SOCK_DGRAM;
+ break;
+ case TARGET_SOCK_STREAM:
+ type = SOCK_STREAM;
+ break;
+ case TARGET_SOCK_RAW:
+ type = SOCK_RAW;
+ break;
+ case TARGET_SOCK_RDM:
+ type = SOCK_RDM;
+ break;
+ case TARGET_SOCK_SEQPACKET:
+ type = SOCK_SEQPACKET;
+ break;
+ case TARGET_SOCK_PACKET:
+ type = SOCK_PACKET;
+ break;
+ }
+#endif
+ return get_errno(socket(domain, type, protocol));
+}
+
+static long do_bind(int sockfd, target_ulong target_addr,
+ socklen_t addrlen)
+{
+ void *addr = alloca(addrlen);
+
+ target_to_host_sockaddr(addr, target_addr, addrlen);
+ return get_errno(bind(sockfd, addr, addrlen));
+}
+
+static long do_connect(int sockfd, target_ulong target_addr,
+ socklen_t addrlen)
+{
+ void *addr = alloca(addrlen);
+
+ target_to_host_sockaddr(addr, target_addr, addrlen);
+ return get_errno(connect(sockfd, addr, addrlen));
+}
+
+static long do_sendrecvmsg(int fd, target_ulong target_msg,
+ int flags, int send)
+{
+ long ret;
+ struct target_msghdr *msgp;
+ struct msghdr msg;
+ int count;
+ struct iovec *vec;
+ target_ulong target_vec;
+
+ lock_user_struct(msgp, target_msg, 1);
+ if (msgp->msg_name) {
+ msg.msg_namelen = tswap32(msgp->msg_namelen);
+ msg.msg_name = alloca(msg.msg_namelen);
+ target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
+ msg.msg_namelen);
+ } else {
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ }
+ msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
+ msg.msg_control = alloca(msg.msg_controllen);
+ msg.msg_flags = tswap32(msgp->msg_flags);
+
+ count = tswapl(msgp->msg_iovlen);
+ vec = alloca(count * sizeof(struct iovec));
+ target_vec = tswapl(msgp->msg_iov);
+ lock_iovec(vec, target_vec, count, send);
+ msg.msg_iovlen = count;
+ msg.msg_iov = vec;
+
+ if (send) {
+ target_to_host_cmsg(&msg, msgp);
+ ret = get_errno(sendmsg(fd, &msg, flags));
+ } else {
+ ret = get_errno(recvmsg(fd, &msg, flags));
+ if (!is_error(ret))
+ host_to_target_cmsg(msgp, &msg);
+ }
+ unlock_iovec(vec, target_vec, count, !send);
+ return ret;
+}
+
+static long do_accept(int fd, target_ulong target_addr,
+ target_ulong target_addrlen)
+{
+ socklen_t addrlen = tget32(target_addrlen);
+ void *addr = alloca(addrlen);
+ long ret;
+
+ ret = get_errno(accept(fd, addr, &addrlen));
+ if (!is_error(ret)) {
+ host_to_target_sockaddr(target_addr, addr, addrlen);
+ tput32(target_addrlen, addrlen);
+ }
+ return ret;
+}
+
+static long do_getpeername(int fd, target_ulong target_addr,
+ target_ulong target_addrlen)
+{
+ socklen_t addrlen = tget32(target_addrlen);
+ void *addr = alloca(addrlen);
+ long ret;
+
+ ret = get_errno(getpeername(fd, addr, &addrlen));
+ if (!is_error(ret)) {
+ host_to_target_sockaddr(target_addr, addr, addrlen);
+ tput32(target_addrlen, addrlen);
+ }
+ return ret;
+}
+
+static long do_getsockname(int fd, target_ulong target_addr,
+ target_ulong target_addrlen)
+{
+ socklen_t addrlen = tget32(target_addrlen);
+ void *addr = alloca(addrlen);
+ long ret;
+
+ ret = get_errno(getsockname(fd, addr, &addrlen));
+ if (!is_error(ret)) {
+ host_to_target_sockaddr(target_addr, addr, addrlen);
+ tput32(target_addrlen, addrlen);
+ }
+ return ret;