- more work on the Unknown problems: the only
[libfirm] / ir / net / firmnet.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Interfaces for TCP/IP handling (Windows and Unix like systems)
23  * @author  Christian Wuerdig, copied from liblpp created by Sebastian Hack
24  * @date    17.11.2006
25  * @version $Id$
26  */
27
28 #ifndef FIRM_NET_FIRMNET_H
29 #define FIRM_NET_FIRMNET_H
30
31 #ifdef _WIN32
32 #include <winsock.h>
33 #include <io.h>
34
35 #else /* _WIN32 */
36 #include <sys/time.h>
37 #include <sys/socket.h>
38 #include <sys/types.h>
39 #include <sys/resource.h>
40 #include <sys/wait.h>
41
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45
46 #include <unistd.h>
47 #endif /* _WIN32 */
48
49 #include <signal.h>
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54
55 #ifdef _MSC_VER
56
57 typedef size_t                          ssize_t;
58 typedef unsigned __int16        uint16_t;
59 typedef unsigned __int32        uint32_t;
60
61 #else /* _MSC_VER */
62
63 #include <stdint.h>
64 #include <unistd.h>
65 #include <errno.h>
66 #include <netinet/in.h>
67
68 #endif /* _MSC_VER */
69
70 /**
71  * Establishes a TCP/IP connection to @p host at port @p port.
72  * @param host Hostname to connect to
73  * @param port Port number
74  * @return The file descriptor on success, -1 otherwise
75  */
76 int firmnet_connect_tcp(const char *host, uint16_t port);
77
78 /**
79  * Closes connection established on socket @p fd.
80  * @param fd The file descriptor identifying the connection
81  */
82 void firmnet_close_socket(int fd);
83
84 /**
85  * Send message of size @p n from buffer @p buf to file descriptor @p fd.
86  * @param fd   The file descriptor, the message should be send to.
87  * @param buf  The buffer containing the message
88  * @param n    The length of the message.
89  * @return Number of bytes written or -1 on failure.
90  */
91 ssize_t firmnet_send(int fd, const void *buf, size_t n);
92
93 /**
94  * Try to read some bytes but block until a certain amount is read.
95  * @param fd The file descriptor.
96  * @param buf The buffer to read into.
97  * @param try The amount of bytes to try to read.
98  * @param at_least block until this many bytes are read.
99  * @return The number of bytes read or -1 on error.
100  */
101 ssize_t firmnet_recv(int fd, void *buf, size_t try, size_t at_least);
102
103 #endif