4 #pragma comment(lib, "wsock32.lib")
5 #pragma comment(lib, "shell32.lib")
7 char *argv0 = "winplumb";
9 unsigned long parseip(char*, char*);
10 typedef unsigned long ulong;
14 hnputl(void *p, unsigned long v)
26 hnputs(void *p, unsigned short v)
40 return (a[0]<<24)|(a[1]<<16)|(a[2]<<8)|(a[3]<<0);
48 return (a[0]<<8)|(a[1]<<0);
53 main(int argc, char **argv)
55 char *addr, *p, *q, to[4];
57 int port, fd, nfd, one, len, n, tot;
59 struct sockaddr_in sin;
62 if(argc != 1 && argc != 2){
64 fprintf(stderr, "usage: winplumb [tcp!ipaddr!port]\n");
83 if(strcmp(buf, "tcp") != 0)
87 if(strcmp(p, "*") == 0)
92 WSAStartup(MAKEWORD(1, 1), &wasdat);
95 fd = socket(AF_INET, SOCK_STREAM, 0);
98 fprintf(stderr, "socket: %s\n", errbuf);
103 if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one) != 0){
105 fprintf(stderr, "setsockopt nodelay: %s\n", errbuf);
108 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof one) != 0){
110 fprintf(stderr, "setsockopt reuse: %s\n", errbuf);
112 memset(&sin, 0, sizeof sin);
113 sin.sin_family = AF_INET;
114 hnputs(&sin.sin_port, port);
115 hnputl(&sin.sin_addr, ip);
116 if(bind(fd, (struct sockaddr*)&sin, sizeof sin) < 0){
118 fprintf(stderr, "bind: %s\n", errbuf);
122 if(listen(fd, 5) < 0){
124 fprintf(stderr, "listen: %s\n", errbuf);
130 nfd = accept(fd, (struct sockaddr*)&sin, &len);
133 fprintf(stderr, "accept: %s\n", errbuf);
137 while(tot == 0 || buf[tot-1] != '\n'){
138 n = recv(nfd, buf+tot, sizeof buf-tot, 0);
143 if(buf[tot-1] == '\n'){
145 p = strchr(buf, ' ');
148 ShellExecute(0, 0, buf, p, 0, SW_SHOWNORMAL);
155 #define CLASS(p) ((*(unsigned char*)(p))>>6)
159 parseip(char *to, char *from)
166 for(i = 0; i < 4 && *p; i++){
167 to[i] = strtoul(p, &p, 0);
172 case 0: /* class A - 1 byte net */
183 case 2: /* class B - 2 byte net */
201 r = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
202 0, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
203 buf, sizeof(buf), 0);
206 sprintf(buf, "windows error %d", e);
209 for(i = strlen(buf)-1; i>=0 && buf[i] == '\n' || buf[i] == '\r'; i--)
215 extern int main(int, char*[]);
216 static int args(char *argv[], int n, char *p);
219 WinMain(HANDLE hInst, HANDLE hPrev, LPSTR arg, int nshow)
224 /* conservative guess at the number of args */
225 for(argc=5,p=arg; *p; p++)
226 if(*p == ' ' || *p == '\t')
229 argv = malloc(argc*sizeof(char*));
230 argc = args(argv+1, argc, arg);
239 * Break the command line into arguments
240 * The rules for this are not documented but appear to be the following
241 * according to the source for the microsoft C library.
242 * Words are seperated by space or tab
243 * Words containing a space or tab can be quoted using "
244 * 2N backslashes + " ==> N backslashes and end quote
245 * 2N+1 backslashes + " ==> N backslashes + literal "
246 * N backslashes not followed by " ==> N backslashes
249 args(char *argv[], int n, char *p)
252 int i, j, quote, nbs;
254 for(i=0; *p && i<n-1; i++) {
255 while(*p == ' ' || *p == '\t')
260 if(!quote && (*p == ' ' || *p == '\t'))
262 for(nbs=0; *p == '\\'; p++,nbs++)
265 for(j=0; j<(nbs>>1); j++)
277 /* move p up one to avoid pointing to null at end of p2 */