コード例 #1
0
ファイル: tns.c プロジェクト: ClearwaterCore/clearwater-monit
void check_tns(Socket_T socket) {

        unsigned char  buf[STRLEN];

        unsigned char requestPing[] = {
                0x00, 0x57,                           /** Packet Length */
                0x00, 0x00,                           /** Packet Checksum */
                0x01,                                 /** Packet Type: CONNECT */
                0x00,                                 /** Reserved */
                0x00, 0x00,                           /** Header Checksum */
                0x01, 0x36,                           /** Version */
                0x01, 0x2c,                           /** Compatible */
                0x00, 0x00,                           /** Service Options */
                0x08, 0x00,                           /** Session Data Unit Size */
                0x7f, 0xff,                           /** Maximum Transmission Data Unit Size */
                0xa3, 0x0a,                           /** NT Protocol Characteristics */
                0x00, 0x00,                           /** Line Turnaround Value */
                0x01, 0x00,                           /** Value of 1 in Hardware */
                0x00, 0x1d,                           /** Length of Connect Data */
                0x00, 0x3a,                           /** Offset of Connect Data */
                0x00, 0x00, 0x00, 0x00,               /** Maximum Receivable Connect Data */
                0x00,                                 /** Connect flags 0 */
                0x00,                                 /** Connect flags 1 */
                0x00, 0x00, 0x00, 0x00,               /** Trace Cross Facility Item 1 */
                0x00, 0x00, 0x00, 0x00,               /** Trace Cross Facility Item 2 */
                0x00, 0x00, 0x0b, 0x1c,               /** Trace Unique Connection ID */
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x28, 0x43, 0x4f, 0x4e,               /** Connect Data */
                0x4e, 0x45, 0x43, 0x54,               /** (CONNECT_DATA=(COMMAND=ping)) */
                0x5f, 0x44, 0x41, 0x54,
                0x41, 0x3d, 0x28, 0x43,
                0x4f, 0x4d, 0x4d, 0x41,
                0x4e, 0x44, 0x3d, 0x70,
                0x69, 0x6e, 0x67, 0x29,
                0x29
        };

        ASSERT(socket);

        if (Socket_write(socket, (unsigned char *)requestPing, sizeof(requestPing)) < 0)
                THROW(IOException, "TNS: error sending ping -- %s", STRERROR);

        /* read just first few bytes which contains enough information */
        if (Socket_read(socket, (unsigned char *)buf, 5) < 5)
                THROW(IOException, "TNS: error receiving ping response -- %s", STRERROR);

        /* compare packet type */
        if (buf[4] != TNS_TYPE_REFUSED)
                THROW(IOException, "TNS: invalid ping response");
}
コード例 #2
0
// Response handler
static void _response(mysql_t *mysql) {
        memset(&mysql->response, 0, sizeof(mysql_response_t));
        mysql->response.cursor = mysql->response.buf;
        mysql->response.limit = mysql->response.buf + sizeof(mysql->response.buf);
        // Read the packet length
        if (Socket_read(mysql->socket, mysql->response.cursor, 4) < 4)
                THROW(IOException, "Error receiving server response -- %s", STRERROR);
        mysql->response.len = _getUInt3(&mysql->response);
        mysql->response.seq = _getUInt1(&mysql->response);
        if (mysql->state == MySQL_Init) {
                if (! mysql->response.len || mysql->response.len > STRLEN)
                        THROW(IOException, "Invalid handshake packet length -- not MySQL protocol");
                if (mysql->response.seq != 0)
                        THROW(IOException, "Invalid handshake packet sequence id -- not MySQL protocol");
        }
        mysql->response.len = mysql->response.len > STRLEN ? STRLEN : mysql->response.len; // Adjust packet length for this buffer
        // Read payload
        if (Socket_read(mysql->socket, mysql->response.cursor, mysql->response.len) != mysql->response.len)
                THROW(IOException, "Error receiving server response -- %s", STRERROR);
        // Packet type router
        mysql->response.header = _getUInt1(&mysql->response);
        switch (mysql->response.header) {
                case MYSQL_OK:
                        _responseOk(mysql);
                        break;
                case MYSQL_EOF:
                        _responseEof(mysql);
                        break;
                case MYSQL_ERROR:
                        _responseError(mysql);
                        break;
                default:
                        _responseHandshake(mysql);
                        break;
        }
}
コード例 #3
0
ファイル: byte_stream.cpp プロジェクト: fagoth/libiec61850_vs
int
ByteStream_readUint8(ByteStream self, uint8_t* byte)
{
    int bytes_read;
    uint64_t start = Hal_getTimeInMs();

    do {
        bytes_read = Socket_read(self->socket, byte, 1);
    } while ((bytes_read == 0) && ((Hal_getTimeInMs() - start) < CONFIG_TCP_READ_TIMEOUT_MS));

    if (bytes_read != 1) {
        return -1;
    }

    return 1;
}
コード例 #4
0
ファイル: byte_stream.cpp プロジェクト: fagoth/libiec61850_vs
int
ByteStream_readUint16(ByteStream self, uint16_t* value)
{
    uint8_t byte[2];
    int bytes_read;

    uint64_t start = Hal_getTimeInMs();

    do {
        bytes_read = Socket_read(self->socket, byte, 2);
    } while ((bytes_read == 0)
            && ((Hal_getTimeInMs() - start) < CONFIG_TCP_READ_TIMEOUT_MS));

    if (bytes_read != 2) {
        return -1;
    }

    *value = (byte[0] * 0x100) + byte[1];
    return 2;
}
コード例 #5
0
ファイル: byte_stream.cpp プロジェクト: fagoth/libiec61850_vs
int
ByteStream_skipBytes(ByteStream self, int number)
{
    int c = 0;
    uint8_t byte;

    uint64_t start = Hal_getTimeInMs();

    do {
        int readBytes = Socket_read(self->socket, &byte, 1);

        if (readBytes < 0)
            return -1;
        else
            c = c + readBytes;

    } while ((c < number)
            && ((Hal_getTimeInMs() - start) < CONFIG_TCP_READ_TIMEOUT_MS));

    return c;
}
コード例 #6
0
ファイル: byte_stream.cpp プロジェクト: fagoth/libiec61850_vs
int
ByteStream_readOctets(ByteStream self, uint8_t* buffer, int size)
{
    int readBytes = 0;
    int remainingSize = size;

    uint64_t start = Hal_getTimeInMs();

    do {
        int chunkSize = Socket_read(self->socket, buffer + readBytes, remainingSize);
        if (chunkSize < 0)
            return -1;
        else
        {
            readBytes += chunkSize;
            remainingSize = size - readBytes;
        }
    } while ((readBytes < size)
            && ((Hal_getTimeInMs() - start) < CONFIG_TCP_READ_TIMEOUT_MS));

    return readBytes;
}
コード例 #7
0
ファイル: frame.c プロジェクト: encryptedmessaging/marp
/**
 * int Frame_listen(Frame_T, Socket_T)
 * Block until a new frame is received via the socket over the network (or timeout is reached).
 * @param dest: Frame to fill, all contents will be overwritten
 * @param socket: Where to listen for data. Must not be NULL.
 * @param timeout: How long to wait, set to 0 to disable.
 * @return: bytes written on Success, negative on Failure or Timeout
 **/
