]> git.saurik.com Git - wxWidgets.git/blame - src/msw/wince/net.cpp
fixed wrong argument to wxString::Format(); include wx/thread.h
[wxWidgets.git] / src / msw / wince / net.cpp
CommitLineData
882dfc67
JS
1// Copyright 1998, Ben Goetter. All rights reserved.
2
3/*
39fc096d 4 patch holes in winsock
882dfc67
JS
5
6 WCE 2.0 lacks many of the 'database' winsock routines.
7 Stub just enough them for ss.dll.
8
9 getprotobynumber
10 getservbyport
11 getservbyname
12
13*/
14
d5e64615
VS
15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma implementation "net.h"
17#endif
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
9ed0d735 26#include "wx/msw/wrapwin.h"
882dfc67
JS
27#include <tchar.h>
28#include <winsock.h>
29#include <string.h>
30#include "wx/msw/wince/net.h"
31
32
33#define CCH_MAX_PROTO 4
34
35static struct protoent RgProtoEnt[] =
36{
37 { "tcp", {NULL}, 6 },
38 { "udp", {NULL}, 17 },
39 { "icmp", {NULL}, 1 },
40 { "ip", {NULL}, 0 },
41 { NULL, {NULL}, 0 }
42};
43
44
45#define CCH_MAX_SERV 8
46
47// Ordered by most likely to be requested.
48// Assumes that a service available on different protocols
49// will use the same port number on each protocol.
39fc096d 50// Should that be no longer the case,
882dfc67
JS
51// remove the fFoundOnce code from getservbyXxx fcns.
52
53// This table keeps port numbers in host byte order.
54
55static struct servent RgServEnt[] =
56{
57 { "ftp", {NULL}, 21, "tcp" },
58 { "ftp-data", {NULL}, 20, "tcp" },
59 { "telnet", {NULL}, 23, "tcp" },
60 { "smtp", {NULL}, 25, "tcp" },
61 { "http", {NULL}, 80, "tcp" },
62 { "http", {NULL}, 80, "udp" },
63 { "pop", {NULL}, 109, "tcp" },
64 { "pop2", {NULL}, 109, "tcp" },
65 { "pop3", {NULL}, 110, "tcp" },
66 { "nntp", {NULL}, 119, "tcp" },
67 { "finger", {NULL}, 79, "tcp" },
68 /* include most of the simple TCP services for testing */
69 { "echo", {NULL}, 7, "tcp" },
70 { "echo", {NULL}, 7, "udp" },
71 { "discard", {NULL}, 9, "tcp" },
72 { "discard", {NULL}, 9, "udp" },
73 { "chargen", {NULL}, 19, "tcp" },
74 { "chargen", {NULL}, 19, "udp" },
75 { "systat", {NULL}, 11, "tcp" },
76 { "systat", {NULL}, 11, "udp" },
77 { "daytime", {NULL}, 13, "tcp" },
78 { "daytime", {NULL}, 13, "udp" },
79 { "netstat", {NULL}, 15, "tcp" },
80 { "qotd", {NULL}, 17, "tcp" },
81 { "qotd", {NULL}, 17, "udp" },
82 { NULL, {NULL}, 0, NULL }
83};
84
85// Since table kept in host byte order,
86// return this element to callers
87
88static struct servent ServEntReturn = {0};
89
90// Because CE doesn't have _stricmp - that's why.
91
92static void strcpyLC(char* szDst, const char* szSrc, int cch)
93{
94 int i;
95 char ch;
96 for (i = 0, ch = szSrc[i]; i < cch && ch != 0; ch = szSrc[++i])
97 {
98 szDst[i] = (ch >= 'A' && ch <= 'Z') ? (ch + ('a'-'A')) : ch;
99 } szDst[i] = 0;
100}
101
102
103struct servent * WINSOCKAPI getservbyport(int port, const char * proto)
104{
105
106 port = ntohs((unsigned short)port); // arrives in network byte order
107 struct servent *ps = &RgServEnt[0];
108 BOOL fFoundOnce = FALSE; // flag to short-circuit search through rest of db
109
110 // Make a lowercase version for comparison
111 // truncate to 1 char longer than any value in table
112 char szProtoLC[CCH_MAX_PROTO+2];
113 if (NULL != proto)
114 strcpyLC(szProtoLC, proto, CCH_MAX_PROTO+1);
115
116 while (NULL != ps->s_name)
117 {
118 if (port == ps->s_port)
119 {
120 fFoundOnce = TRUE;
121 if (NULL == proto || !strcmp(szProtoLC, ps->s_proto))
122 {
123 ServEntReturn = *ps;
124 ServEntReturn.s_port = htons(ps->s_port);
125 return &ServEntReturn;
126 }
127 }
128 else if (fFoundOnce)
129 break;
130 ++ps;
131 } return NULL;
132}
133
134
135struct servent * WINSOCKAPI getservbyname(const char * name,
136 const char * proto)
137{
138 struct servent *ps = &RgServEnt[0];
139 BOOL fFoundOnce = FALSE; // flag to short-circuit search through rest of db
140
141 // Make lowercase versions for comparisons
142 // truncate to 1 char longer than any value in table
143 char szNameLC[CCH_MAX_SERV+2];
144 strcpyLC(szNameLC, name, CCH_MAX_SERV+1);
145 char szProtoLC[CCH_MAX_PROTO+2];
146 if (NULL != proto)
147 strcpyLC(szProtoLC, proto, CCH_MAX_PROTO+1);
148
149 while (NULL != ps->s_name)
150 {
151 if (!strcmp(szNameLC, ps->s_name))
152 {
153 fFoundOnce = TRUE;
154 if (NULL == proto || !strcmp(szProtoLC, ps->s_proto))
155 {
156 ServEntReturn = *ps;
157 ServEntReturn.s_port = htons(ps->s_port);
158 return &ServEntReturn;
159 }
160 }
161 else if (fFoundOnce)
162 break;
163 ++ps;
164 } return NULL;
165}
166
167
168struct protoent * WINSOCKAPI getprotobynumber(int proto)
169{
170 struct protoent *pr = &RgProtoEnt[0];
171 while (NULL != pr->p_name)
172 {
173 if (proto == pr->p_proto)
174 return pr;
175 ++pr;
176 } return NULL;
177}
178