Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include "TcpTransport.h" |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 11 | #include <netdb.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <sys/socket.h> |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 15 | #include <arpa/inet.h> |
Jeff Thompson | 6c12a43 | 2013-07-15 12:09:12 -0700 | [diff] [blame] | 16 | #include "../util/ndn_memory.h" |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 17 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 18 | ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port) |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 19 | { |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 20 | if (self->socketDescriptor >= 0) { |
| 21 | close(self->socketDescriptor); |
| 22 | self->socketDescriptor = -1; |
| 23 | } |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 24 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 25 | struct addrinfo hints; |
Jeff Thompson | 6c12a43 | 2013-07-15 12:09:12 -0700 | [diff] [blame] | 26 | ndn_memset((unsigned char *)&hints, 0, sizeof(hints)); |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 27 | hints.ai_family = AF_UNSPEC; |
| 28 | hints.ai_socktype = SOCK_STREAM; |
| 29 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 30 | char portString[10]; |
| 31 | sprintf(portString, "%d", port); |
| 32 | |
| 33 | struct addrinfo *serverInfo; |
| 34 | if (getaddrinfo(host, portString, &hints, &serverInfo) != 0) |
| 35 | return NDN_ERROR_TcpTransport_error_in_getaddrinfo; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 36 | |
| 37 | // loop through all the results and connect to the first we can |
| 38 | struct addrinfo *p; |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 39 | int socketDescriptor; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 40 | for(p = serverInfo; p != NULL; p = p->ai_next) { |
| 41 | if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) |
| 42 | continue; |
| 43 | |
| 44 | if (connect(socketDescriptor, p->ai_addr, p->ai_addrlen) == -1) { |
| 45 | close(socketDescriptor); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | break; |
| 50 | } |
| 51 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 52 | if (p == NULL) { |
| 53 | freeaddrinfo(serverInfo); |
| 54 | return NDN_ERROR_TcpTransport_cannot_connect_to_socket; |
| 55 | } |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 56 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 57 | freeaddrinfo(serverInfo); |
| 58 | self->socketDescriptor = socketDescriptor; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength) |
| 64 | { |
| 65 | if (self->socketDescriptor < 0) |
| 66 | return NDN_ERROR_TcpTransport_socket_is_not_open; |
| 67 | |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 68 | int nBytes; |
| 69 | while (1) { |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 70 | if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0) |
| 71 | return NDN_ERROR_TcpTransport_error_in_send; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 72 | if (nBytes >= dataLength) |
| 73 | break; |
| 74 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 75 | // Send more. |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 76 | dataLength -= nBytes; |
| 77 | } |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | ndn_Error ndn_TcpTransport_receive |
| 83 | (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut) |
| 84 | { |
| 85 | if (self->socketDescriptor < 0) |
| 86 | return NDN_ERROR_TcpTransport_socket_is_not_open; |
| 87 | |
| 88 | int nBytes; |
| 89 | if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1) |
| 90 | return NDN_ERROR_TcpTransport_error_in_recv; |
| 91 | |
| 92 | *nBytesOut = (unsigned int)nBytes; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 93 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 94 | return 0; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 95 | } |