int Frame_listen(Frame_T dest, Socket_T socket, int timeout) {
  int error;
  uint8_t *buf, *payload;

  assert(dest != NULL);
  assert(socket != NULL);

  if (timeout < 0) return timeout;

  if (dest->payload) free(dest->payload);
  buf = calloc(FRAME_MAX, sizeof(uint8_t));
  if (buf == NULL) {
    return -1;
}

  error = Socket_read(socket, (void*)buf, FRAME_MAX, timeout);
  if (error < 0) {
    free(buf);
    return error;
  } else if (error < HEADER) {
    fprintf(stderr, "%s: Received too small frame from socket.", programName);
  }

  dest->payload = calloc(error - HEADER, sizeof(uint8_t));
  if (dest->payload == NULL) {
    free(buf);
    return -1;
  }
  
  /* Fill Frame */
  memcpy(&(dest->sHeader), buf, HEADER);
  payload = buf + HEADER;
  memcpy(dest->payload, payload, error - HEADER);

  free(buf);

  return error;
} /* End Frame_listen() */
コード例 #8
0
ファイル: generic.c プロジェクト: lalitadeviak/monit
/**
 *  Generic service test.
 *
 *  @file
 */
void check_generic(Socket_T socket) {
        ASSERT(socket);

        Generic_T g = NULL;
        if (Socket_getPort(socket))
                g = ((Port_T)(Socket_getPort(socket)))->parameters.generic.sendexpect;

        char *buf = CALLOC(sizeof(char), Run.limits.sendExpectBuffer + 1);

        while (g != NULL) {

                if (g->send != NULL) {
                        /* Unescape any \0x00 escaped chars in g's send string to allow sending a string containing \0 bytes also */
                        char *X = Str_dup(g->send);
                        int l = Util_handle0Escapes(X);

                        if (Socket_write(socket, X, l) < 0) {
                                FREE(X);
                                FREE(buf);
                                THROW(IOException, "GENERIC: error sending data -- %s", STRERROR);
                        } else {
                                DEBUG("GENERIC: successfully sent: '%s'\n", g->send);
                        }
                        FREE(X);
                } else if (g->expect != NULL) {
                        /* Since the protocol is unknown we need to wait on EOF. To avoid waiting
                         timeout seconds on EOF we first read one byte to fill the socket's read
                         buffer and then set a low timeout on next read which reads remaining bytes
                         as well as wait on EOF */
                        int first_byte = Socket_readByte(socket);
                        if (first_byte < 0) {
                                FREE(buf);
                                THROW(IOException, "GENERIC: error receiving data -- %s", STRERROR);
                        }
                        *buf = first_byte;

                        int timeout = Socket_getTimeout(socket);
                        Socket_setTimeout(socket, 200);
                        int n = Socket_read(socket, buf + 1, Run.limits.sendExpectBuffer - 1) + 1;
                        buf[n] = 0;
                        if (n > 0)
                                _escapeZeroInExpectBuffer(buf, n);
                        Socket_setTimeout(socket, timeout); // Reset back original timeout for next send/expect
#ifdef HAVE_REGEX_H
                        int regex_return = regexec(g->expect, buf, 0, NULL, 0);
                        if (regex_return != 0) {
                                char e[STRLEN];
                                regerror(regex_return, g->expect, e, STRLEN);
                                FREE(buf);
                                THROW(IOException, "GENERIC: received unexpected data -- %s", e);
                        } else {
                                DEBUG("GENERIC: successfully received: '%s'\n", Str_trunc(buf, STRLEN - 4));
                        }

#else
                        /* w/o regex support */

                        if (strncmp(buf, g->expect, strlen(g->expect)) != 0) {
                                FREE(buf);
                                THROW(IOException, "GENERIC: received unexpected data");
                        } else {
                                DEBUG("GENERIC: successfully received: '%s'\n", Str_trunc(buf, STRLEN - 4));
                        }

#endif

                } else {
                        /* This should not happen */
                        FREE(buf);
                        THROW(IOException, "GENERIC: unexpected strangeness");
                }
                g = g->next;
        }
        FREE(buf);
}
コード例 #9
0
/**
 *  PostgreSQL test.
 *
 *  @file
 */
