summaryrefslogtreecommitdiff
path: root/web/src/alpined.d/wpcomm.c
blob: b9f17073ed9b4bc46571e25b5bc8a4d509cbcdbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: wpcomm.c 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $";
#endif

/* ========================================================================
 * Copyright 2006-2007 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */


#include <string.h>
#include <tcl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>


#define	VERSION "0.1"

#define	READBUF		4096
#define RESULT_MAX	16

int	append_rbuf(char *, char *, int);
int	WPSendCmd(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []);


/*
 * WPComm_Init - entry point for Web Alpine servlet communications.
 *
 *    returns: TCL defined values for success or failure
 *
 */

int
Wpcomm_Init(Tcl_Interp *interp)
{
    if(Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 0) == NULL
       && TCL_VERSION[0] == '7'
       && Tcl_PkgRequire(interp, "Tcl", "8.0", 0) == NULL)
      return TCL_ERROR;

    if(Tcl_PkgProvide(interp, "WPComm", VERSION) != TCL_OK)
      return TCL_ERROR;

    Tcl_CreateObjCommand(interp, "WPSend", WPSendCmd,
			 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

    return TCL_OK;
}



/*
 * WPSendCmd - establish communication with the specified device,
 *	       send the given command and return results to caller.
 *
 */
int
WPSendCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
    char     buf[READBUF], lbuf[32], *errbuf = NULL, rbuf[RESULT_MAX], *fname, *cmd;
    int	     s, i, n, b, rs, rv = TCL_ERROR, wlen;
    struct   sockaddr_un name;
    Tcl_Obj *lObj;

    errno = 0;

    if(objc == 3
       && (fname = Tcl_GetStringFromObj(objv[1], NULL))
       && (cmd = Tcl_GetByteArrayFromObj(objv[2], &wlen))){
	if((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1){
	    snprintf(errbuf = buf, sizeof(buf), "WPC: socket: %s", strerror(errno));
	}
	else{
	    name.sun_family = AF_UNIX;
	    strcpy(name.sun_path, fname);

	    if(connect(s, (struct sockaddr *) &name, sizeof(name)) == -1){
		if(errno == ECONNREFUSED || errno == ENOENT)
		  snprintf(errbuf = buf, sizeof(buf), "WPC: Inactive session");
		else
		  snprintf(errbuf = buf, sizeof(buf), "WPC: connect: %s", strerror(errno));
	    }
	    else if((n = wlen) != 0){
		if(n < 0x7fffffff){
		    snprintf(lbuf, sizeof(lbuf), "%d\n", n);
		    i = strlen(lbuf);
		    if(write(s, lbuf, i) == i){
			for(i = 0; n; n = n - i)
			  if((i = write(s, cmd + i, n)) == -1){
			      snprintf(errbuf = buf, sizeof(buf), "WPC: write: %s", strerror(errno));
			      break;
			  }
		    }
		    else
		      snprintf(errbuf = buf, sizeof(buf), "Can't write command length.");
		}
		else
		  snprintf(errbuf = buf, sizeof(buf), "Command too long.");

		rbuf[0] = '\0';
		rs = 0;
		lObj = NULL;
		while((n = read(s, buf, READBUF)) > 0)
		  if(!errbuf){
		      for(i = b = 0; i < n; i++)
			if(buf[i] == '\n'){
			    if(rs){
				Tcl_AppendToObj(Tcl_GetObjResult(interp), &buf[b], i - b);
				Tcl_AppendToObj(Tcl_GetObjResult(interp), " ", 1);
			    }
			    else{
				rs = 1;
				if(append_rbuf(rbuf, &buf[b], i - b) < 0)
				  snprintf(errbuf = buf, sizeof(buf), "WPC: Response Code Overrun");
				else if(!strcasecmp(rbuf,"OK"))
				  rv = TCL_OK;
				else if(!strcasecmp(rbuf,"ERROR"))
				  rv = TCL_ERROR;
				else if(!strcasecmp(rbuf,"BREAK"))
				  rv = TCL_BREAK;
				else if(!strcasecmp(rbuf,"RETURN"))
				  rv = TCL_RETURN;
				else
				  snprintf(errbuf = buf, sizeof(buf), "WPC: Unexpected response: %s", rbuf);
			    }

			    b = i + 1;
			}

		      if(i - b > 0){
			  if(rs)
			    Tcl_AppendToObj(Tcl_GetObjResult(interp), &buf[b], i - b);
			  else if(append_rbuf(rbuf, &buf[b], i - b) < 0)
			    snprintf(errbuf = buf, sizeof(buf), "WPC: Response Code Overrun");
		      }
		  }

		if(!errbuf){
		    if(n < 0){
			snprintf(errbuf = buf, sizeof(buf), "WPC: read: %s", strerror(errno));
			rv = TCL_ERROR;
		    }
		    else if(!rs){
			if(n == 0)
			  snprintf(errbuf = buf, sizeof(buf), "WPC: Server connection closed (%d)", errno);
			else
			  snprintf(errbuf = buf, sizeof(buf), "WPC: Invalid Response to \"%.*s\" (len %d) (%d): %.*s", 12, cmd, wlen, errno, RESULT_MAX, rbuf);

			rv = TCL_ERROR;
		    }
		    else if(rv == TCL_ERROR){
			char *s = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), NULL);
			if(!(s && *s))
			  snprintf(errbuf = buf, sizeof(buf), "WPC: Empty ERROR Response");
		    }
		}
	    }

	    close(s);
	}
    }
    else
      snprintf(errbuf = buf, sizeof(buf), "Usage: %s path cmd", Tcl_GetStringFromObj(objv[0], NULL));

    if(errbuf)
      Tcl_SetResult(interp, errbuf, TCL_VOLATILE);

    return(rv);
}


int
append_rbuf(char *rbuf, char *b, int l)
{
    int i;

    if(l + (i = strlen(rbuf)) > RESULT_MAX)
      return(-1);

    rbuf += i;

    while(l--)
      *rbuf++ = *b++;

    *rbuf = '\0';
    return(0);
}