trunk/src/osd/sdl/netdev_pcap.c
| r243098 | r243099 | |
| 6 | 6 | #include "emu.h" |
| 7 | 7 | #include "osdnet.h" |
| 8 | 8 | |
| 9 | #define pcap_compile_dl pcap_compile |
| 10 | #define pcap_findalldevs_dl pcap_findalldevs |
| 11 | #define pcap_open_live_dl pcap_open_live |
| 12 | #define pcap_next_ex_dl pcap_next_ex |
| 13 | #define pcap_close_dl pcap_close |
| 14 | #define pcap_setfilter_dl pcap_setfilter |
| 15 | #define pcap_sendpacket_dl pcap_sendpacket |
| 16 | #define pcap_set_datalink_dl pcap_set_datalink |
| 17 | |
| 18 | |
| 9 | 19 | class netdev_pcap : public netdev |
| 10 | 20 | { |
| 11 | 21 | public: |
| r243098 | r243099 | |
| 24 | 34 | : netdev(ifdev, rate) |
| 25 | 35 | { |
| 26 | 36 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 27 | | m_p = pcap_open_live(name, 65535, 1, 1, errbuf); |
| 37 | m_p = pcap_open_live_dl(name, 65535, 1, 1, errbuf); |
| 28 | 38 | if(!m_p) |
| 29 | 39 | { |
| 30 | 40 | osd_printf_verbose("Unable to open %s: %s\n", name, errbuf); |
| 31 | 41 | return; |
| 32 | 42 | } |
| 33 | | if(pcap_set_datalink(m_p, DLT_EN10MB) == -1) |
| 43 | if(pcap_set_datalink_dl(m_p, DLT_EN10MB) == -1) |
| 34 | 44 | { |
| 35 | | osd_printf_verbose("Unable to set %s to ethernet", name); |
| 36 | | pcap_close(m_p); |
| 45 | osd_printf_verbose("Unable to set %s to ethernet\n", name); |
| 46 | pcap_close_dl(m_p); |
| 37 | 47 | m_p = NULL; |
| 38 | 48 | return; |
| 39 | 49 | } |
| r243098 | r243099 | |
| 46 | 56 | struct bpf_program fp; |
| 47 | 57 | if(!m_p) return; |
| 48 | 58 | sprintf(filter, "ether dst %.2X:%.2X:%.2X:%.2X:%.2X:%.2X or ether multicast or ether broadcast", (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2],(unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5]); |
| 49 | | pcap_compile(m_p, &fp, filter, 1, 0); |
| 50 | | pcap_setfilter(m_p, &fp); |
| 59 | if(pcap_compile_dl(m_p, &fp, filter, 1, 0) == -1) { |
| 60 | osd_printf_verbose("Error with pcap_compile\n"); |
| 61 | } |
| 62 | if(pcap_setfilter_dl(m_p, &fp) == -1) { |
| 63 | osd_printf_verbose("Error with pcap_setfilter\n"); |
| 64 | } |
| 51 | 65 | } |
| 52 | 66 | |
| 53 | 67 | int netdev_pcap::send(UINT8 *buf, int len) |
| 54 | 68 | { |
| 55 | 69 | if(!m_p) return 0; |
| 56 | | return (!pcap_sendpacket(m_p, buf, len))?len:0; |
| 70 | return (!pcap_sendpacket_dl(m_p, buf, len))?len:0; |
| 57 | 71 | } |
| 58 | 72 | |
| 59 | 73 | int netdev_pcap::recv_dev(UINT8 **buf) |
| 60 | 74 | { |
| 61 | 75 | struct pcap_pkthdr *header; |
| 62 | 76 | if(!m_p) return 0; |
| 63 | | return (pcap_next_ex(m_p, &header, (const u_char **)buf) == 1)?header->len:0; |
| 77 | return (pcap_next_ex_dl(m_p, &header, (const u_char **)buf) == 1)?header->len:0; |
| 64 | 78 | } |
| 65 | 79 | |
| 66 | 80 | netdev_pcap::~netdev_pcap() |
| 67 | 81 | { |
| 68 | | if(m_p) pcap_close(m_p); |
| 82 | if(m_p) pcap_close_dl(m_p); |
| 69 | 83 | } |
| 70 | 84 | |
| 71 | 85 | static CREATE_NETDEV(create_pcap) |
| r243098 | r243099 | |
| 78 | 92 | { |
| 79 | 93 | pcap_if_t *devs; |
| 80 | 94 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 81 | | if(pcap_findalldevs(&devs, errbuf) == -1) |
| 95 | if(pcap_findalldevs_dl(&devs, errbuf) == -1) |
| 82 | 96 | { |
| 83 | 97 | osd_printf_verbose("Unable to get network devices: %s\n", errbuf); |
| 84 | 98 | return; |
| 85 | 99 | } |
| 86 | 100 | |
| 87 | | if (devs) |
| 88 | | { |
| 89 | | while(devs->next) |
| 90 | | { |
| 91 | | add_netdev(devs->name, devs->description, create_pcap); |
| 92 | | devs = devs->next; |
| 93 | | } |
| 94 | | } |
| 101 | while(devs) |
| 102 | { |
| 103 | add_netdev(devs->name, devs->description, create_pcap); |
| 104 | devs = devs->next; |
| 105 | } |
| 95 | 106 | } |
| 96 | 107 | |
| 97 | 108 | void deinit_pcap() |
trunk/src/osd/sdl/netdev_pcap_osx.c
| r243098 | r243099 | |
| 8 | 8 | #include <pthread.h> |
| 9 | 9 | #include <libkern/OSAtomic.h> |
| 10 | 10 | |
| 11 | #define pcap_compile_dl pcap_compile |
| 12 | #define pcap_findalldevs_dl pcap_findalldevs |
| 13 | #define pcap_open_live_dl pcap_open_live |
| 14 | #define pcap_next_ex_dl pcap_next_ex |
| 15 | #define pcap_close_dl pcap_close |
| 16 | #define pcap_setfilter_dl pcap_setfilter |
| 17 | #define pcap_sendpacket_dl pcap_sendpacket |
| 18 | #define pcap_set_datalink_dl pcap_set_datalink |
| 19 | |
| 11 | 20 | struct netdev_pcap_context { |
| 12 | 21 | UINT8 *pkt; |
| 13 | 22 | int len; |
| r243098 | r243099 | |
| 61 | 70 | : netdev(ifdev, rate) |
| 62 | 71 | { |
| 63 | 72 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 64 | | m_p = pcap_open_live(name, 65535, 1, 1, errbuf); |
| 73 | m_p = pcap_open_live_dl(name, 65535, 1, 1, errbuf); |
| 65 | 74 | if(!m_p) |
| 66 | 75 | { |
| 67 | 76 | osd_printf_verbose("Unable to open %s: %s\n", name, errbuf); |
| 68 | 77 | return; |
| 69 | 78 | } |
| 70 | | if(pcap_set_datalink(m_p, DLT_EN10MB) == -1) |
| 79 | if(pcap_set_datalink_dl(m_p, DLT_EN10MB) == -1) |
| 71 | 80 | { |
| 72 | 81 | osd_printf_verbose("Unable to set %s to ethernet\n", name); |
| 73 | | pcap_close(m_p); |
| 82 | pcap_close_dl(m_p); |
| 74 | 83 | m_p = NULL; |
| 75 | 84 | return; |
| 76 | 85 | } |
| r243098 | r243099 | |
| 88 | 97 | struct bpf_program fp; |
| 89 | 98 | if(!m_p) return; |
| 90 | 99 | sprintf(filter, "not ether src %.2X:%.2X:%.2X:%.2X:%.2X:%.2X and (ether dst %.2X:%.2X:%.2X:%.2X:%.2X:%.2X or ether multicast or ether broadcast or ether dst 09:00:07:ff:ff:ff)", (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2],(unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5], (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2],(unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5]); |
| 91 | | if(pcap_compile(m_p, &fp, filter, 1, 0) == -1) { |
| 100 | if(pcap_compile_dl(m_p, &fp, filter, 1, 0) == -1) { |
| 92 | 101 | osd_printf_verbose("Error with pcap_compile\n"); |
| 93 | 102 | } |
| 94 | | if(pcap_setfilter(m_p, &fp) == -1) { |
| 103 | if(pcap_setfilter_dl(m_p, &fp) == -1) { |
| 95 | 104 | osd_printf_verbose("Error with pcap_setfilter\n"); |
| 96 | 105 | } |
| 97 | 106 | } |
| r243098 | r243099 | |
| 99 | 108 | int netdev_pcap::send(UINT8 *buf, int len) |
| 100 | 109 | { |
| 101 | 110 | if(!m_p) return 0; |
| 102 | | return (!pcap_sendpacket(m_p, buf, len))?len:0; |
| 111 | return (!pcap_sendpacket_dl(m_p, buf, len))?len:0; |
| 103 | 112 | } |
| 104 | 113 | |
| 105 | 114 | int netdev_pcap::recv_dev(UINT8 **buf) |
| r243098 | r243099 | |
| 121 | 130 | |
| 122 | 131 | netdev_pcap::~netdev_pcap() |
| 123 | 132 | { |
| 124 | | if(m_p) pcap_close(m_p); |
| 133 | if(m_p) pcap_close_dl(m_p); |
| 125 | 134 | } |
| 126 | 135 | |
| 127 | 136 | static CREATE_NETDEV(create_pcap) |
| r243098 | r243099 | |
| 134 | 143 | { |
| 135 | 144 | pcap_if_t *devs; |
| 136 | 145 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 137 | | if(pcap_findalldevs(&devs, errbuf) == -1) |
| 146 | if(pcap_findalldevs_dl(&devs, errbuf) == -1) |
| 138 | 147 | { |
| 139 | 148 | osd_printf_verbose("Unable to get network devices: %s\n", errbuf); |
| 140 | 149 | return; |
| 141 | 150 | } |
| 142 | 151 | |
| 152 | #if 1 |
| 153 | while(devs) |
| 154 | { |
| 155 | add_netdev(devs->name, devs->description, create_pcap); |
| 156 | devs = devs->next; |
| 157 | } |
| 158 | #else |
| 143 | 159 | if (devs) |
| 144 | 160 | { |
| 145 | 161 | while(devs->next) |
| r243098 | r243099 | |
| 148 | 164 | devs = devs->next; |
| 149 | 165 | } |
| 150 | 166 | } |
| 167 | #endif |
| 151 | 168 | } |
| 152 | 169 | |
| 153 | 170 | void deinit_pcap() |
trunk/src/osd/sdl/sdlos_win32.c
| r243098 | r243099 | |
| 102 | 102 | return result; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | | //============================================================ |
| 106 | | // astring_from_utf8 |
| 107 | | //============================================================ |
| 108 | | |
| 109 | | CHAR *astring_from_utf8(const char *utf8string) |
| 110 | | { |
| 111 | | WCHAR *wstring; |
| 112 | | int char_count; |
| 113 | | CHAR *result; |
| 114 | | |
| 115 | | // convert MAME string (UTF-8) to UTF-16 |
| 116 | | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 117 | | wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); |
| 118 | | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count); |
| 119 | | |
| 120 | | // convert UTF-16 to "ANSI code page" string |
| 121 | | char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL); |
| 122 | | result = (CHAR *)osd_malloc_array(char_count * sizeof(*result)); |
| 123 | | if (result != NULL) |
| 124 | | WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL); |
| 125 | | |
| 126 | | return result; |
| 127 | | } |
| 128 | | |
| 129 | | //============================================================ |
| 130 | | // wstring_from_utf8 |
| 131 | | //============================================================ |
| 132 | | |
| 133 | | WCHAR *wstring_from_utf8(const char *utf8string) |
| 134 | | { |
| 135 | | int char_count; |
| 136 | | WCHAR *result; |
| 137 | | |
| 138 | | // convert MAME string (UTF-8) to UTF-16 |
| 139 | | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 140 | | result = (WCHAR *)osd_malloc_array(char_count * sizeof(*result)); |
| 141 | | if (result != NULL) |
| 142 | | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count); |
| 143 | | |
| 144 | | return result; |
| 145 | | } |
| 146 | | |
trunk/src/osd/sdl/strconv.c
| r243098 | r243099 | |
| 1 | // license:BSD-3-Clause |
| 2 | // copyright-holders:Aaron Giles |
| 1 | 3 | //============================================================ |
| 2 | 4 | // |
| 3 | | // strconv.c - SDL (POSIX) string conversion |
| 5 | // strconv.c - Win32 string conversion |
| 4 | 6 | // |
| 5 | | // Copyright (c) 1996-2010, Nicola Salmoria and the MAME Team. |
| 6 | | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | | // |
| 8 | | // SDLMAME by Olivier Galibert and R. Belmont |
| 9 | | // |
| 10 | 7 | //============================================================ |
| 11 | 8 | |
| 12 | | #ifdef SDLMAME_WIN32 |
| 9 | #if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS) |
| 13 | 10 | #define WIN32_LEAN_AND_MEAN |
| 14 | 11 | #include <windows.h> |
| 15 | 12 | #endif |
| 16 | 13 | |
| 17 | | #include <stdlib.h> |
| 18 | | |
| 19 | 14 | // MAMEOS headers |
| 20 | 15 | #include "strconv.h" |
| 21 | 16 | #include "unicode.h" |
| 22 | 17 | |
| 23 | | #ifdef SDLMAME_WIN32 |
| 18 | #if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS) |
| 24 | 19 | //============================================================ |
| 20 | // astring_from_utf8 |
| 21 | //============================================================ |
| 22 | |
| 23 | CHAR *astring_from_utf8(const char *utf8string) |
| 24 | { |
| 25 | WCHAR *wstring; |
| 26 | int char_count; |
| 27 | CHAR *result; |
| 28 | |
| 29 | // convert MAME string (UTF-8) to UTF-16 |
| 30 | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 31 | wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); |
| 32 | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count); |
| 33 | |
| 34 | // convert UTF-16 to "ANSI code page" string |
| 35 | char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL); |
| 36 | result = (CHAR *)osd_malloc_array(char_count * sizeof(*result)); |
| 37 | if (result != NULL) |
| 38 | WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL); |
| 39 | |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | //============================================================ |
| 25 | 45 | // utf8_from_astring |
| 26 | 46 | //============================================================ |
| 27 | 47 | |
| r243098 | r243099 | |
| 45 | 65 | return result; |
| 46 | 66 | } |
| 47 | 67 | |
| 68 | |
| 48 | 69 | //============================================================ |
| 70 | // wstring_from_utf8 |
| 71 | //============================================================ |
| 72 | |
| 73 | WCHAR *wstring_from_utf8(const char *utf8string) |
| 74 | { |
| 75 | int char_count; |
| 76 | WCHAR *result; |
| 77 | |
| 78 | // convert MAME string (UTF-8) to UTF-16 |
| 79 | char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); |
| 80 | result = (WCHAR *)osd_malloc_array(char_count * sizeof(*result)); |
| 81 | if (result != NULL) |
| 82 | MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count); |
| 83 | |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | //============================================================ |
| 49 | 89 | // utf8_from_wstring |
| 50 | 90 | //============================================================ |
| 51 | 91 | |
| r243098 | r243099 | |
| 62 | 102 | |
| 63 | 103 | return result; |
| 64 | 104 | } |
| 65 | | #endif |
| 66 | 105 | |
| 67 | 106 | //============================================================ |
| 68 | 107 | // osd_uchar_from_osdchar |
| 69 | 108 | //============================================================ |
| 70 | 109 | |
| 71 | | int osd_uchar_from_osdchar(unicode_char *uchar, const char *osdchar, size_t count) |
| 110 | int osd_uchar_from_osdchar(UINT32 *uchar, const char *osdchar, size_t count) |
| 72 | 111 | { |
| 73 | | wchar_t wch; |
| 112 | WCHAR wch; |
| 74 | 113 | |
| 75 | | count = mbstowcs(&wch, (char *)osdchar, 1); |
| 76 | | if (count != -1) |
| 114 | count = MIN(count, IsDBCSLeadByte(*osdchar) ? 2 : 1); |
| 115 | if (MultiByteToWideChar(CP_ACP, 0, osdchar, (DWORD)count, &wch, 1) != 0) |
| 77 | 116 | *uchar = wch; |
| 78 | 117 | else |
| 79 | 118 | *uchar = 0; |
| 119 | return (int) count; |
| 120 | } |
| 80 | 121 | |
| 81 | | return count; |
| 122 | #else |
| 123 | |
| 124 | //============================================================ |
| 125 | // osd_uchar_from_osdchar |
| 126 | //============================================================ |
| 127 | |
| 128 | int osd_uchar_from_osdchar(unicode_char *uchar, const char *osdchar, size_t count) |
| 129 | { |
| 130 | wchar_t wch; |
| 131 | |
| 132 | count = mbstowcs(&wch, (char *)osdchar, 1); |
| 133 | if (count != -1) |
| 134 | *uchar = wch; |
| 135 | else |
| 136 | *uchar = 0; |
| 137 | |
| 138 | return count; |
| 82 | 139 | } |
| 140 | |
| 141 | #endif |
trunk/src/osd/windows/strconv.c
| r243098 | r243099 | |
| 6 | 6 | // |
| 7 | 7 | //============================================================ |
| 8 | 8 | |
| 9 | | // standard windows headers |
| 9 | #if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS) |
| 10 | 10 | #define WIN32_LEAN_AND_MEAN |
| 11 | 11 | #include <windows.h> |
| 12 | #endif |
| 12 | 13 | |
| 13 | 14 | // MAMEOS headers |
| 14 | 15 | #include "strconv.h" |
| 15 | 16 | #include "unicode.h" |
| 16 | 17 | |
| 17 | | |
| 18 | #if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS) |
| 18 | 19 | //============================================================ |
| 19 | 20 | // astring_from_utf8 |
| 20 | 21 | //============================================================ |
| r243098 | r243099 | |
| 101 | 102 | |
| 102 | 103 | return result; |
| 103 | 104 | } |
| 105 | |
| 106 | //============================================================ |
| 107 | // osd_uchar_from_osdchar |
| 108 | //============================================================ |
| 109 | |
| 110 | int osd_uchar_from_osdchar(UINT32 *uchar, const char *osdchar, size_t count) |
| 111 | { |
| 112 | WCHAR wch; |
| 113 | |
| 114 | count = MIN(count, IsDBCSLeadByte(*osdchar) ? 2 : 1); |
| 115 | if (MultiByteToWideChar(CP_ACP, 0, osdchar, (DWORD)count, &wch, 1) != 0) |
| 116 | *uchar = wch; |
| 117 | else |
| 118 | *uchar = 0; |
| 119 | return (int) count; |
| 120 | } |
| 121 | |
| 122 | #else |
| 123 | |
| 124 | //============================================================ |
| 125 | // osd_uchar_from_osdchar |
| 126 | //============================================================ |
| 127 | |
| 128 | int osd_uchar_from_osdchar(unicode_char *uchar, const char *osdchar, size_t count) |
| 129 | { |
| 130 | wchar_t wch; |
| 131 | |
| 132 | count = mbstowcs(&wch, (char *)osdchar, 1); |
| 133 | if (count != -1) |
| 134 | *uchar = wch; |
| 135 | else |
| 136 | *uchar = 0; |
| 137 | |
| 138 | return count; |
| 139 | } |
| 140 | |
| 141 | #endif |