COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
tcp.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdarg.h>
5 
6 #ifdef _WIN32
7 #include <winsock2.h>
8 #else
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <netdb.h>
13 #include <unistd.h>
14 #endif
15 
16 #include "tcp.h"
17 
18 int growl_tcp_parse_hostname( const char *const server , int default_port , struct sockaddr_in *const sockaddr );
19 
20 void growl_tcp_write( int sock , const char *const format , ... )
21 {
22  int length;
23  char *output;
24  char *stop;
25 
26  va_list ap;
27 
28  va_start( ap , format );
29  length = vsnprintf( NULL , 0 , format , ap );
30  va_end(ap);
31 
32  va_start(ap,format);
33  output = (char*)malloc(length+1);
34  vsnprintf( output , length+1 , format , ap );
35  va_end(ap);
36 
37  while ((stop = strstr(output, "\r\n"))) strcpy(stop, stop + 1);
38 
39  send( sock , output , length , 0 );
40  send( sock , "\r\n" , 2 , 0 );
41 
42  free(output);
43 }
44 
45 char *growl_tcp_read(int sock) {
46  const int growsize = 80;
47  char c = 0;
48  char* line = (char*) malloc(growsize);
49  int len = growsize, pos = 0;
50  while (line) {
51  if (recv(sock, &c, 1, 0) <= 0) break;
52  if (c == '\r') continue;
53  if (c == '\n') break;
54  line[pos++] = c;
55  if (pos >= len) {
56  len += growsize;
57  line = (char*) realloc(line, len);
58  }
59  }
60  line[pos] = 0;
61  return line;
62 }
63 
64 int growl_tcp_open(const char* server) {
65  int sock = -1;
66 #ifdef _WIN32
67  char on;
68 #else
69  int on;
70 #endif
71  struct sockaddr_in serv_addr;
72 
73  if( growl_tcp_parse_hostname( (const char *const) server , 23053 , &serv_addr ) == -1 )
74  {
75  return -1;
76  }
77 
78  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
79  perror("create socket");
80  return -1;
81  }
82 
83  if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
84  perror("connect");
85  return -1;
86  }
87 
88  on = 1;
89  if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == -1) {
90  perror("setsockopt");
91  return -1;
92  }
93 
94  return sock;
95 }
96 
97 void growl_tcp_close(int sock) {
98 #ifdef _WIN32
99  if (sock > 0) closesocket(sock);
100 #else
101  if (sock > 0) close(sock);
102 #endif
103 }
104 
105 int growl_tcp_parse_hostname( const char *const server , int default_port , struct sockaddr_in *const sockaddr )
106 {
107  char *hostname = malloc (strlen (server) + 1);
108  if(hostname !=NULL) strcpy(hostname,server);
109  char *port = strchr( hostname, ':' );
110  struct hostent* host_ent;
111  if( port != NULL )
112  {
113  *port = '\0';
114  port++;
115  default_port = atoi(port);
116  }
117 
118  host_ent = gethostbyname(hostname);
119  if( host_ent == NULL )
120  {
121  perror("gethostbyname");
122  free(hostname);
123  return -1;
124  }
125 
126  memset( sockaddr , 0 , sizeof(*sockaddr) );
127  sockaddr->sin_family = AF_INET;
128  memcpy( &sockaddr->sin_addr , host_ent->h_addr_list[0] , host_ent->h_length );
129  sockaddr->sin_port = htons(default_port);
130 
131  free(hostname);
132  return 0;
133 }
134 
135 int growl_tcp_datagram( const char *server , const unsigned char *data , const int data_length )
136 {
137  struct sockaddr_in serv_addr;
138  int sock = 0;
139 
140  if( growl_tcp_parse_hostname( server , 9887 , &serv_addr ) == -1 )
141  {
142  return -1;
143  }
144 
145  sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
146  if( sock < 0 )
147  {
148  return -1;
149  }
150 
151  if( sendto(sock, (char*)data , data_length , 0 , (struct sockaddr*)&serv_addr , sizeof(serv_addr) ) > 0 )
152  {
153  return 0;
154  }
155  else
156  {
157  return 1;
158  }
159 }
int growl_tcp_parse_hostname(const char *const server, int default_port, struct sockaddr_in *const sockaddr)
Definition: tcp.c:105
void growl_tcp_write(int sock, const char *const format,...)
Definition: tcp.c:20
int growl_tcp_open(const char *server)
Definition: tcp.c:64
void growl_tcp_close(int sock)
Definition: tcp.c:97
char * growl_tcp_read(int sock)
Definition: tcp.c:45
#define closesocket(socket)
Definition: prowl.h:39
int growl_tcp_datagram(const char *server, const unsigned char *data, const int data_length)
Definition: tcp.c:135
Send comments to:
COOLFluiD Web Admin