/*------------------------------------------------------------------------------------------------------------------ -- FUNCTION: socket_event -- -- DATE: Febuary 6 2014 -- REVISIONS: none -- -- DESIGNER: Ramzi Chennafi -- PROGRAMMER: Ramzi Chennafi -- -- INTERFACE: void socket_event(HWND hwnd, WPARAM wParam, LPARAM lParam), where wParam is the socket and lParam is the error message. -- The hwnd is the parent HWND. -- -- RETURNS: void -- -- NOTES: -- Asynchronously responds to socket accept and read events on the server side of the program. Will reallocate the -- SocketInfo structure whenever a transfer ends. ----------------------------------------------------------------------------------------------------------------------*/ void socket_event(HWND hwnd, WPARAM wParam, LPARAM lParam){ SETTINGS * st = (SETTINGS*)GetClassLongPtr(hwnd, 0); char msg[MAX_SIZE]; if (WSAGETSELECTERROR(lParam)) { sprintf_s(msg, "WSAGETSELECTERROR %d, connection failed.\n", WSAGETSELECTERROR(lParam)); activity(msg, EB_STATUSBOX); closesocket(st->server_socket); } else { switch (WSAGETSELECTEVENT(lParam)) { case FD_ACCEPT: accept_data(hwnd, wParam); break; case FD_READ: if (st->protocol == TCP){ if (read_tcp(hwnd, st->server_socket) >= 1){ if ((SocketInfo = (LPSOCKET_INFORMATION)GlobalAlloc(GPTR, sizeof(SOCKET_INFORMATION))) == NULL){ activity("GlobalAlloc() failed with error\n", EB_STATUSBOX); } SocketInfo->header_received = 0; // transmission ends, program waits for a new header } } else{ if (read_udp(hwnd, st->server_socket) >= 1){ if ((SocketInfo = (LPSOCKET_INFORMATION)GlobalAlloc(GPTR, sizeof(SOCKET_INFORMATION))) == NULL){ activity("GlobalAlloc() failed with error\n", EB_STATUSBOX); } SocketInfo->header_received = 0; } } } } }
int main(int argc, char **argv) { int fd; //time_t ticks; //char buff[MAXLINE]; struct addrinfo *ai; int socktype; char port[5]; /*char *filename; out = FALSE; verbose = 0; */ char *server_name = argv[1]; time_record_tail = &time_record_head; /* tail points to the first element, i.e. the head of the list */ seqlist_tail = &seqlist_head; seqlist_tail->next = NULL; /* default values for port and socktype */ strncpy(port, "5001", 4); socktype = SOCK_STREAM; if (argc < 2) { usage(); } argc--; argv++; /* skip program name */ argc--; argv++; /* skip source address or name */ while( argc > 0 && argv[0][0] == '-' ) { switch (argv[0][1]) { case 'u': socktype = SOCK_DGRAM; break; case 't': socktype = SOCK_STREAM; break; case 'p': strncpy(port, &argv[0][2], 5); break; case 'l': //strncpy(port, &argv[0][2], 5); buflen = atoi(&argv[0][2]); break; /* case 'v': verbose++; break; case 'o': // next string specifies a filename strncpy(filename,&argv[1][0] , MAXFILENAME); argv++; argc--; // skip filename out = TRUE; break;*/ default: usage(); }; argv++; argc--; } /* open output file if specified */ /* if(out) { output = fopen(filename, "w"); if(!output) { fprintf(stderr, "Failed opening file for output %s: ", filename); perror(""); } }*/ ai = getaddrlist(server_name, port, AI_PASSIVE, AF_UNSPEC, socktype); fd = init_sock(ai); check("init_socket", fd); if(socktype == SOCK_STREAM) { printf("Waiting for TCP connection on port %s\n", port); read_tcp(fd); } else if(socktype == SOCK_DGRAM) { printf("Waiting for UDP connection on port %s\n", port); read_udp(fd); } /* if(output) { if(fclose(output)) { fprintf(stderr, "Failed closing file %s\n", filename); perror(""); } }*/ if(time_record_head.next != NULL) free_time_record(time_record_head.next); if(seqlist_head.next != NULL) free_seqlist(seqlist_head.next); return 0; }
int probe_main(probe_ctx *ctx, void *arg) { SEXP_t *object; int err; llist ll; object = probe_ctx_getobject(ctx); req.protocol_ent = probe_obj_getent(object, "protocol", 1); if (req.protocol_ent == NULL) { err = PROBE_ENOVAL; goto cleanup; } req.local_address_ent = probe_obj_getent(object, "local_address", 1); if (req.local_address_ent == NULL) { err = PROBE_ENOVAL; goto cleanup; } req.local_port_ent = probe_obj_getent(object, "local_port", 1); if (req.local_port_ent == NULL) { err = PROBE_ENOVAL; goto cleanup; } // Now start collecting the info list_create(&ll); if (collect_process_info(&ll)) { SEXP_t *msg; msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "Permission error."); probe_cobj_add_msg(probe_ctx_getresult(ctx), msg); SEXP_free(msg); probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); err = 0; goto cleanup; } // Now we check the tcp socket list... read_tcp("/proc/net/tcp", "tcp", &ll, ctx); read_tcp("/proc/net/tcp6", "tcp", &ll, ctx); // Next udp sockets... read_udp("/proc/net/udp", "udp", &ll, ctx); read_udp("/proc/net/udp6", "udp", &ll, ctx); // Next, raw sockets...not exactly part of standard yet. They // can be used to send datagrams, so we will pretend they are udp read_raw("/proc/net/raw", "udp", &ll, ctx); read_raw("/proc/net/raw6", "udp", &ll, ctx); list_clear(&ll); err = 0; cleanup: SEXP_vfree(req.protocol_ent, req.local_address_ent, req.local_port_ent, NULL); return err; }