fc744d493ba47907c0c2a523d18fbef7a56f53bd
[dragonfly.git] / lib / libdmsg / subs.c
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 int
71 dmsg_connect(const char *hostname)
72 {
73         struct sockaddr_in lsin;
74         struct hostent *hen;
75         int fd;
76
77         /*
78          * Acquire socket and set options
79          */
80         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
81                 fprintf(stderr, "cmd_debug: socket(): %s\n",
82                         strerror(errno));
83                 return -1;
84         }
85
86         /*
87          * Connect to the target
88          */
89         bzero(&lsin, sizeof(lsin));
90         lsin.sin_family = AF_INET;
91         lsin.sin_addr.s_addr = 0;
92         lsin.sin_port = htons(DMSG_LISTEN_PORT);
93
94         if (hostname) {
95                 hen = gethostbyname2(hostname, AF_INET);
96                 if (hen == NULL) {
97                         if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
98                                 fprintf(stderr,
99                                         "Cannot resolve %s\n", hostname);
100                                 return -1;
101                         }
102                 } else {
103                         bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
104                 }
105         }
106         if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
107                 close(fd);
108                 fprintf(stderr, "debug: connect failed: %s\n",
109                         strerror(errno));
110                 return -1;
111         }
112         return (fd);
113 }