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 | |
| 18 | // get sockaddr, IPv4 or IPv6: |
| 19 | static inline void *get_in_addr(struct sockaddr *sa) |
| 20 | { |
| 21 | if (sa->sa_family == AF_INET) |
| 22 | return &(((struct sockaddr_in*)sa)->sin_addr); |
| 23 | |
| 24 | return &(((struct sockaddr_in6*)sa)->sin6_addr); |
| 25 | } |
| 26 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 27 | 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] | 28 | { |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 29 | if (self->socketDescriptor >= 0) { |
| 30 | close(self->socketDescriptor); |
| 31 | self->socketDescriptor = -1; |
| 32 | } |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 33 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 34 | struct addrinfo hints; |
Jeff Thompson | 6c12a43 | 2013-07-15 12:09:12 -0700 | [diff] [blame^] | 35 | ndn_memset((unsigned char *)&hints, 0, sizeof(hints)); |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 36 | hints.ai_family = AF_UNSPEC; |
| 37 | hints.ai_socktype = SOCK_STREAM; |
| 38 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 39 | char portString[10]; |
| 40 | sprintf(portString, "%d", port); |
| 41 | |
| 42 | struct addrinfo *serverInfo; |
| 43 | if (getaddrinfo(host, portString, &hints, &serverInfo) != 0) |
| 44 | return NDN_ERROR_TcpTransport_error_in_getaddrinfo; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 45 | |
| 46 | // loop through all the results and connect to the first we can |
| 47 | struct addrinfo *p; |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 48 | int socketDescriptor; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 49 | for(p = serverInfo; p != NULL; p = p->ai_next) { |
| 50 | if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) |
| 51 | continue; |
| 52 | |
| 53 | if (connect(socketDescriptor, p->ai_addr, p->ai_addrlen) == -1) { |
| 54 | close(socketDescriptor); |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | break; |
| 59 | } |
| 60 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 61 | if (p == NULL) { |
| 62 | freeaddrinfo(serverInfo); |
| 63 | return NDN_ERROR_TcpTransport_cannot_connect_to_socket; |
| 64 | } |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 65 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 66 | freeaddrinfo(serverInfo); |
| 67 | self->socketDescriptor = socketDescriptor; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 68 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength) |
| 73 | { |
| 74 | if (self->socketDescriptor < 0) |
| 75 | return NDN_ERROR_TcpTransport_socket_is_not_open; |
| 76 | |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 77 | int nBytes; |
| 78 | while (1) { |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 79 | if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0) |
| 80 | return NDN_ERROR_TcpTransport_error_in_send; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 81 | if (nBytes >= dataLength) |
| 82 | break; |
| 83 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 84 | // Send more. |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 85 | dataLength -= nBytes; |
| 86 | } |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | ndn_Error ndn_TcpTransport_receive |
| 92 | (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut) |
| 93 | { |
| 94 | if (self->socketDescriptor < 0) |
| 95 | return NDN_ERROR_TcpTransport_socket_is_not_open; |
| 96 | |
| 97 | int nBytes; |
| 98 | if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1) |
| 99 | return NDN_ERROR_TcpTransport_error_in_recv; |
| 100 | |
| 101 | *nBytesOut = (unsigned int)nBytes; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 102 | |
Jeff Thompson | 0d567da | 2013-07-14 22:10:21 -0700 | [diff] [blame] | 103 | return 0; |
Jeff Thompson | 7850d78 | 2013-07-14 17:59:14 -0700 | [diff] [blame] | 104 | } |