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