Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #include "socket-transport.h" |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <netdb.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <sys/socket.h> |
| 16 | #include <arpa/inet.h> |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 17 | #include <poll.h> |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 18 | #include "../util/ndn_memory.h" |
| 19 | |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 20 | ndn_Error ndn_SocketTransport_connect(struct ndn_SocketTransport *self, ndn_SocketType socketType, char *host, unsigned short port) |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 21 | { |
| 22 | if (self->socketDescriptor >= 0) { |
| 23 | close(self->socketDescriptor); |
| 24 | self->socketDescriptor = -1; |
| 25 | } |
| 26 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 27 | struct addrinfo hints; |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 28 | ndn_memset((uint8_t *)&hints, 0, sizeof(hints)); |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 29 | hints.ai_family = AF_UNSPEC; |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 30 | if (socketType == SOCKET_TCP) |
| 31 | hints.ai_socktype = SOCK_STREAM; |
| 32 | else if (socketType == SOCKET_UDP) |
| 33 | hints.ai_socktype = SOCK_DGRAM; |
| 34 | else |
| 35 | return NDN_ERROR_unrecognized_ndn_SocketTransport; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 36 | |
| 37 | char portString[10]; |
| 38 | sprintf(portString, "%d", port); |
| 39 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 40 | struct addrinfo *serverInfo; |
| 41 | if (getaddrinfo(host, portString, &hints, &serverInfo) != 0) |
| 42 | return NDN_ERROR_SocketTransport_error_in_getaddrinfo; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 43 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 44 | // loop through all the results and connect to the first we can |
| 45 | struct addrinfo *p; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 46 | int socketDescriptor; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 47 | for(p = serverInfo; p != NULL; p = p->ai_next) { |
| 48 | if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) |
| 49 | continue; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 51 | if (connect(socketDescriptor, p->ai_addr, p->ai_addrlen) == -1) { |
| 52 | close(socketDescriptor); |
| 53 | continue; |
| 54 | } |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 55 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 56 | break; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 59 | if (p == NULL) { |
| 60 | freeaddrinfo(serverInfo); |
| 61 | return NDN_ERROR_SocketTransport_cannot_connect_to_socket; |
| 62 | } |
| 63 | |
| 64 | freeaddrinfo(serverInfo); |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 65 | self->socketDescriptor = socketDescriptor; |
| 66 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 67 | return NDN_ERROR_success; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 70 | ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, uint8_t *data, size_t dataLength) |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 71 | { |
| 72 | if (self->socketDescriptor < 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 73 | return NDN_ERROR_SocketTransport_socket_is_not_open; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 74 | |
| 75 | int nBytes; |
| 76 | while (1) { |
| 77 | if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 78 | return NDN_ERROR_SocketTransport_error_in_send; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 79 | if (nBytes >= dataLength) |
| 80 | break; |
| 81 | |
| 82 | // Send more. |
| 83 | dataLength -= nBytes; |
| 84 | } |
| 85 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 86 | return NDN_ERROR_success; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 89 | ndn_Error ndn_SocketTransport_receiveIsReady(struct ndn_SocketTransport *self, int *receiveIsReady) |
| 90 | { |
| 91 | // Default to not ready. |
| 92 | *receiveIsReady = 0; |
| 93 | |
| 94 | if (self->socketDescriptor < 0) |
| 95 | // The socket is not open. Just silently return. |
| 96 | return NDN_ERROR_success; |
| 97 | |
| 98 | struct pollfd pollInfo[1]; |
| 99 | pollInfo[0].fd = self->socketDescriptor; |
| 100 | pollInfo[0].events = POLLIN; |
| 101 | |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 102 | int pollResult = poll(pollInfo, 1, 0); |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 103 | |
| 104 | if (pollResult < 0) |
| 105 | return NDN_ERROR_SocketTransport_error_in_poll; |
| 106 | else if (pollResult == 0) |
| 107 | // Timeout, so no data ready. |
| 108 | return NDN_ERROR_success; |
| 109 | else { |
| 110 | if (pollInfo[0].revents & POLLIN) |
| 111 | *receiveIsReady = 1; |
| 112 | } |
| 113 | |
| 114 | return NDN_ERROR_success; |
| 115 | } |
| 116 | |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 117 | ndn_Error ndn_SocketTransport_receive |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 118 | (struct ndn_SocketTransport *self, uint8_t *buffer, size_t bufferLength, size_t *nBytesOut) |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 119 | { |
| 120 | if (self->socketDescriptor < 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 121 | return NDN_ERROR_SocketTransport_socket_is_not_open; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 122 | |
| 123 | int nBytes; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 124 | if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 125 | return NDN_ERROR_SocketTransport_error_in_recv; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 126 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 127 | *nBytesOut = (size_t)nBytes; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 128 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 129 | return NDN_ERROR_success; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 130 | } |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 131 | |
| 132 | ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self) |
| 133 | { |
| 134 | if (self->socketDescriptor < 0) |
| 135 | // Already closed. Do nothing. |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 136 | return NDN_ERROR_success; |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 137 | |
| 138 | if (close(self->socketDescriptor) != 0) |
| 139 | return NDN_ERROR_SocketTransport_error_in_close; |
| 140 | |
| 141 | self->socketDescriptor = -1; |
| 142 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 143 | return NDN_ERROR_success; |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 144 | } |