fixed doxygen output
[libfirm] / ir / net / firmnet.c
1 #include "firmnet_t.h"
2
3 #ifdef _WIN32
4 static int winsock_init(void) {
5         WORD wVersionRequested;
6         WSADATA wsaData;
7         int err;
8
9         wVersionRequested = MAKEWORD( 2, 2 );
10
11         err = WSAStartup( wVersionRequested, &wsaData );
12         if ( err != 0 ) {
13                 /* Tell the user that we could not find a usable */
14                 /* WinSock DLL.                                  */
15                 return 0;
16         }
17
18         /* Confirm that the WinSock DLL supports 2.2.*/
19         /* Note that if the DLL supports versions greater    */
20         /* than 2.2 in addition to 2.2, it will still return */
21         /* 2.2 in wVersion since that is the version we      */
22         /* requested.                                        */
23
24         if ( LOBYTE( wsaData.wVersion ) != 2 ||
25                 HIBYTE( wsaData.wVersion ) != 2 ) {
26                         /* Tell the user that we could not find a usable */
27                         /* WinSock DLL.                                  */
28                         WSACleanup( );
29                         return 0;
30         }
31         return 1;
32 }
33 #endif /* _WIN32 */
34
35 int firmnet_connect_tcp(const char *host, uint16_t port)
36 {
37         struct hostent     *phe;
38         struct protoent    *ppe;
39         struct sockaddr_in sin;
40         int s;
41
42 #ifdef _WIN32
43         winsock_init();
44 #endif
45
46         memset(&sin, 0, sizeof(sin));
47         sin.sin_family = AF_INET;
48         sin.sin_port   = htons(port);
49
50         if ((phe = gethostbyname(host)))
51                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
52         else if((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
53                 fprintf(stderr, "cannot get host entry for %s", host);
54                 return -1;
55         }
56
57         ppe = getprotobyname("tcp");
58         ERRNO_CHECK_RETURN(s = socket(PF_INET, SOCK_STREAM, ppe->p_proto), <, 0, -1);
59         ERRNO_CHECK_RETURN(connect(s, (struct sockaddr *) &sin, sizeof(sin)), <, 0, -1);
60
61         return s;
62 }
63
64 void firmnet_close_socket(int fd) {
65 #ifdef _WIN32
66         closesocket(fd);
67 #else /* _WIN32 */
68         close(fd);
69 #endif /* _WIN32 */
70 }
71
72 /**
73  * Send message of size @p n from buffer @p buf to file descriptor @p fd.
74  * @param fd   The file descriptor, the message should be send to.
75  * @param buf  The buffer containing the message
76  * @param n    The length of the message.
77  * @return Number of bytes written or -1 on failure.
78  */
79 ssize_t firmnet_send(int fd, const void *buf, size_t n)
80 {
81         ssize_t    res;
82         size_t     bytes_written = 0;
83         const char *data = buf;
84
85         do {
86                 res = send(fd, &data[bytes_written], n - bytes_written, 0);
87                 if (res < 0) {
88                         if (errno != EAGAIN)
89                                 return -1;
90                         continue;
91                 }
92
93                 bytes_written += res;
94
95         } while (bytes_written < n);
96
97         return n;
98 }
99
100 /**
101  * Try to read some bytes but block until a certain amount is read.
102  * @param fd The file descriptor.
103  * @param buf The buffer to read into.
104  * @param try The amount of bytes to try to read.
105  * @param at_least block until this many bytes are read.
106  * @return The number of bytes read or -1 on error.
107  */
108 ssize_t firmnet_recv(int fd, void *buf, size_t try, size_t at_least)
109 {
110         ssize_t res;
111         size_t  bytes_read = 0;
112         char   *data       = buf;
113
114         do {
115                 res = recv(fd, &data[bytes_read], try - bytes_read, 0);
116                 if (res <= 0) {
117                         if (res == 0 || errno != EAGAIN)
118                                 return -1;
119                         continue;
120                 }
121
122                 bytes_read += res;
123
124         } while (bytes_read < at_least);
125
126         return bytes_read;
127 }