Project

General

Profile

Bug #4596 ยป sin6_wire.c

Improved bug-tickler, does UDP sendto() or TCP connect(). - Dan McDonald, 2014-03-03 10:21 PM

 
1
#include <sys/socket.h>
2
#include <netinet/in.h>
3
#include <err.h>
4
#include <netdb.h>
5
#include <string.h>
6

    
7
int
8
main(int argc, char *argv[])
9
{
10
	struct sockaddr_in6 sin6;
11
	int s;
12
	boolean_t udp;
13
	struct addrinfo *ai, hints;
14

    
15
	if (argc < 2) {
16
		fprintf(stderr, "Usage: %s {-u} <IPv6 Destination> {srcid}\n");
17
		exit(1);
18
	}
19

    
20
	udp = (strcmp(argv[1], "-u") == 0);
21

    
22
	if (udp) {
23
		argv++;
24
		argc--;
25
		s = socket(AF_INET6, SOCK_DGRAM, 0);
26
	} else {
27
		s = socket(AF_INET6, SOCK_STREAM, 0);
28
	}
29
	if (s == -1)
30
		err(-1, "socket");
31
	argv++;
32
	argc--;
33

    
34
	memset(&sin6, 0, sizeof (sin6));
35
	sin6.sin6_family = AF_INET6;
36
	/* NOTE: For localhost testing, port must be something locally bound. */
37
	sin6.sin6_port = ntohs(22);
38
	memset(&hints, 0, sizeof (hints));
39
	hints.ai_family = PF_INET6;
40
	if (getaddrinfo(argv[0], NULL, &hints, &ai) != 0)
41
		err(-1, "getaddrinfo(\"%s\")", argv[0]);
42
	/* For now, just assume the first one does it! */
43
	sin6.sin6_addr = ((struct sockaddr_in6 *)(ai->ai_addr))->sin6_addr;
44
	freeaddrinfo(ai);
45

    
46
	if (argc > 1) {
47
		sin6.__sin6_src_id = atoi(argv[1]);
48
		printf("\tUsing __sin6_src_id %d.\n", sin6.__sin6_src_id);
49
	}
50

    
51
	if (udp) {
52
		if (sendto(s, "Hello!!!\n", 10, 0, &sin6, sizeof (sin6)) == -1)
53
			err(-1, "sendto");
54
	} else {
55
		if (connect(s, &sin6, sizeof (sin6)) == -1)
56
			err(-1, "connect");
57
	}
58

    
59
	printf("Made it out!\n");
60
	return (0);
61
}
    (1-1/1)