Added MINGW support for compiling in Windows

* Still need a fix for missing libresolv.
This commit is contained in:
Jose Marcial Vieira Bisneto 2017-01-12 18:45:44 -03:00
parent 3c3474eacd
commit 7d3418e9c7
7 changed files with 202 additions and 100 deletions

View file

@ -46,9 +46,49 @@ Expect these resolutions:
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <netinet/in.h> #ifdef __MINGW32__
#include <arpa/nameser.h> #include <stdint.h>
#include <resolv.h>
#define u_char unsigned char
#define u_int16_t uint16_t
#define u_int32_t uint32_t
#define NS_MAXDNAME 1025 /*%< maximum domain name */
#define ns_c_in 1
#define ns_t_txt 16
typedef enum __ns_sect {
ns_s_qd = 0, /*%< Query: Question. */
ns_s_zn = 0, /*%< Update: Zone. */
ns_s_an = 1, /*%< Query: Answer. */
ns_s_pr = 1, /*%< Update: Prerequisites. */
ns_s_ns = 2, /*%< Query: Name servers. */
ns_s_ud = 2, /*%< Update: Update. */
ns_s_ar = 3, /*%< Query|Update: Additional records. */
ns_s_max = 4
} ns_sect;
typedef struct __ns_msg {
const unsigned char *_msg, *_eom;
u_int16_t _id, _flags, _counts[ns_s_max];
const unsigned char *_sections[ns_s_max];
ns_sect _sect;
int _rrnum;
const unsigned char *_msg_ptr;
} ns_msg;
typedef struct __ns_rr {
char name[NS_MAXDNAME];
u_int16_t type;
u_int16_t rr_class;
u_int32_t ttl;
u_int16_t rdlength;
const u_char * rdata;
} ns_rr;
#else
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#endif
#include "ipfs/namesys/namesys.h" #include "ipfs/namesys/namesys.h"
#define IPFS_DNSLINK_C #define IPFS_DNSLINK_C
#include "ipfs/dnslink/dnslink.h" #include "ipfs/dnslink/dnslink.h"
@ -57,65 +97,71 @@ Expect these resolutions:
int ipfs_dns (int argc, char **argv) int ipfs_dns (int argc, char **argv)
{ {
int err, r=0, i; #ifdef __MINGW32__
char **txt, *path, *param; fprintf (stderr, "Sorry, dns has not yet been implemented for the Windows version.\n");
if (argc == 4 && strcmp ("-r", argv[2])==0) {
r = 1;
argc--; argv++;
}
if (argc != 3) {
fprintf (stderr, "usage: ipfs dns [-r] dns.name.com\n");
return -1; return -1;
} }
#else
int err, r=0, i;
char **txt, *path, *param;
param = malloc (strlen (argv[2]) + 1); if (argc == 4 && strcmp ("-r", argv[2])==0) {
if (!param) { r = 1;
fprintf (stderr, "memory allocation failed.\n"); argc--; argv++;
return 1;
}
strcpy (param, argv[2]);
for (i = 0 ; i < DefaultDepthLimit ; i++) {
if (memcmp(param, "/ipns/", 6) == 0) {
err = ipfs_dnslink_resolv_lookupTXT (&txt, param+6);
} else {
err = ipfs_dnslink_resolv_lookupTXT (&txt, param);
}
if (err) {
fprintf (stderr, "dns lookupTXT: %s\n", Err[err]);
return err;
} }
err = ipfs_dnslink_parse_txt(&path, *txt); if (argc != 3) {
if (err) { fprintf (stderr, "usage: ipfs dns [-r] dns.name.com\n");
return -1;
}
param = malloc (strlen (argv[2]) + 1);
if (!param) {
fprintf (stderr, "memory allocation failed.\n");
return 1;
}
strcpy (param, argv[2]);
for (i = 0 ; i < DefaultDepthLimit ; i++) {
if (memcmp(param, "/ipns/", 6) == 0) {
err = ipfs_dnslink_resolv_lookupTXT (&txt, param+6);
} else {
err = ipfs_dnslink_resolv_lookupTXT (&txt, param);
}
if (err) {
fprintf (stderr, "dns lookupTXT: %s\n", Err[err]);
return err;
}
err = ipfs_dnslink_parse_txt(&path, *txt);
if (err) {
free (*txt);
free (txt);
fprintf (stderr, "dns parse_txt: %s\n", Err[err]);
return err;
}
free (*txt); free (*txt);
free (txt); free (txt);
fprintf (stderr, "dns parse_txt: %s\n", Err[err]); free (param);
return err;
}
free (*txt); if (! r) {
free (txt); // not recursive.
free (param); break;
}
if (! r) { if (memcmp(path, "/ipfs/", 6) == 0) {
// not recursive. break;
break; }
}
if (memcmp(path, "/ipfs/", 6) == 0) { param = path;
break; } while (--r);
} fprintf (stdout, "%s\n", path);
free (path);
param = path; return 0;
} while (--r); }
fprintf (stdout, "%s\n", path); #endif // MINGW
free (path);
return 0;
}
// ipfs_dnslink_resolve resolves the dnslink at a particular domain. It will // ipfs_dnslink_resolve resolves the dnslink at a particular domain. It will
// recursively keep resolving until reaching the defaultDepth of Resolver. If // recursively keep resolving until reaching the defaultDepth of Resolver. If
@ -190,56 +236,58 @@ int ipfs_dnslink_resolve_n (char **p, char *d, int depth)
return ErrResolveLimit; return ErrResolveLimit;
} }
// lookup using libresolv -lresolv #ifndef __MINGW32__
int ipfs_dnslink_resolv_lookupTXT(char ***txt, char *domain) // lookup using libresolv -lresolv
{ int ipfs_dnslink_resolv_lookupTXT(char ***txt, char *domain)
char buf[4096], *p; {
int responseLength; char buf[4096], *p;
int i, l, n = 0; int responseLength;
ns_msg query_parse_msg; int i, l, n = 0;
ns_rr query_parse_rr; ns_msg query_parse_msg;
u_char responseByte[4096]; ns_rr query_parse_rr;
u_char responseByte[4096];
// Use res_query from libresolv to retrieve TXT record from DNS server. // Use res_query from libresolv to retrieve TXT record from DNS server.
if ((responseLength = res_query(domain,ns_c_in,ns_t_txt,responseByte,sizeof(responseByte))) < 0 || if ((responseLength = res_query(domain,ns_c_in,ns_t_txt,responseByte,sizeof(responseByte))) < 0 ||
ns_initparse(responseByte,responseLength,&query_parse_msg) < 0) { ns_initparse(responseByte,responseLength,&query_parse_msg) < 0) {
return ErrResolveFailed; return ErrResolveFailed;
} else { } else {
l = sizeof (buf); l = sizeof (buf);
buf[--l] = '\0'; buf[--l] = '\0';
p = buf; p = buf;
// save every TXT record to buffer separating with a \0 // save every TXT record to buffer separating with a \0
for (i=0 ; i < ns_msg_count(query_parse_msg,ns_s_an) ; i++) { for (i=0 ; i < ns_msg_count(query_parse_msg,ns_s_an) ; i++) {
if (ns_parserr(&query_parse_msg,ns_s_an,i,&query_parse_rr)) { if (ns_parserr(&query_parse_msg,ns_s_an,i,&query_parse_rr)) {
return ErrResolveFailed; return ErrResolveFailed;
} else { } else {
char *rdata = ns_rr_rdata(query_parse_rr); char *rdata = ns_rr_rdata(query_parse_rr);
memcpy(p, rdata+1, *rdata); // first byte is record length memcpy(p, rdata+1, *rdata); // first byte is record length
p += *rdata; // update pointer p += *rdata; // update pointer
*p++ = '\0'; // mark end-of-record and update pointer to next record. *p++ = '\0'; // mark end-of-record and update pointer to next record.
n++; // update record count n++; // update record count
}
}
// allocate array for all records + NULL pointer terminator.
*txt = calloc(n+1, sizeof(void*));
if (!*txt) {
return ErrAllocFailed;
}
l = p - buf; // length of all records in buffer.
p = malloc(l); // allocate memory that will be used as string data at *txt array.
if (!p) {
free(*txt);
*txt = NULL;
return ErrAllocFailed;
}
memcpy(p, buf, l); // transfer from buffer to allocated memory.
for (i = 0 ; i < n ; i++) {
*txt[i] = p; // save position of current record at *txt array.
p = memchr(p, '\0', l - (p - *txt[0])) + 1; // find next record position after next \0
} }
} }
// allocate array for all records + NULL pointer terminator. return 0;
*txt = calloc(n+1, sizeof(void*));
if (!*txt) {
return ErrAllocFailed;
}
l = p - buf; // length of all records in buffer.
p = malloc(l); // allocate memory that will be used as string data at *txt array.
if (!p) {
free(*txt);
*txt = NULL;
return ErrAllocFailed;
}
memcpy(p, buf, l); // transfer from buffer to allocated memory.
for (i = 0 ; i < n ; i++) {
*txt[i] = p; // save position of current record at *txt array.
p = memchr(p, '\0', l - (p - *txt[0])) + 1; // find next record position after next \0
}
} }
return 0; #endif
}
// ipfs_dnslink_resolve_once implements resolver. // ipfs_dnslink_resolve_once implements resolver.
int ipfs_dnslink_resolve_once (char ***p, char *domain) int ipfs_dnslink_resolve_once (char ***p, char *domain)
@ -257,11 +305,13 @@ int ipfs_dnslink_resolve_once (char ***p, char *domain)
return ErrInvalidDomain; return ErrInvalidDomain;
} }
#ifndef __MINGW32__
if (!ipfs_dnslink_lookup_txt) { // if not set if (!ipfs_dnslink_lookup_txt) { // if not set
ipfs_dnslink_lookup_txt = ipfs_dnslink_resolv_lookupTXT; // use default libresolv ipfs_dnslink_lookup_txt = ipfs_dnslink_resolv_lookupTXT; // use default libresolv
} }
err = ipfs_dnslink_lookup_txt (&txt, domain); err = ipfs_dnslink_lookup_txt (&txt, domain);
#endif
if (err) { if (err) {
return err; return err;
} }

