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