comparison xsocket.c @ 261:a03df7dc98f8

Add xsocket wrappers
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 15:09:23 +0200
parents
children ebbe0f198322
comparison
equal deleted inserted replaced
260:3622e24a443f 261:a03df7dc98f8
1 #ifdef _CVI_
2 #include <winsock2.h>
3 #include <ansi_c.h>
4 #define strdup(s) StrDup(s)
5 #else
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netdb.h>
16 #include <ctype.h>
17 #include <math.h>
18 #include <stdarg.h>
19 #endif
20
21 #include "xsocket.h"
22
23
24 struct xsocket * xsocket(const char *hostname, const int port)
25 {
26 struct xsocket *s = malloc(sizeof(struct xsocket));
27 if (! s)
28 return NULL;
29
30 s->hostname = strdup(hostname);
31 s->port = port;
32 s->fd = -1;
33
34 return s;
35 }
36
37
38 int xconnect(struct xsocket *s)
39 {
40 int r, sock;
41 struct sockaddr_in addr;
42 struct hostent* host;
43
44 #ifdef _CVI_
45 WSADATA wsdata;
46 WSAStartup(MAKEWORD(2,2), &wsdata);
47 #endif
48
49 if (s->fd >= 0)
50 return 0;
51
52 host = gethostbyname(s->hostname);
53 if (host == NULL)
54 return -errno;
55
56 sock = socket(AF_INET, SOCK_STREAM, 0);
57 if (sock < 0)
58 return -errno;
59
60 addr.sin_family = host->h_addrtype;
61 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
62 addr.sin_port = htons(s->port);
63
64 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
65 if (r < 0)
66 return -errno;
67
68 s->fd = sock;
69 return 0;
70 }
71
72
73 int xsend(struct xsocket *s, const char *data, size_t len)
74 {
75 int r, retry = 2;
76
77 while (--retry) {
78
79 r = xconnect(s);
80 if (r < 0)
81 return r;
82
83 r = send(s->fd, data, len, 0);
84 if (r < 0) {
85 close(s->fd);
86 s->fd = -1;
87 continue;
88 }
89
90 break;
91 }
92
93 return (r < 0) ? -errno : 0;
94 }
95
96
97 int xrecv(struct xsocket *s, char *buffer, size_t len)
98 {
99 return recv(s->fd, buffer, len, 0);
100 }
101
102
103 int msend(struct xsocket *s, const char *data, size_t len)
104 {
105 char *buffer = malloc(len + 2);
106 memcpy(buffer, data, len);
107
108 buffer[len++] = '\r';
109 buffer[len++] = '\n';
110
111 return xsend(s, buffer, len);
112 }
113
114
115 int mrecv(struct xsocket *s, char *buffer, size_t len)
116 {
117 int n;
118
119 n = xrecv(s, buffer, len);
120 if (n < 0)
121 return n;
122
123 if ((buffer[--n] != '\n') || (buffer[--n] != '\r'))
124 return -EINVAL;
125
126 buffer[n] = '\0';
127
128 return n;
129 }
130
131
132 int command(struct xsocket *s, char *frmt, ...)
133 {
134 int r, n;
135 char buffer[1024];
136 va_list v;
137
138 va_start(v, frmt);
139 n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v);
140 va_end(v);
141
142 buffer[n++] = '\r';
143 buffer[n++] = '\n';
144
145 r = xsend(s, buffer, n);
146 if (r < 0)
147 return r;
148
149 r = mrecv(s, buffer, sizeof(buffer));
150 if (r < 0)
151 return r;
152
153 if (strcmp(buffer, "OK") != 0)
154 return -EINVAL;
155
156 return 0;
157 }