blob: a9c5c11ceecba274f7fd2d8e6dad8c33a24b5db6 [file] [log] [blame]
Jeff Thompson7850d782013-07-14 17:59:14 -07001/**
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 Thompson7850d782013-07-14 17:59:14 -070011#include <netdb.h>
12#include <sys/types.h>
13#include <netinet/in.h>
14#include <sys/socket.h>
Jeff Thompson7850d782013-07-14 17:59:14 -070015#include <arpa/inet.h>
Jeff Thompson6c12a432013-07-15 12:09:12 -070016#include "../util/ndn_memory.h"
Jeff Thompson7850d782013-07-14 17:59:14 -070017
18// get sockaddr, IPv4 or IPv6:
19static 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 Thompson0d567da2013-07-14 22:10:21 -070027ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port)
Jeff Thompson7850d782013-07-14 17:59:14 -070028{
Jeff Thompson0d567da2013-07-14 22:10:21 -070029 if (self->socketDescriptor >= 0) {
30 close(self->socketDescriptor);
31 self->socketDescriptor = -1;
32 }
Jeff Thompson7850d782013-07-14 17:59:14 -070033
Jeff Thompson0d567da2013-07-14 22:10:21 -070034 struct addrinfo hints;
Jeff Thompson6c12a432013-07-15 12:09:12 -070035 ndn_memset((unsigned char *)&hints, 0, sizeof(hints));
Jeff Thompson7850d782013-07-14 17:59:14 -070036 hints.ai_family = AF_UNSPEC;
37 hints.ai_socktype = SOCK_STREAM;
38
Jeff Thompson0d567da2013-07-14 22:10:21 -070039 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 Thompson7850d782013-07-14 17:59:14 -070045
46 // loop through all the results and connect to the first we can
47 struct addrinfo *p;
Jeff Thompson0d567da2013-07-14 22:10:21 -070048 int socketDescriptor;
Jeff Thompson7850d782013-07-14 17:59:14 -070049 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 Thompson0d567da2013-07-14 22:10:21 -070061 if (p == NULL) {
62 freeaddrinfo(serverInfo);
63 return NDN_ERROR_TcpTransport_cannot_connect_to_socket;
64 }
Jeff Thompson7850d782013-07-14 17:59:14 -070065
Jeff Thompson0d567da2013-07-14 22:10:21 -070066 freeaddrinfo(serverInfo);
67 self->socketDescriptor = socketDescriptor;
Jeff Thompson7850d782013-07-14 17:59:14 -070068
Jeff Thompson0d567da2013-07-14 22:10:21 -070069 return 0;
70}
71
72ndn_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 Thompson7850d782013-07-14 17:59:14 -070077 int nBytes;
78 while (1) {
Jeff Thompson0d567da2013-07-14 22:10:21 -070079 if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0)
80 return NDN_ERROR_TcpTransport_error_in_send;
Jeff Thompson7850d782013-07-14 17:59:14 -070081 if (nBytes >= dataLength)
82 break;
83
Jeff Thompson0d567da2013-07-14 22:10:21 -070084 // Send more.
Jeff Thompson7850d782013-07-14 17:59:14 -070085 dataLength -= nBytes;
86 }
Jeff Thompson0d567da2013-07-14 22:10:21 -070087
88 return 0;
89}
90
91ndn_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 Thompson7850d782013-07-14 17:59:14 -0700102
Jeff Thompson0d567da2013-07-14 22:10:21 -0700103 return 0;
Jeff Thompson7850d782013-07-14 17:59:14 -0700104}