View file

@ -50,8 +50,11 @@ int ipfs_flatfs_create_directory(const char* full_directory) {
return 0; return 0;
} }
// it is not there, create it // it is not there, create it
#ifdef __MINGW32__
if (mkdir(full_directory) == -1)
#else
if (mkdir(full_directory, S_IRWXU) == -1) if (mkdir(full_directory, S_IRWXU) == -1)
return 0; #endif return 0;
return 1; return 1;
} }

View file

@ -6,6 +6,33 @@
#include "ipfs/importer/exporter.h" #include "ipfs/importer/exporter.h"
#include "ipfs/dnslink/dnslink.h" #include "ipfs/dnslink/dnslink.h"
#ifdef __MINGW32__
void bzero(void *s, size_t n)
{
memset (s, '\0', n);
}
char *strtok_r(char *str, const char *delim, char **save)
{
char *res, *last;
if( !save )
return strtok(str, delim);
if( !str && !(str = *save) )
return NULL;
last = str + strlen(str);
if( (*save = res = strtok(str, delim)) )
{
*save += strlen(res);
if( *save < last )
(*save)++;
else
*save = NULL;
}
return res;
}
#endif // MINGW
void stripit(int argc, char** argv) { void stripit(int argc, char** argv) {
char tmp[strlen(argv[argc])]; char tmp[strlen(argv[argc])];
strcpy(tmp, &argv[argc][1]); strcpy(tmp, &argv[argc][1]);

View file

@ -1,9 +1,12 @@
#include "ipfs/os/utils.h" #include "ipfs/os/utils.h"
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <pwd.h> #ifndef __MINGW32__
#include <pwd.h>
#endif
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <dirent.h> #include <dirent.h>
@ -25,7 +28,11 @@ char* os_utils_getenv(const char* variable) {
* @returns true(1) on success * @returns true(1) on success
*/ */
int os_utils_setenv(const char* variable, const char* value, int overwrite) { int os_utils_setenv(const char* variable, const char* value, int overwrite) {
return setenv(variable, value, overwrite); #ifdef __MINGW32__
return 0;
#else
setenv(variable, value, overwrite);
#endif
} }
/** /**
@ -33,8 +40,11 @@ int os_utils_setenv(const char* variable, const char* value, int overwrite) {
* @returns the home directory * @returns the home directory
*/ */
char* os_utils_get_homedir() { char* os_utils_get_homedir() {
#ifndef __MINGW32__
struct passwd *pw = getpwuid(getuid()); struct passwd *pw = getpwuid(getuid());
return pw->pw_dir; return pw->pw_dir;
#endif
return ".";
} }
/** /**

View file

@ -482,7 +482,11 @@ int ipfs_repo_fsrepo_blockstore_init(const struct FSRepo* fs_repo) {
if (retVal == 0) if (retVal == 0)
return 0; return 0;
#ifdef __MINGW32__
if (mkdir(full_path) != 0)
#else
if (mkdir(full_path, S_IRWXU) != 0) if (mkdir(full_path, S_IRWXU) != 0)
#endif
return 0; return 0;
return 1; return 1;
} }

View file

@ -183,6 +183,10 @@ int repo_fsrepo_lmdb_cast(struct Datastore* datastore) {
* @returns true(1) on success * @returns true(1) on success
*/ */
int repo_fsrepo_lmdb_create_directory(struct Datastore* datastore) { int repo_fsrepo_lmdb_create_directory(struct Datastore* datastore) {
#ifdef __MINGW32__
return mkdir(datastore->path) == 0;
#else
return mkdir(datastore->path, S_IRWXU) == 0; return mkdir(datastore->path, S_IRWXU) == 0;
#endif
} }

View file

@ -74,7 +74,11 @@ int ipfs_repo_init(int argc, char** argv) {
return 0; return 0;
} }
// make the directory // make the directory
#ifdef __MINGW32__
if (mkdir(dir) == -1) {
#else
if (mkdir(dir, S_IRWXU) == -1) { if (mkdir(dir, S_IRWXU) == -1) {
#endif
printf("Unable to create the directory: %s\n", dir); printf("Unable to create the directory: %s\n", dir);
return 0; return 0;
} }