-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient6.c
More file actions
150 lines (123 loc) · 3.33 KB
/
client6.c
File metadata and controls
150 lines (123 loc) · 3.33 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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/ip6.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define CMSG_BUFLEN 64
#define BUFLEN 512
#define NPACK 10
#define PORT 9930
#define SRV_IP "::1"
#include "sockopt.h"
void diep(char *s)
{
perror(s);
exit(1);
}
/*Create a simple msghdr with one iov, and buffer for ancillary data */
void msghdr_create(struct msghdr* m, struct iovec* iov,
void* buffer, int buflen,
void* control_buf, int control_len,
struct sockaddr_in6* saddr)
{
memset(m,0,sizeof(struct msghdr));
memset(iov,0,sizeof(struct iovec));
iov[0].iov_base = buffer;
iov[0].iov_len = buflen;
m->msg_iov = iov;
m->msg_iovlen = 1;
m->msg_name = saddr;
m->msg_namelen = sizeof(struct sockaddr_in6);
m->msg_control = control_buf;
m->msg_controllen = control_len;
}
int main(int argc, char *argv[])
{
struct sockaddr_in6 si_other;
uint32_t s, i, slen=sizeof(si_other);
struct msghdr msg;
struct iovec iov;
uint8_t buf[BUFLEN];
uint8_t cmsgbuf[CMSG_BUFLEN];
uint32_t on = 1;
uint8_t join_mcgrp;
uint32_t acclen = 0;
int c;
char *etherdev = NULL;
char *dest = NULL;
char *mc_addr = NULL;
char *port = NULL;
int buflen = BUFLEN;
while ((c = getopt (argc, argv, "d:e:m:p:b:")) != -1)
{
switch (c) {
case 'd':
dest = optarg;
break;
case 'e':
etherdev = optarg;
break;
case 'm':
mc_addr = optarg;
break;
case 'p':
port = optarg;
break;
case 'b':
buflen=atoi(optarg);
break;
}
}
if (dest == NULL)
diep("destination address reqd\n");
printf("send packets to %s\n", dest);
if ((s=socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("Socket");
memset(cmsgbuf,0,CMSG_BUFLEN);
memset((char *) &si_other, 0, sizeof(si_other));
if (etherdev)
si_other.sin6_scope_id = if_nametoindex(etherdev);
else
printf("no etherdevice specified, scope will be 0\n");
si_other.sin6_family = AF_INET6;
int myport = PORT;
if(port)
myport = atoi(port);
si_other.sin6_port = htons(myport);
if (inet_pton(AF_INET6, dest, &si_other.sin6_addr) == 0) {
fprintf(stderr, "inet_aton() failed\n");
diep("aton");
}
if (mc_addr) {
struct ipv6_mreq mreq;
if (inet_pton(AF_INET6, mc_addr, &mreq.ipv6mr_multiaddr) == 0) {
diep("aton/mcast");
}
mreq.ipv6mr_interface = 0; //any if
printf("join mc group %s\n", mc_addr);
if (setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char*) &mreq, sizeof(mreq)) != 0)
diep("IPV6_ADD_MEMBERSHIP");
}
#define DATASZ 5000
char *message = (char *) malloc(10000);
char *data = (char *) malloc(DATASZ);
memset(data, 'A', DATASZ);
char hostname[128];
gethostname(hostname, sizeof hostname);
double count = 0;
while(1) {
printf("Sending packet: %lf\n", count);
snprintf(message, buflen, "%s:%lf:%s ", hostname, count, data);
if(sendto(s, message, buflen, 0, (struct sockaddr*) &si_other, slen) == -1)
diep("sendto");
usleep(100000);
count++;
}
close(s);
return 0;
}