Repository navigation
Expand file tree
/
Copy pathtest04_cli.c
More file actions
executable file
·148 lines (126 loc) · 3.49 KB
/
Copy pathtest04_cli.c
File metadata and controls
executable file
·148 lines (126 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Test name: test04_cli.c
*
* Objective: Test a simple UDP client
*
* Description: Implements a simple echo client using the UDP protocol. First
* it waits until it is possible to write (which is always).
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <net/netlib.h>
#include <net/gen/netdb.h>
#include <net/gen/in.h>
#include <net/gen/udp.h>
#include <net/gen/udp_hdr.h>
#include <net/gen/udp_io.h>
#include <net/hton.h>
#define PORT 6000L
/* Type for received data */
typedef struct
{
udp_io_hdr_t header;
char data[1024];
} udp_buffer_t;
int udp_conf(char *host, long port, udp_io_hdr_t *header)
{
/* configures UDP connection */
char *udp_device;
struct hostent *hp;
int netfd;
nwio_udpopt_t udpopt;
ipaddr_t dirhost;
int result;
/* get host address */
if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
{
fprintf(stderr,"Unknown host\n");
return(-1);
}
memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
/* Get UDP device */
if (( udp_device = getenv("UDP_DEVICE") ) == NULL)
udp_device = UDP_DEVICE;
/* Get UDP connection */
if ((netfd = open(udp_device, O_RDWR)) < 0)
{
fprintf(stderr,"Error opening UDP device\n");
return -1;
}
/* Configure UDP connection */
udpopt.nwuo_flags = NWUO_COPY | NWUO_LP_SEL | NWUO_EN_LOC | NWUO_DI_BROAD
| NWUO_RP_SET | NWUO_RA_SET | NWUO_RWDATALL | NWUO_DI_IPOPT;
udpopt.nwuo_remaddr = dirhost;
udpopt.nwuo_remport = (udpport_t) htons(port);
if ((result = ioctl(netfd, NWIOSUDPOPT, &udpopt) ) <0)
{
fprintf(stderr, "Error establishing communication\n");
printf("Error: %d\n",result);
close(netfd);
return -1;
}
/* Get configuration for UDP comm */
if ((result = ioctl(netfd, NWIOGUDPOPT, &udpopt) ) < 0)
{
fprintf(stderr,"Error getting configuration\n");
printf("Error: %d\n", result);
close(netfd);
return -1;
}
header->uih_src_addr = udpopt.nwuo_locaddr;
header->uih_dst_addr = udpopt.nwuo_remaddr;
header->uih_src_port = udpopt.nwuo_locport;
header->uih_dst_port = udpopt.nwuo_remport;
return netfd;
}
int main(int argc,char *argv[]) {
int fd;
ssize_t data_read;
udp_buffer_t buffer_send, buffer_rec;
fd_set fds_write;
int ret;
/* Check parameters */
if (argc !=2) {
fprintf(stderr,"Usage: %s host\n", argv[0]);
exit(-1);
}
if ((fd = udp_conf(argv[1], PORT, &buffer_send.header) ) < 0)
exit(-1);
/* init fd_set */
FD_ZERO(&fds_write);
FD_SET(fd, &fds_write);
while (1)
{
/* Wait until it is possible to write with select */
ret = select(4, NULL, &fds_write, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Error on select waiting for write: %d\n", errno);
exit(-1);
}
if (!FD_ISSET(fd, &fds_write)) {
fprintf(stderr, "Error: The net connection is not ready for writing (?)\n");
exit(-1);
}
/* Get a string and send it */
printf("Ready to write...\n");
printf("Send data: ");
gets(buffer_send.data);
write(fd, &buffer_send, sizeof(udp_buffer_t));
/* If data sent is exit then break */
if (!strcmp(buffer_send.data,"exit"))
break;
/* Get server response */
data_read = read(fd, &buffer_rec, sizeof(udp_buffer_t));
printf("Received: %s\n\n", buffer_rec.data);
}
/* Close UDP communication */
close(fd);
}