OLD | NEW |
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 "net/udp/udp_socket_libevent.h" | 5 #include "net/udp/udp_socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netdb.h> | 9 #include <netdb.h> |
10 #include <net/if.h> | 10 #include <net/if.h> |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 DCHECK(address); | 121 DCHECK(address); |
122 if (!is_connected()) | 122 if (!is_connected()) |
123 return ERR_SOCKET_NOT_CONNECTED; | 123 return ERR_SOCKET_NOT_CONNECTED; |
124 | 124 |
125 if (!remote_address_.get()) { | 125 if (!remote_address_.get()) { |
126 SockaddrStorage storage; | 126 SockaddrStorage storage; |
127 if (getpeername(socket_, storage.addr, &storage.addr_len)) | 127 if (getpeername(socket_, storage.addr, &storage.addr_len)) |
128 return MapSystemError(errno); | 128 return MapSystemError(errno); |
129 scoped_ptr<IPEndPoint> address(new IPEndPoint()); | 129 scoped_ptr<IPEndPoint> address(new IPEndPoint()); |
130 if (!address->FromSockAddr(storage.addr, storage.addr_len)) | 130 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
131 return ERR_FAILED; | 131 return ERR_ADDRESS_INVALID; |
132 remote_address_.reset(address.release()); | 132 remote_address_.reset(address.release()); |
133 } | 133 } |
134 | 134 |
135 *address = *remote_address_; | 135 *address = *remote_address_; |
136 return OK; | 136 return OK; |
137 } | 137 } |
138 | 138 |
139 int UDPSocketLibevent::GetLocalAddress(IPEndPoint* address) const { | 139 int UDPSocketLibevent::GetLocalAddress(IPEndPoint* address) const { |
140 DCHECK(CalledOnValidThread()); | 140 DCHECK(CalledOnValidThread()); |
141 DCHECK(address); | 141 DCHECK(address); |
142 if (!is_connected()) | 142 if (!is_connected()) |
143 return ERR_SOCKET_NOT_CONNECTED; | 143 return ERR_SOCKET_NOT_CONNECTED; |
144 | 144 |
145 if (!local_address_.get()) { | 145 if (!local_address_.get()) { |
146 SockaddrStorage storage; | 146 SockaddrStorage storage; |
147 if (getsockname(socket_, storage.addr, &storage.addr_len)) | 147 if (getsockname(socket_, storage.addr, &storage.addr_len)) |
148 return MapSystemError(errno); | 148 return MapSystemError(errno); |
149 scoped_ptr<IPEndPoint> address(new IPEndPoint()); | 149 scoped_ptr<IPEndPoint> address(new IPEndPoint()); |
150 if (!address->FromSockAddr(storage.addr, storage.addr_len)) | 150 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
151 return ERR_FAILED; | 151 return ERR_ADDRESS_INVALID; |
152 local_address_.reset(address.release()); | 152 local_address_.reset(address.release()); |
153 net_log_.AddEvent(NetLog::TYPE_UDP_LOCAL_ADDRESS, | 153 net_log_.AddEvent(NetLog::TYPE_UDP_LOCAL_ADDRESS, |
154 CreateNetLogUDPConnectCallback(local_address_.get())); | 154 CreateNetLogUDPConnectCallback(local_address_.get())); |
155 } | 155 } |
156 | 156 |
157 *address = *local_address_; | 157 *address = *local_address_; |
158 return OK; | 158 return OK; |
159 } | 159 } |
160 | 160 |
161 int UDPSocketLibevent::Read(IOBuffer* buf, | 161 int UDPSocketLibevent::Read(IOBuffer* buf, |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 HANDLE_EINTR(recvfrom(socket_, | 469 HANDLE_EINTR(recvfrom(socket_, |
470 buf->data(), | 470 buf->data(), |
471 buf_len, | 471 buf_len, |
472 flags, | 472 flags, |
473 storage.addr, | 473 storage.addr, |
474 &storage.addr_len)); | 474 &storage.addr_len)); |
475 int result; | 475 int result; |
476 if (bytes_transferred >= 0) { | 476 if (bytes_transferred >= 0) { |
477 result = bytes_transferred; | 477 result = bytes_transferred; |
478 if (address && !address->FromSockAddr(storage.addr, storage.addr_len)) | 478 if (address && !address->FromSockAddr(storage.addr, storage.addr_len)) |
479 result = ERR_FAILED; | 479 result = ERR_ADDRESS_INVALID; |
480 } else { | 480 } else { |
481 result = MapSystemError(errno); | 481 result = MapSystemError(errno); |
482 } | 482 } |
483 if (result != ERR_IO_PENDING) | 483 if (result != ERR_IO_PENDING) |
484 LogRead(result, buf->data(), storage.addr_len, storage.addr); | 484 LogRead(result, buf->data(), storage.addr_len, storage.addr); |
485 return result; | 485 return result; |
486 } | 486 } |
487 | 487 |
488 int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len, | 488 int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len, |
489 const IPEndPoint* address) { | 489 const IPEndPoint* address) { |
490 SockaddrStorage storage; | 490 SockaddrStorage storage; |
491 struct sockaddr* addr = storage.addr; | 491 struct sockaddr* addr = storage.addr; |
492 if (!address) { | 492 if (!address) { |
493 addr = NULL; | 493 addr = NULL; |
494 storage.addr_len = 0; | 494 storage.addr_len = 0; |
495 } else { | 495 } else { |
496 if (!address->ToSockAddr(storage.addr, &storage.addr_len)) { | 496 if (!address->ToSockAddr(storage.addr, &storage.addr_len)) { |
497 int result = ERR_FAILED; | 497 int result = ERR_ADDRESS_INVALID; |
498 LogWrite(result, NULL, NULL); | 498 LogWrite(result, NULL, NULL); |
499 return result; | 499 return result; |
500 } | 500 } |
501 } | 501 } |
502 | 502 |
503 int result = HANDLE_EINTR(sendto(socket_, | 503 int result = HANDLE_EINTR(sendto(socket_, |
504 buf->data(), | 504 buf->data(), |
505 buf_len, | 505 buf_len, |
506 0, | 506 0, |
507 addr, | 507 addr, |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_TCLASS, | 761 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_TCLASS, |
762 &dscp_and_ecn, sizeof(dscp_and_ecn)); | 762 &dscp_and_ecn, sizeof(dscp_and_ecn)); |
763 } | 763 } |
764 if (rv < 0) | 764 if (rv < 0) |
765 return MapSystemError(errno); | 765 return MapSystemError(errno); |
766 | 766 |
767 return OK; | 767 return OK; |
768 } | 768 } |
769 | 769 |
770 } // namespace net | 770 } // namespace net |
OLD | NEW |