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