void check_pgsql(Socket_T socket) {

        unsigned char buf[STRLEN];

        unsigned char requestLogin[33] = {
                0x00,                              /** Length */
                0x00,
                0x00,
                0x21,

                0x00,                              /** ProtoVer 3.0 */
                0x03,
                0x00,
                0x00,

                0x75, 0x73, 0x65, 0x72, 0x00,      /** user */
                0x72, 0x6f, 0x6f, 0x74, 0x00,      /** root */

                0x64, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x00,  /** database */
                0x72, 0x6f, 0x6f, 0x74, 0x00,                          /** root */

                0x00
        };

        /** Doing this is too suspicious maybe.
         * Type Q, Length 19 and QUERY select 1 as a; */
        /**
         unsigned char requestQuery[20] = {
         0x51,

         0x00,
         0x00,
         0x00,
         0x13,

         0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x20,
         0x31, 0x20, 0x61, 0x73, 0x20, 0x61, 0x3b,

         0x00
         };
         */

        unsigned char requestTerm[5] = {
                0x58,                              /** Type X */

                0x00,                              /** Length */
                0x00,
                0x00,
                0x04
        };

        unsigned char responseAuthOk[9] = {
                0x52,                              /** Type R */

                0x00,                              /** Length */
                0x00,
                0x00,
                0x08,

                0x00,                              /** OK code 0 */
                0x00,
                0x00,
                0x00
        };

        ASSERT(socket);

        if (Socket_write(socket, (unsigned char *)requestLogin, sizeof(requestLogin)) <= 0)
                THROW(IOException, "PGSQL: error sending data -- %s", STRERROR);

        /** Nine-byte is enough to hold Auth-Ok */
        if (Socket_read(socket, buf, 9) <= 0)
                THROW(IOException, "PGSQL: error receiving data -- %s", STRERROR);

        /** If server insists on auth error it is working anyway */
        if (*buf == 'E')
                return;

        /** Successful connection */
        if (! memcmp((unsigned char *)buf, (unsigned char *)responseAuthOk, 9)) {
                /** This is where suspicious people can do SELECT query that I dont */
                Socket_write(socket, (unsigned char *)requestTerm, sizeof(requestTerm));
                return;
        }

        /** The last possibility must be that server is demanding password */
        if (*buf == 'R')
                return;

        THROW(IOException, "PGSQL: unknown error");
}
コード例 #10
0
ファイル: cotp.c プロジェクト: nono19710321/libiec61850
TpktState
CotpConnection_readToTpktBuffer(CotpConnection* self)
{
    uint8_t* buffer = self->readBuffer->buffer;
    int bufferSize = self->readBuffer->maxSize;
    int bufPos = self->readBuffer->size;

    assert (bufferSize > 4);

    int readBytes;

    if (bufPos < 4) {

        readBytes = Socket_read(self->socket, buffer + bufPos, 4 - bufPos);

        if (readBytes < 0)
            goto exit_closed;

        if (DEBUG_COTP) {
            if (readBytes > 0)
                printf("TPKT: read %i bytes from socket\n", readBytes);
        }

        bufPos += readBytes;

        if (bufPos == 4) {
            if ((buffer[0] == 3) && (buffer[1] == 0)) {
                self->packetSize = (buffer[2] * 0x100) + buffer[3];

                if (DEBUG_COTP)
                    printf("TPKT: header complete (msg size = %i)\n", self->packetSize);

                if (self->packetSize > bufferSize) {
                    if (DEBUG_COTP) printf("TPKT: packet too large\n");
                    goto exit_error;
                }
            }
            else {
                if (DEBUG_COTP) printf("TPKT: failed to decode TPKT header.\n");
                goto exit_error;
            }
        }
        else
            goto exit_waiting;
    }

    readBytes = Socket_read(self->socket, buffer + bufPos, self->packetSize - bufPos);

    if (readBytes < 0)
        goto exit_closed;

    bufPos += readBytes;

    if (bufPos < self->packetSize)
       goto exit_waiting;

    if (DEBUG_COTP) printf("TPKT: message complete (size = %i)\n", self->packetSize);

    self->readBuffer->size = bufPos;
    return TPKT_PACKET_COMPLETE;

exit_closed:
    if (DEBUG_COTP) printf("TPKT: socket closed or socket error\n");
    return TPKT_ERROR;

exit_error:
    if (DEBUG_COTP) printf("TPKT: Error parsing message\n");
    return TPKT_ERROR;

exit_waiting:

    if (DEBUG_COTP)
        if (bufPos != 0)
            printf("TPKT: waiting (read %i of %i)\n", bufPos, self->packetSize);

    self->readBuffer->size = bufPos;
    return TPKT_WAITING;
}
コード例 #11
0
/**
 *  Simple LDAPv3 protocol test.
 *
 *  Try anonymous bind to the server.
 *
 *  BindRequest based on RFC2251. Request and response are ASN.1
 *  BER encoded strings. To make the test as simple as possible
 *  we work with BER encoded data.
 *
 *  The test checks only if the bind was successfull - in the
 *  case of failure it don't provide any erroneous message
 *  analysis.
 *
 *  @file
 */
