~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/datagram.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#ifndef DATAGRAM_H
 
9
#define DATAGRAM_H
 
10
 
 
11
/** @file datagram.h UDP datagram communications.
 
12
This module implements datagram communications using UDP.
 
13
A datagram is a small, fixed size message send to a given
 
14
host and port, which is not guaranteed to arrive.
 
15
<p>
 
16
This module is arguably misnamed: A <tt>struct datagram</tt>
 
17
does not represent a datagram, but rather an open port that
 
18
can be used to send datagrams with @ref datagram_send.
 
19
<p>
 
20
Example sender:
 
21
<pre>
 
22
include "datagram.h"
 
23
include "debug.h"
 
24
 
 
25
const char *message = "hello world!";
 
26
const char *address = "192.168.2.1";
 
27
int port = 40000;
 
28
 
 
29
d = datagram_create(DATAGRAM_PORT_ANY);
 
30
if(!d) fatal("couldn't create datagram port");
 
31
 
 
32
datagram_send(d,message,strlen(message),address,port);
 
33
</pre>
 
34
 
 
35
And an example receiver:
 
36
 
 
37
<pre>
 
38
include "datagram.h"
 
39
include "debug.h"
 
40
 
 
41
char message[DATAGRAM_PAYLOAD_MAX];
 
42
char address[DATAGRAM_ADDRESS_MAX];
 
43
int  port = 40000;
 
44
 
 
45
d = datagram_create(port);
 
46
if(!d) fatal("couldn't create datagram port");
 
47
 
 
48
length = datagram_recv(d,message,sizeof(message),&address,&port,10000000);
 
49
if(length>0) {
 
50
        message[length] = 0;
 
51
        printf("got message: %s\n",message);
 
52
} else {
 
53
        printf("no message received.\n");
 
54
}
 
55
</pre>
 
56
 
 
57
 
 
58
*/
 
59
 
 
60
/** Maximum number of characters in a text formatted datagram address. */
 
61
#define DATAGRAM_ADDRESS_MAX 48
 
62
 
 
63
/** Maximum number of bytes in a datagram payload */
 
64
#define DATAGRAM_PAYLOAD_MAX 65536
 
65
 
 
66
/** Used to indicate any available port. */
 
67
#define DATAGRAM_PORT_ANY 0
 
68
 
 
69
/** The address to send to for broadcasting. */
 
70
#define DATAGRAM_ADDRESS_BROADCAST "255.255.255.255"
 
71
 
 
72
/** Create a new port for sending or receiving datagrams.
 
73
@param port The UDP port number to bind to.  On most versions of Unix, an ordinary user can only bind to ports greater than 1024.
 
74
@return A new object for sending or receiving datagrams.  On failure, returns null and sets errno appropriately.  A very common error is EADDRINUSE, which indicates another process is already bound to that port.
 
75
*/
 
76
struct datagram * datagram_create( int port );
 
77
 
 
78
/** Destroy a datagram port.
 
79
@param d The datagram object to destroy.
 
80
*/
 
81
void datagram_delete( struct datagram *d );
 
82
 
 
83
/** Receive a datagram.
 
84
@param d The datagram object.
 
85
@param data Where to store the received message.
 
86
@param length The length of the buffer, typically DATAGRAM_PAYLOAD_MAX.
 
87
@param addr Pointer to a string of at least DATAGRAM_ADDRESS_MAX characters, which will be filled in with the IP address of the sender.
 
88
@param port Pointer to an integer which will be filled in with the port number of the sender.
 
89
@param timeout Maximum time to wait, in microseconds.
 
90
@return On success, returns the number of bytes received.  On failure, returns less than zero and sets errno appropriately.
 
91
*/
 
92
int datagram_recv( struct datagram *d, char *data, int length, char *addr, int *port, int timeout );
 
93
 
 
94
/** Send a datagram.
 
95
@param d The datagram object.
 
96
@param data The data to send.
 
97
@param length The length of the datagram.
 
98
@param addr The address of the recipient.
 
99
@param port The port of the recipient.
 
100
@return On success, returns the number of bytes sent.  On failure, returns less than zero and sets errno appropriately.
 
101
*/
 
102
int datagram_send( struct datagram *d, const char *data, int length, const char *addr, int port );
 
103
 
 
104
/** Obtain the file descriptor of a datagram object.
 
105
@param d The datagram object.
 
106
@return The file descriptor associated with the underlying socket.
 
107
*/
 
108
int datagram_fd( struct datagram *d );
 
109
 
 
110
#endif