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 *
dmsg_alloc(size_t bytes)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
dmsg_free(void * ptr)53 dmsg_free(void *ptr)
54 {
55 free(ptr);
56 }
57
58 const char *
dmsg_uuid_to_str(uuid_t * uuid,char ** strp)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 *
dmsg_peer_type_to_str(uint8_t type)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_ROUTER:
77 return("ROUTER");
78 case DMSG_PEER_BLOCK:
79 return("BLOCK");
80 case DMSG_PEER_HAMMER2:
81 return("HAMMER2");
82 case DMSG_PEER_CLIENT:
83 return("CLIENT");
84 default:
85 return("?PEERTYPE?");
86 }
87 }
88
89 int
dmsg_connect(const char * hostname)90 dmsg_connect(const char *hostname)
91 {
92 struct sockaddr_in lsin;
93 struct hostent *hen;
94 int fd;
95 int opt;
96
97 /*
98 * Acquire socket and set options
99 */
100 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
101 dm_printf(1, "cmd_debug: socket(): %s\n",
102 strerror(errno));
103 return -1;
104 }
105 opt = 1;
106 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt);
107
108 /*
109 * Connect to the target
110 */
111 bzero(&lsin, sizeof(lsin));
112 lsin.sin_family = AF_INET;
113 lsin.sin_addr.s_addr = 0;
114 lsin.sin_port = htons(DMSG_LISTEN_PORT);
115
116 if (hostname) {
117 hen = gethostbyname2(hostname, AF_INET);
118 if (hen == NULL) {
119 if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
120 dm_printf(3, "Cannot resolve %s\n", hostname);
121 return -1;
122 }
123 } else {
124 bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
125 }
126 }
127 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
128 close(fd);
129 if (DMsgDebugOpt > 2) {
130 dm_printf(3, "debug: Connect failed: %s\n",
131 strerror(errno));
132 }
133 return -1;
134 }
135 return (fd);
136 }
137