void check_ldap3(Socket_T socket) {

        unsigned char buf[STRLEN];

        unsigned char request[14] = {
                0x30,                         /** Universal Sequence TAG */
                0x0c,               /** Length of the packet's data part */

                0x02,                          /** Universal Integer TAG */
                0x01,                                    /** Integer length */
                0x00,                                         /** MessageID */

                0x60,                    /** Application BindRequest TAG */
                0x07,                        /** Length of the data part */

                0x02,                         /** Universal Integer TAG */
                0x01,                                 /** Integer length */
                0x03,                               /** Protocol version */

                0x04,                    /** Universal Octet string TAG */
                0x00,                                  /** Octet string length */
                /* NULL */                                 /** Anonymous BindDN */

                0x80,                /** Context specific SimpleAuth TAG */
                0x00                    /** SimpleAuth (octet string) length */
                /* NULL */                            /** Anonymous Credentials */
        };

        unsigned char response[14] = {
                0x30,                         /** Universal Sequence TAG */
                0x0c,               /** Length of the packet's data part */

                0x02,                          /** Universal Integer TAG */
                0x01,                                    /** Integer length */
                0x00,                                         /** MessageID */

                0x61,                   /** Application BindResponse TAG */
                0x07,                        /** Length of the data part */

                0x0a,                      /** Universal Enumerated TAG */
                0x01,                              /** Enumerated length */
                0x00,                                        /** Success */

                0x04,                    /** Universal Octet string TAG */
                0x00,                                  /** Octet string length */
                /* NULL */                                        /** MatchedDN */

                0x04,                    /** Universal Octet string TAG */
                0x00                                  /** Octet string length */
                /* NULL */                                     /** ErrorMessage */
        };

        unsigned char unbind[7] = {
                0x30,                         /** Universal Sequence TAG */
                0x05,               /** Length of the packet's data part */

                0x02,                          /** Universal Integer TAG */
                0x01,                                    /** Integer length */
                0x01,                                         /** MessageID */

                0x42,                  /** Application UnbindRequest TAG */
                0x00                        /** Length of the data part */
                /* NULL */

        };

        ASSERT(socket);


        if (Socket_write(socket, (unsigned char *)request, sizeof(request)) < 0)
                THROW(IOException, "LDAP: error sending data -- %s", STRERROR);

        if (Socket_read(socket, (unsigned char *)buf, sizeof(response)) <= 0)
                THROW(IOException, "LDAP: error receiving data -- %s", STRERROR);

        if (memcmp((unsigned char *)buf, (unsigned char *)response, sizeof(response)))
                THROW(IOException, "LDAP: anonymous bind failed");

        if (Socket_write(socket, (unsigned char *)unbind, sizeof(unbind)) < 0)
                THROW(IOException, "LDAP: error sending data -- %s", STRERROR);
}