46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
59 const size_t USERLEN = 32;
60 const size_t CREDLEN = 128;
62 const char *MBDynSocketDrivePath =
"/var/mbdyn/mbdyn.sock";
64 SocketDrive::SocketDrive(
unsigned int uL,
const DriveHandler* pDH,
66 integer nd,
const std::vector<doublereal>&
v0)
82 const char *err_msg = strerror(save_errno);
84 silent_cerr(
"SocketDrive(" << GetLabel()
85 <<
"): socket failed "
86 "(" << save_errno <<
": "<< err_msg <<
")"
90 }
else if (sock == -2) {
91 const char *err_msg = strerror(save_errno);
93 silent_cerr(
"SocketDrive(" << GetLabel()
95 "(" << save_errno <<
": "<< err_msg <<
")"
103 SocketDrive::SocketDrive(
unsigned int uL,
const DriveHandler* pDH,
105 integer nd,
const std::vector<doublereal>& v0)
122 const char *err_msg = strerror(save_errno);
124 silent_cerr(
"SocketDrive(" << GetLabel()
125 <<
"): socket failed "
126 "(" << save_errno <<
": "<< err_msg <<
")"
130 }
else if (sock == -2) {
131 const char *err_msg = strerror(save_errno);
133 silent_cerr(
"SocketDrive(" << GetLabel()
135 "(" << save_errno <<
": "<< err_msg <<
")"
144 SocketDrive::Init(
void)
147 int oldflags = fcntl(sock, F_GETFL, 0);
148 if (oldflags == -1) {
149 silent_cerr(
"SocketDrive(" << GetLabel()
150 <<
": unable to get socket flags"
154 oldflags |= O_NONBLOCK;
155 if (fcntl(sock, F_SETFL, oldflags) == -1) {
156 silent_cerr(
"SocketDrive(" << GetLabel()
157 <<
": unable to set socket flags"
162 if (listen(sock, 1) < 0) {
163 silent_cerr(
"SocketDrive(" << GetLabel()
164 <<
"): listen failed" << std::endl);
169 for (
int iCnt = 0; iCnt <= iNumDrives; iCnt++) {
170 pFlags[iCnt] = SocketDrive::DEFAULT;
175 SocketDrive::~SocketDrive(
void)
178 shutdown(sock, SHUT_RDWR );
203 if (fgets(buf, bufsize, fd) == NULL) {
208 if (len > 0 && buf[len-1] ==
'\n') {
210 if (len > 1 && buf[len-2] ==
'\r') {
214 fprintf(stderr,
"buffer overflow\n");
242 if (strncasecmp(buf,
"user:", 5) != 0) {
243 *nextline = (
char *)malloc(
sizeof(
char)*(strlen(buf)+1));
244 if ((*nextline) == NULL) {
247 strcpy(*nextline, buf);
254 while (isspace(*p)) {
257 for (i = 0; i < USERLEN; i++) {
258 if ((user[i] = p[i]) ==
'\0' || isspace(user[i])) {
269 if (strncasecmp(buf,
"password:", 9) != 0) {
270 *nextline = (
char *)malloc(
sizeof(
char)*(strlen(buf)+1));
271 if ((*nextline) == NULL) {
274 strcpy(*nextline, buf);
281 while (isspace(*p)) {
284 for (i = 0; i < CREDLEN; i++) {
285 if ((cred[i] = p[i]) ==
'\0' || isspace(cred[i])) {
298 struct sockaddr_in client_name;
302 for (
integer iCnt = 1; iCnt <= iNumDrives; iCnt++) {
303 if (pFlags[iCnt] & SocketDrive::IMPULSIVE) {
310 char *nextline = NULL;
311 const size_t bufsize =
BUFSIZE;
317 int cur_sock = accept(sock,
318 (
struct sockaddr *)&client_name,
321 if (cur_sock == -1) {
322 int save_errno = errno;
323 if (save_errno != EWOULDBLOCK) {
325 "SocketDrive(" << GetLabel() <<
"): "
327 "(" << save_errno <<
": "
328 << strerror(save_errno) <<
")"
334 silent_cout(
"SocketDrive(" << GetLabel() <<
"): "
335 "connect from " << inet_ntoa(client_name.sin_addr)
336 <<
":" << ntohs(client_name.sin_port) << std::endl);
340 if (dynamic_cast<SASL2_Auth*>(auth)) {
343 "SocketDrive(" << GetLabel() <<
"): "
344 "authentication failed" << std::endl);
351 FILE* fd = fdopen(cur_sock,
"r");
354 char user[USERLEN + 1];
355 char cred[CREDLEN + 1];
359 "SocketDrive(" << GetLabel() <<
"): "
360 "corrupted stream" << std::endl);
365 DEBUGCOUT(
"got auth token: user=\"" << user
366 <<
"\", cred=\"" << cred <<
"\"" << std::endl);
370 "SocketDrive(" << GetLabel() <<
"): "
371 "authentication failed" << std::endl);
383 if (nextline == NULL) {
384 if (
get_line(buf, bufsize, fd) == NULL) {
386 "SocketDrive(" << GetLabel() <<
"): "
387 "corrupted stream" << std::endl);
392 strncpy(buf, nextline, bufsize);
398 if (strncasecmp(nextline,
"label:", 6) != 0) {
399 silent_cerr(
"SocketDrive(" << GetLabel() <<
"): "
400 "missing label" << std::endl);
405 char *p = nextline + 6;
406 while (isspace(p[0])) {
410 if (sscanf(p,
"%d", &label) != 1) {
411 silent_cerr(
"SocketDrive(" << GetLabel() <<
"): "
412 "unable to read label" << std::endl);
417 if (label <= 0 || label > iNumDrives) {
418 silent_cerr(
"SocketDrive(" << GetLabel() <<
"): "
419 "illegal label " << label << std::endl);
425 if (
get_line(buf, bufsize, fd) == NULL) {
427 "SocketDrive(" << GetLabel() <<
"): "
428 "corrupted stream" << std::endl);
435 if (nextline[0] ==
'.') {
440 if (strncasecmp(nextline,
"value:", 6) == 0) {
441 char *p = nextline+6;
442 while (isspace(p[0])) {
446 if (sscanf(p,
"%lf", &value) != 1) {
447 silent_cerr(
"SocketDrive(" << GetLabel() <<
"): "
448 "unable to read value"
455 }
else if (strncasecmp(nextline,
"inc:", 4) == 0) {
456 char *p = nextline+4;
457 while (isspace(p[0])) {
461 if (strncasecmp(p,
"yes", 3) == 0) {
462 pFlags[label] |= SocketDrive::INCREMENTAL;
463 }
else if (strncasecmp(p,
"no", 2) == 0) {
464 pFlags[label] &= !SocketDrive::INCREMENTAL;
466 silent_cerr(
"SocketDrive(" << GetLabel() <<
"): "
468 "\"" << nextline <<
"\" "
476 }
else if (strncasecmp(nextline,
"imp:", 4) == 0) {
477 char *p = nextline+4;
478 while (isspace(p[0])) {
482 if (strncasecmp(p,
"yes", 3) == 0) {
483 pFlags[label] |= SocketDrive::IMPULSIVE;
484 }
else if (strncasecmp(p,
"no", 2) == 0) {
485 pFlags[label] &= !SocketDrive::IMPULSIVE;
487 silent_cerr(
"SocketDrive(" << GetLabel() <<
"): "
488 "\"imp\" line" " in "
489 "\"" << nextline <<
"\""
500 if (pFlags[label] & SocketDrive::INCREMENTAL) {
501 silent_cout(
"SocketDrive(" << GetLabel() <<
"): "
503 <<
" to label " << label
505 pdVal[label] += value;
508 silent_cout(
"SocketDrive(" << GetLabel() <<
"): "
509 "setting label " << label
510 <<
" to value " << value
512 pdVal[label] = value;
521 SocketDrive::Restart(std::ostream& out)
const
523 return out <<
"SocketDrive not implemented yet" << std::endl;
534 unsigned short int port = MBDynSocketDrivePort;
535 const char *path = NULL;
537 std::vector<doublereal>
v0;
540 for (
integer i = 0; i < idrives; i++) {
551 #ifdef IPPORT_USERRESERVED
553 silent_cerr(
"SocketDrive(" << uLabel <<
"): "
554 "cannot listen on port " << port
555 <<
": less than IPPORT_USERRESERVED="
570 silent_cerr(
"need an authentication method "
579 port, pAuth, idrives,
v0));
#define MBDYN_EXCEPT_ARGS
virtual integer GetInt(integer iDefval=0)
#define SAFEDELETEARR(pnt)
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
const DriveHandler * pGetDrvHdl(void) const
int get_auth_token(FILE *fd, char *user, char *cred, char **nextline)
virtual Drive * Read(unsigned uLabel, const DataManager *pDM, MBDynParser &HP)
int mbdyn_make_inet_socket(struct sockaddr_in *name, const char *hostname, unsigned short int port, int dobind, int *perror)
#define SAFENEW(pnt, item)
virtual bool IsKeyWord(const char *sKeyWord)
int get_line(FILE *fin, char *buf, int buf_size)
#define ASSERT(expression)
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
const unsigned long BUFSIZE
int mbdyn_make_named_socket(struct sockaddr_un *name, const char *path, int dobind, int *perror)
#define SAFESTRDUP(pnt, src)
AuthMethod * ReadAuthMethod(const DataManager *, MBDynParser &HP)
#define SAFENEWARR(pnt, item, sz)
static const doublereal a
#define IPPORT_USERRESERVED
static const std::vector< doublereal > v0
static doublereal buf[BUFSIZE]
virtual HighParser::ErrOut GetLineData(void) const
virtual doublereal GetReal(const doublereal &dDefval=0.0)