xref: /dragonfly/lib/libdmsg/subs.c (revision 82730a9c)
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 const char *
88 dmsg_pfs_type_to_str(uint8_t type)
89 {
90 	switch(type) {
91 	case DMSG_PFSTYPE_NONE:
92 		return("NONE");
93 	case DMSG_PFSTYPE_ADMIN:
94 		return("ADMIN");
95 	case DMSG_PFSTYPE_CLIENT:
96 		return("CLIENT");
97 	case DMSG_PFSTYPE_CACHE:
98 		return("CACHE");
99 	case DMSG_PFSTYPE_COPY:
100 		return("COPY");
101 	case DMSG_PFSTYPE_SLAVE:
102 		return("SLAVE");
103 	case DMSG_PFSTYPE_SOFT_SLAVE:
104 		return("SOFT_SLAVE");
105 	case DMSG_PFSTYPE_SOFT_MASTER:
106 		return("SOFT_MASTER");
107 	case DMSG_PFSTYPE_MASTER:
108 		return("MASTER");
109 	case DMSG_PFSTYPE_SERVER:
110 		return("SERVER");
111 	default:
112 		return("?PFSTYPE?");
113 	}
114 }
115 
116 int
117 dmsg_connect(const char *hostname)
118 {
119 	struct sockaddr_in lsin;
120 	struct hostent *hen;
121 	int fd;
122 
123 	/*
124 	 * Acquire socket and set options
125 	 */
126 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
127 		fprintf(stderr, "cmd_debug: socket(): %s\n",
128 			strerror(errno));
129 		return -1;
130 	}
131 
132 	/*
133 	 * Connect to the target
134 	 */
135 	bzero(&lsin, sizeof(lsin));
136 	lsin.sin_family = AF_INET;
137 	lsin.sin_addr.s_addr = 0;
138 	lsin.sin_port = htons(DMSG_LISTEN_PORT);
139 
140 	if (hostname) {
141 		hen = gethostbyname2(hostname, AF_INET);
142 		if (hen == NULL) {
143 			if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
144 				fprintf(stderr,
145 					"Cannot resolve %s\n", hostname);
146 				return -1;
147 			}
148 		} else {
149 			bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
150 		}
151 	}
152 	if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
153 		close(fd);
154 		fprintf(stderr, "debug: connect failed: %s\n",
155 			strerror(errno));
156 		return -1;
157 	}
158 	return (fd);
159 }
160