Line data Source code
1 : /* Copyright (C) 2013 by Ben Martin */
2 : /*
3 : * Redistribution and use in source and binary forms, with or without
4 : * modification, are permitted provided that the following conditions are met:
5 :
6 : * Redistributions of source code must retain the above copyright notice, this
7 : * list of conditions and the following disclaimer.
8 :
9 : * Redistributions in binary form must reproduce the above copyright notice,
10 : * this list of conditions and the following disclaimer in the documentation
11 : * and/or other materials provided with the distribution.
12 :
13 : * The name of the author may not be used to endorse or promote products
14 : * derived from this software without specific prior written permission.
15 :
16 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 : * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 : * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 : * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 : * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 : * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 : * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 : * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 : */
27 : #include <fontforge-config.h>
28 :
29 : #include "inc/gnetwork.h"
30 : #include "inc/ustring.h"
31 :
32 : #ifdef BUILD_COLLAB
33 : #include "czmq.h"
34 : #endif
35 :
36 : #include <sys/types.h>
37 : #include <sys/stat.h>
38 : #include <unistd.h>
39 : #include <stdlib.h>
40 : #include <stdio.h>
41 : #include <limits.h>
42 :
43 : #if defined(__MINGW32__)
44 : # include <winsock2.h>
45 : #else
46 : extern int h_errno;
47 : # include <netdb.h>
48 : #endif
49 :
50 : #include <arpa/inet.h>
51 :
52 0 : char* ff_gethostname( char* outstring, int outstring_sz )
53 : {
54 : char hostname[PATH_MAX+1];
55 0 : int rc = 0;
56 :
57 0 : rc = gethostname( hostname, PATH_MAX );
58 0 : if( rc == -1 )
59 : {
60 0 : strncpy( outstring, "localhost", outstring_sz );
61 0 : return outstring;
62 : }
63 :
64 0 : strncpy( outstring, hostname, outstring_sz );
65 0 : return outstring;
66 : }
67 :
68 :
69 0 : char* getNetworkAddress( char* outstring )
70 : {
71 : char hostname[PATH_MAX+1];
72 0 : int rc = 0;
73 :
74 0 : rc = gethostname( hostname, PATH_MAX );
75 0 : if( rc == -1 )
76 : {
77 0 : return 0;
78 : }
79 0 : printf("hostname: %s\n", hostname );
80 0 : struct hostent * he = gethostbyname( hostname );
81 0 : if( !he )
82 : {
83 0 : return 0;
84 : }
85 0 : if( he->h_addrtype != AF_INET && he->h_addrtype != AF_INET6 )
86 : {
87 0 : return 0;
88 : }
89 :
90 0 : inet_ntop( he->h_addrtype, he->h_addr_list[0],
91 : outstring, IPADDRESS_STRING_LENGTH_T-1 );
92 :
93 0 : return outstring;
94 : }
95 :
96 0 : char* HostPortPack( char* hostname, int port )
97 : {
98 : static char ret[PATH_MAX+1];
99 0 : snprintf(ret,PATH_MAX,"%s:%d",hostname,port);
100 0 : return ret;
101 : }
102 :
103 0 : char* HostPortUnpack( char* packed, int* port, int port_default )
104 : {
105 0 : char* colon = strrchr( packed, ':' );
106 0 : if( !colon )
107 : {
108 0 : *port = port_default;
109 0 : return packed;
110 : }
111 :
112 0 : *colon = '\0';
113 0 : *port = atoi(colon+1);
114 0 : return packed;
115 : }
116 :
117 :
118 : //
119 : // target must be at least 32 bytes + NUL in length.
120 : //
121 0 : char* ff_uuid_generate( char* target )
122 : {
123 : #ifdef BUILD_COLLAB
124 : zuuid_t *uuid = zuuid_new ();
125 : strcpy (target, zuuid_str (uuid));
126 : zuuid_destroy (&uuid);
127 : #else
128 0 : strcpy( target, "" );
129 : #endif // collab guard.
130 :
131 0 : return target;
132 : }
133 :
134 0 : int ff_uuid_isValid( char* uuid )
135 : {
136 0 : if( !uuid ) return 0;
137 0 : if( !strlen(uuid)) return 0;
138 0 : return 1;
139 : }
140 :
|