xref: /dflybsd-src/lib/libdmsg/subs.c (revision 9e1c08804a46f1c1a9cd11e190ddba7d2bc4abed)
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "dmsg_local.h"
37 
38 /*
39  * Allocation wrappers give us shims for possible future use
40  */
41 void *
42 dmsg_alloc(size_t bytes)
43 {
44 	void *ptr;
45 
46 	ptr = malloc(bytes);
47 	assert(ptr);
48 	bzero(ptr, bytes);
49 	return (ptr);
50 }
51 
52 void
53 dmsg_free(void *ptr)
54 {
55 	free(ptr);
56 }
57 
58 const char *
59 dmsg_uuid_to_str(uuid_t *uuid, char **strp)
60 {
61 	uint32_t status;
62 	if (*strp) {
63 		free(*strp);
64 		*strp = NULL;
65 	}
66 	uuid_to_string(uuid, strp, &status);
67 	return (*strp);
68 }
69 
70 const char *
71 dmsg_peer_type_to_str(uint8_t type)
72 {
73 	switch(type) {
74 	case DMSG_PEER_NONE:
75 		return("NONE");
76 	case DMSG_PEER_CLUSTER:
77 		return("CLUSTER");
78 	case DMSG_PEER_BLOCK:
79 		return("BLOCK");
80 	case DMSG_PEER_HAMMER2:
81 		return("HAMMER2");
82 	default:
83 		return("?PEERTYPE?");
84 	}
85 }
86 
87 int
88 dmsg_connect(const char *hostname)
89 {
90 	struct sockaddr_in lsin;
91 	struct hostent *hen;
92 	int fd;
93 
94 	/*
95 	 * Acquire socket and set options
96 	 */
97 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
98 		fprintf(stderr, "cmd_debug: socket(): %s\n",
99 			strerror(errno));
100 		return -1;
101 	}
102 
103 	/*
104 	 * Connect to the target
105 	 */
106 	bzero(&lsin, sizeof(lsin));
107 	lsin.sin_family = AF_INET;
108 	lsin.sin_addr.s_addr = 0;
109 	lsin.sin_port = htons(DMSG_LISTEN_PORT);
110 
111 	if (hostname) {
112 		hen = gethostbyname2(hostname, AF_INET);
113 		if (hen == NULL) {
114 			if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
115 				fprintf(stderr,
116 					"Cannot resolve %s\n", hostname);
117 				return -1;
118 			}
119 		} else {
120 			bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
121 		}
122 	}
123 	if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
124 		close(fd);
125 		fprintf(stderr, "debug: connect failed: %s\n",
126 			strerror(errno));
127 		return -1;
128 	}
129 	return (fd);
130 }
131