+ setsockopt(c->fd,
+ SOL_SOCKET,
+ SO_REUSEADDR,
+ (void *)&so_reuseaddr_option,
+ sizeof(int));
+
+ socket_nonblock(c->fd);
+
+ memset(&c->sin, 0, sizeof(c->sin));
+ c->sin.sin_family = AF_INET;
+
+ if (bindto_name == NULL)
+ c->sin.sin_addr.s_addr = INADDR_ANY;
+ else {
+ hp = gethostbyname(bindto_name);
+ if (hp == NULL) {
+ LOG_ERROR("couldn't resolve bindto address: %s", bindto_name);
+ close_socket(c->fd);
+ free_service(c);
+ return ERROR_FAIL;
+ }
+ memcpy(&c->sin.sin_addr, hp->h_addr_list[0], hp->h_length);
+ }
+ c->sin.sin_port = htons(c->portnumber);
+
+ if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
+ LOG_ERROR("couldn't bind %s to socket: %s", name, strerror(errno));
+ close_socket(c->fd);
+ free_service(c);
+ return ERROR_FAIL;
+ }
+
+#ifndef _WIN32
+ int segsize = 65536;
+ setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
+#endif
+ int window_size = 128 * 1024;
+
+ /* These setsockopt()s must happen before the listen() */
+
+ setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
+ (char *)&window_size, sizeof(window_size));
+ setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
+ (char *)&window_size, sizeof(window_size));
+
+ if (listen(c->fd, 1) == -1) {
+ LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
+ close_socket(c->fd);
+ free_service(c);
+ return ERROR_FAIL;
+ }
+ } else if (c->type == CONNECTION_STDINOUT) {
+ c->fd = fileno(stdin);
+
+#ifdef _WIN32
+ /* for win32 set stdin/stdout to binary mode */
+ if (_setmode(_fileno(stdout), _O_BINARY) < 0)
+ LOG_WARNING("cannot change stdout mode to binary");
+ if (_setmode(_fileno(stdin), _O_BINARY) < 0)
+ LOG_WARNING("cannot change stdin mode to binary");
+ if (_setmode(_fileno(stderr), _O_BINARY) < 0)
+ LOG_WARNING("cannot change stderr mode to binary");
+#else
+ socket_nonblock(c->fd);
+#endif
+ } else if (c->type == CONNECTION_PIPE) {
+#ifdef _WIN32
+ /* we currenty do not support named pipes under win32
+ * so exit openocd for now */
+ LOG_ERROR("Named pipes currently not supported under this os");
+ free_service(c);
+ return ERROR_FAIL;
+#else
+ /* Pipe we're reading from */
+ c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
+ if (c->fd == -1) {
+ LOG_ERROR("could not open %s", c->port);
+ free_service(c);
+ return ERROR_FAIL;
+ }
+#endif