diff --git a/libretro-common/include/net/net_ifinfo.h b/libretro-common/include/net/net_ifinfo.h new file mode 100644 index 0000000000..982226f895 --- /dev/null +++ b/libretro-common/include/net/net_ifinfo.h @@ -0,0 +1,49 @@ +/* Copyright (C) 2010-2016 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (net_ifinfo.h). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef _LIBRETRO_NET_IFINFO_H +#define _LIBRETRO_NET_IFINFO_H + +#include +#include + +#include + +struct net_ifinfo_entry +{ + char *name; + char *host; +}; + +struct net_ifinfo +{ + struct net_ifinfo_entry *entries; + size_t size; +}; + +typedef struct net_ifinfo net_ifinfo_t; + +void net_ifinfo_free(net_ifinfo_t *list); + +bool net_ifinfo_new(net_ifinfo_t *list); + +#endif diff --git a/libretro-common/net/Makefile b/libretro-common/net/Makefile index f3392b6fd3..5dfdf3b185 100644 --- a/libretro-common/net/Makefile +++ b/libretro-common/net/Makefile @@ -1,21 +1,44 @@ -TARGET := http_test +TARGETS = http_test net_ifinfo + +INCFLAGS = -I../include -I../include/net SOURCES := $(wildcard *.c) \ ../compat/compat.c OBJS := $(SOURCES:.c=.o) -CFLAGS += -Wall -pedantic -std=gnu99 -O0 -g -I../include +ifeq ($(DEBUG),1) +CFLAGS += -O0 -g +else +CFLAGS += -O2 +endif +CFLAGS += -Wall -pedantic -std=gnu99 -all: $(TARGET) +HTTP_TEST_C = \ + net_http.c \ + net_http_test.c \ + net_compat.c \ + ../compat/compat_strl.c + +HTTP_TEST_OBJS := $(HTTP_TEST_C:.c=.o) + +NET_IFINFO_C = \ + net_ifinfo.c \ + net_ifinfo_test.c + +NET_IFINFO_OBJS := $(NET_IFINFO_C:.c=.o) + +.PHONY: all clean + +all: $(TARGETS) %.o: %.c - $(CC) -c -o $@ $< $(CFLAGS) + $(CC) $(INCFLAGS) $< -c $(CFLAGS) -o $@ -$(TARGET): $(OBJS) - $(CC) -o $@ $^ $(LDFLAGS) +http_test: $(HTTP_TEST_OBJS) + $(CC) $(INCFLAGS) $(HTTP_TEST_OBJS) $(CFLAGS) -o $@ + +net_ifinfo: $(NET_IFINFO_OBJS) + $(CC) $(INCFLAGS) $(NET_IFINFO_OBJS) $(CFLAGS) -o $@ clean: - rm -f $(TARGET) $(OBJS) - -.PHONY: clean - + rm -rf $(TARGETS) $(HTTP_TEST_OBJS) $(NET_IFINFO_OBJS) diff --git a/libretro-common/net/net_http_test.c b/libretro-common/net/net_http_test.c index 9e21aeb258..e6940a5373 100644 --- a/libretro-common/net/net_http_test.c +++ b/libretro-common/net/net_http_test.c @@ -31,7 +31,7 @@ int main(void) { char *data; - http_t *http1, *http3; + struct http_t *http1, *http3; size_t len, pos = 0, tot = 0; if (!network_init()) diff --git a/libretro-common/net/net_ifinfo.c b/libretro-common/net/net_ifinfo.c new file mode 100644 index 0000000000..8f39c38578 --- /dev/null +++ b/libretro-common/net/net_ifinfo.c @@ -0,0 +1,115 @@ +/* Copyright (C) 2010-2016 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (compat_fnmatch.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include + +void net_ifinfo_free(net_ifinfo_t *list) +{ + unsigned k; + + if (!list) + return; + + for (k = 0; k < list->size; k++) + { + struct net_ifinfo_entry *ptr = + (struct net_ifinfo_entry*)&list->entries[k]; + + if (!ptr) + continue; + + if (*ptr->name) + free(ptr->name); + if (*ptr->host) + free(ptr->host); + + ptr->name = NULL; + ptr->host = NULL; + } + free(list->entries); + free(list); +} + +bool net_ifinfo_new(net_ifinfo_t *list) +{ + unsigned k = 0; + struct ifaddrs *ifa = NULL; + struct ifaddrs *ifaddr = NULL; + + if (getifaddrs(&ifaddr) == -1) + goto error; + + if (!list) + goto error; + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) + { + char host[NI_MAXHOST]; + int s = 0; + + if (!ifa->ifa_addr) + continue; + + if (ifa->ifa_addr->sa_family != AF_INET) + continue; + + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), + host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + + if (s != 0) + goto error; + + { + struct net_ifinfo_entry *ptr = (struct net_ifinfo_entry*) + realloc(list->entries, (k+1) * sizeof(struct net_ifinfo_entry)); + + if (!ptr) + goto error; + + list->entries = ptr; + } + + list->entries[k].name = strdup(ifa->ifa_name); + list->entries[k].host = strdup(host); + k++; + list->size = k; + } + + freeifaddrs(ifaddr); + + return true; + +error: + freeifaddrs(ifaddr); + net_ifinfo_free(list); + + return false; +} diff --git a/libretro-common/net/net_ifinfo_test.c b/libretro-common/net/net_ifinfo_test.c new file mode 100644 index 0000000000..aef3d96e42 --- /dev/null +++ b/libretro-common/net/net_ifinfo_test.c @@ -0,0 +1,49 @@ +/* Copyright (C) 2010-2016 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (compat_fnmatch.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include + +int main(int argc, const char *argv[]) +{ + unsigned k = 0; + net_ifinfo_t *list = + (net_ifinfo_t*)calloc(1, sizeof(*list)); + + if (!list) + return -1; + + if (!net_ifinfo_new(list)) + return -1; + + for (k = 0; k < list->size; k++) + { + printf("%s:%s\n", list->entries[k].name, list->entries[k].host); + } + + net_ifinfo_free(list); + + return 0; +}