blob: 30e68284ddf1271109c09e8b9a9917586b1af3ac [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>
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:
21static 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
29ndn_Error ndn_TcpTransport_connect(ndn_TcpTransport *self, char *host, int port)
30{
31
32}
33
34int testTcpTransport(unsigned char *data, unsigned int dataLength)
35{
36 struct addrinfo hints, *serverInfo;
37
38 printf("starting\n");
39
40 memset(&hints, 0, sizeof hints);
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_socktype = SOCK_STREAM;
43
44 if (getaddrinfo("E.hub.ndn.ucla.edu", "9695", &hints, &serverInfo) != 0)
45 return 1;
46
47 // loop through all the results and connect to the first we can
48 struct addrinfo *p;
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
61 if (p == NULL)
62 return 2;
63
64 freeaddrinfo(serverInfo); // all done with this structure
65
66 int nBytes;
67 while (1) {
68 if ((nBytes = send(socketDescriptor, data, dataLength, 0)) < 0)
69 return 1;
70 if (nBytes >= dataLength)
71 break;
72
73 dataLength -= nBytes;
74 }
75
76 unsigned char buffer[1000];
77 if ((nBytes = recv(socketDescriptor, buffer, sizeof(buffer) - 1, 0)) == -1)
78 return 1;
79
80 printf("received %d bytes\n", nBytes);
81 int i;
82 for (i = 0; i < nBytes; ++i)
83 printf("%02X ", (unsigned int)buffer[i]);
84 printf("\n");
85
86 close(socketDescriptor);
87
88 return 0;
89}