Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 6 | #include "SocketTransport.h" |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <netdb.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <arpa/inet.h> |
| 16 | #include "../util/ndn_memory.h" |
| 17 | |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 18 | 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] | 19 | { |
| 20 | if (self->socketDescriptor >= 0) { |
| 21 | close(self->socketDescriptor); |
| 22 | self->socketDescriptor = -1; |
| 23 | } |
| 24 | |
| 25 | struct addrinfo hints; |
| 26 | ndn_memset((unsigned char *)&hints, 0, sizeof(hints)); |
| 27 | hints.ai_family = AF_UNSPEC; |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 28 | if (socketType == SOCKET_TCP) |
| 29 | hints.ai_socktype = SOCK_STREAM; |
| 30 | else if (socketType == SOCKET_UDP) |
| 31 | hints.ai_socktype = SOCK_DGRAM; |
| 32 | else |
| 33 | return NDN_ERROR_unrecognized_ndn_SocketTransport; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 34 | |
| 35 | char portString[10]; |
| 36 | sprintf(portString, "%d", port); |
| 37 | |
| 38 | struct addrinfo *serverInfo; |
| 39 | if (getaddrinfo(host, portString, &hints, &serverInfo) != 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 40 | return NDN_ERROR_SocketTransport_error_in_getaddrinfo; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 41 | |
| 42 | // loop through all the results and connect to the first we can |
| 43 | struct addrinfo *p; |
| 44 | int socketDescriptor; |
| 45 | for(p = serverInfo; p != NULL; p = p->ai_next) { |
| 46 | if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) |
| 47 | continue; |
| 48 | |
| 49 | if (connect(socketDescriptor, p->ai_addr, p->ai_addrlen) == -1) { |
| 50 | close(socketDescriptor); |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | if (p == NULL) { |
| 58 | freeaddrinfo(serverInfo); |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 59 | return NDN_ERROR_SocketTransport_cannot_connect_to_socket; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | freeaddrinfo(serverInfo); |
| 63 | self->socketDescriptor = socketDescriptor; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 68 | ndn_Error ndn_SocketTransport_send(struct ndn_SocketTransport *self, unsigned char *data, unsigned int dataLength) |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 69 | { |
| 70 | if (self->socketDescriptor < 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 71 | return NDN_ERROR_SocketTransport_socket_is_not_open; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 72 | |
| 73 | int nBytes; |
| 74 | while (1) { |
| 75 | if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 76 | return NDN_ERROR_SocketTransport_error_in_send; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 77 | if (nBytes >= dataLength) |
| 78 | break; |
| 79 | |
| 80 | // Send more. |
| 81 | dataLength -= nBytes; |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 87 | ndn_Error ndn_SocketTransport_receive |
| 88 | (struct ndn_SocketTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut) |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 89 | { |
| 90 | if (self->socketDescriptor < 0) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 91 | return NDN_ERROR_SocketTransport_socket_is_not_open; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 92 | |
| 93 | int nBytes; |
| 94 | if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1) |
Jeff Thompson | 0aa754a | 2013-07-17 17:42:28 -0700 | [diff] [blame] | 95 | return NDN_ERROR_SocketTransport_error_in_recv; |
Jeff Thompson | 7790c68 | 2013-07-17 17:22:12 -0700 | [diff] [blame] | 96 | |
| 97 | *nBytesOut = (unsigned int)nBytes; |
| 98 | |
| 99 | return 0; |
| 100 | } |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 101 | |
| 102 | ndn_Error ndn_SocketTransport_close(struct ndn_SocketTransport *self) |
| 103 | { |
| 104 | if (self->socketDescriptor < 0) |
| 105 | // Already closed. Do nothing. |
| 106 | return 0; |
| 107 | |
| 108 | if (close(self->socketDescriptor) != 0) |
| 109 | return NDN_ERROR_SocketTransport_error_in_close; |
| 110 | |
| 111 | self->socketDescriptor = -1; |
| 112 | |
| 113 | return 0; |
| 114 | } |