text:
macro_rules! patchable_struct_diff {
    ($($field:ident),* $(,)?) => {
        #[allow(unused_variables)]
        fn diff(&self, other: &Self, differ: &mut Differ) {
            $(
                differ.field(stringify!($field), &self.$field, &other.$field);
            )*
        }
    };
}

/// Generate the `apply_add` method for a `struct`
macro_rules! patchable_struct_apply_add {
    ($($field:ident),* $(,)?) => {
        #[allow(unused_variables)]
        fn apply_add(&mut self, address: &mut Address, value: &Value) -> Result<()> {
            if let Some(Slot::Name(name)) = address.pop_front() {
                match name.as_str() {
                    $(
                        stringify!($field) => self.$field.apply_add(address, value),
                    )*
                    _ => bail!(invalid_slot_name::<Self>(&name)),
                }
            } else {
                bail!(invalid_address::<Self>("first slot should be a name"))
            }
        }
    };
}

/// Generate the `apply_remove` method for a `struct`
macro_rules! patchable_struct_apply_remove {
    ($($field:ident),* $(,)?) => {
        #[allow(unused_variables)]
        fn apply_remove(&mut self, address: &mut Address, items: usize) -> Result<()> {
            if let Some(Slot::Name(name)) = address.pop_front() {
                match name.as_str() {
                    $(
                        stringify!($field) => self.$field.apply_remove(address, items),
                    )*
                    _ => bail!(invalid_slot_name::<Self>(&name)),
                }
            } else {
                bail!(invalid_address::<Self>("first slot should be a name"))
            }
        }
    };
}

/// Generate the `apply_replace` method for a `struct`
macro_rules! patchable_struct_apply_replace {
    ($($field:ident),* $(,)?) => {
        #[allow(unused_variables)]
        fn apply_replace(&mut self, address: &mut Address, items: usize, value: &Value) -> Result<()> {
            if let Some(Slot::Name(name)) = address.pop_front() {
                match name.as_str() {
                    $(
                        stringify!($field) => self.$field.apply_replace(address, items, value),
                    )*
                    _ => bail!(invalid_slot_name::<Self>(&name)),
                }
            } else {
                bail!(invalid_address::<Self>("first slot should be a name"))
            }
        }
    };
}

/// Generate the `apply_move` method for a `struct`
macro_rules! patchable_struct_apply_move {
    ($($field:ident),* $(,)?) => {
        #[allow(unused_variables)]
        fn apply_move(&mut self, from: &mut Address, items: usize, to: &mut Address) -> Result<()> {
            if let (Some(Slot::Name(name)), Some(Slot::Name(_name_again))) = (from.pop_front(), to.pop_front()) {
                match name.as_str() {
                    $(
                        stringify!($field) => self.$field.apply_move(from, items, to),
                    )*
                    _ => bail!(invalid_slot_name::<Self>(&name)),
                }
            } else {
                bail!(invalid_address::<Self>("first slot should be a name"))
            }
        }
    };
}

/// Generate the `apply_transform` method for a `struct`
macro_rules! patchable_struct_apply_transform {
    ($($field:ident),* $(,)?) => {
        #[allow(unused_variables)]
        fn apply_transform(&mut self, address: &mut Address, from: &str, to: &str) -> Result<()> {
            if let Some(Slot::Name(name)) = address.pop_front() {
                match name.as_str() {
                    $(
                        stringify!($field) => self.$field.apply_transform(address, from, to),
                    )*
                    _ => bail!(invalid_slot_name::<Self>(&name)),
                }
            } else {
                bail!(invalid_address::<Self>("first slot should be a name"))
            }
        }
    };
}

/// Generate a `impl Patchable` for a `struct`, passing
/// a list of fields for comparison, diffing, and applying ops.
macro_rules! patchable_struct {
    ($type:ty $(,$field:ident)* $(,)?) => {
        impl Patchable for $type {
            patchable_struct_diff!($($field,)*);
            patchable_struct_apply_add!($($field,)*);
            patchable_struct_apply_remove!($($field,)*);
            patchable_struct_apply_replace!($($field,)*);
            patchable_struct_apply_move!($($field,)*);
            patchable_struct_apply_transform!($($field,)*);
        }
    };
}

/// Generate a `impl Patchable` for a `struct` that should
/// be replaced as a whole, rather than patching its fields separately.
///
/// Use this, for example, for `struct`s whose HTML encoding is more complicated
/// than a set of elements for each field.
///
/// The `diff` method returns a `Replace` operation and it only implements
/// `apply_replace`.
macro_rules! replaceable_struct {
    ($type:ty $(,$field:ident)* $(,)?) => {
        impl Patchable for $type {
            fn diff(&self, other: &Self, differ: &mut Differ) {
                if self != other {
                    differ.replace(other)
                }
            }

            patchable_struct_apply_replace!($($field,)*);
        }
    };
}


text:
<?php

namespace Tmdb\Api;

/**
 * Class Find
 * @package Tmdb\Api
 * @see http://docs.themoviedb.apiary.io/#find
 */
class Find extends AbstractApi
{
    /**
     * The find method makes it easy to search for objects in our database by an external id.
     *
     * For instance, an IMDB ID. This will search all objects (movies, TV shows and people)
     * and return the results in a single response.
     *
     * TV season and TV episode searches will be supported shortly.
     * The supported external sources for each object are as follows:
     *
     * Movies: imdb_id
     * People: imdb_id, freebase_mid, freebase_id, tvrage_id
     * TV Series: imdb_id, freebase_mid, freebase_id, tvdb_id, tvrage_id
     *
     * @param  string $id
     * @param  array  $parameters
     * @param  array  $headers
     * @return mixed
     */
    public function find($id, array $parameters = array(), array $headers = array())
    {
        return $this->get(
            sprintf('find/%s', $id),
            $parameters,
            $headers
        );
    }
}

redpajama_set_name: RedPajamaGithub


text:
package com.reactnativenavigation.parse;

import com.reactnativenavigation.parse.params.Colour;
import com.reactnativenavigation.parse.params.NullColor;
import com.reactnativenavigation.parse.params.NullNumber;
import com.reactnativenavigation.parse.params.Number;
import com.reactnativenavigation.parse.parsers.ColorParser;
import com.reactnativenavigation.parse.parsers.NumberParser;

import org.json.JSONObject;

public class LayoutOptions {
    public static LayoutOptions parse(JSONObject json) {
        LayoutOptions result = new LayoutOptions();
        if (json == null) return result;

        result.backgroundColor = ColorParser.parse(json, "backgroundColor");
        result.componentBackgroundColor = ColorParser.parse(json, "componentBackgroundColor");
        result.topMargin = NumberParser.parse(json, "topMargin");
        result.orientation = OrientationOptions.parse(json);
        result.direction = LayoutDirection.fromString(json.optString("direction", ""));

        return result;
    }

    public Colour backgroundColor = new NullColor();
    public Colour componentBackgroundColor = new NullColor();
    public Number topMargin = new NullNumber();
    public OrientationOptions orientation = new OrientationOptions();
    public LayoutDirection direction = LayoutDirection.DEFAULT;

    public void mergeWith(LayoutOptions other) {
        if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor;
        if (other.componentBackgroundColor.hasValue()) componentBackgroundColor = other.componentBackgroundColor;
        if (other.topMargin.hasValue()) topMargin = other.topMargin;
        if (other.orientation.hasValue()) orientation = other.orientation;
        if (other.direction.hasValue()) direction = other.direction;
    }

    public void mergeWithDefault(LayoutOptions defaultOptions) {
        if (!backgroundColor.hasValue()) backgroundColor = defaultOptions.backgroundColor;
        if (!componentBackgroundColor.hasValue()) componentBackgroundColor = defaultOptions.componentBackgroundColor;
        if (!topMargin.hasValue()) topMargin = defaultOptions.topMargin;
        if (!orientation.hasValue()) orientation = defaultOptions.orientation;
        if (!direction.hasValue()) direction = defaultOptions.direction;
    }
}


text:
//%LICENSE////////////////////////////////////////////////////////////////
//
// Licensed to The Open Group (TOG) under one or more contributor license
// agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
// this work for additional information regarding copyright ownership.
// Each contributor licenses this file to you under the OpenPegasus Open
// Source License; you may not use this file except in compliance with the
// License.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////
/* NOCHKSRC */

//
//%/////////////////////////////////////////////////////////////////////////////

#ifndef Pegasus_CIMStatusCode_h
#define Pegasus_CIMStatusCode_h

#include <Pegasus/Common/Config.h>
#include <Pegasus/Common/Linkage.h>

PEGASUS_NAMESPACE_BEGIN

/** CIM Status codes defined in the CIM/HTTP standard. These are the valid
    codes which may be passed on the wire.
*/
enum CIMStatusCode
{
    /// Success.
    CIM_ERR_SUCCESS = 0,

    /// A general error occurred that is not covered by a more specific error code.

    CIM_ERR_FAILED = 1,

    /// Access to a CIM resource was not available to the client.

    CIM_ERR_ACCESS_DENIED = 2,

    /// The target namespace does not exist.

    CIM_ERR_INVALID_NAMESPACE = 3,

    /// One or more parameter values passed to the method were invalid.

    CIM_ERR_INVALID_PARAMETER = 4,

    /// The specified class does not exist.

    CIM_ERR_INVALID_CLASS = 5,

    /// The requested object could not be found.

    CIM_ERR_NOT_FOUND = 6,

    /// The requested operation is not supported.

    CIM_ERR_NOT_SUPPORTED = 7,

    /// Operation cannot be carried out on this class since it has subclasses.

    CIM_ERR_CLASS_HAS_CHILDREN = 8,

    /// Operation cannot be carried out on this class since it has instances.

    CIM_ERR_CLASS_HAS_INSTANCES = 9,

    /// Operation cannot be carried out since the specified superClass does not exist.

    CIM_ERR_INVALID_SUPERCLASS = 10,

    /// Operation cannot be carried out because an object already exists.

    CIM_ERR_ALREADY_EXISTS = 11,

    /// The specified property does not exist:

    CIM_ERR_NO_SUCH_PROPERTY = 12,

    /// The value supplied is incompatible with the type.

    CIM_ERR_TYPE_MISMATCH = 13,

    /// The query language is not recognized or supported.

    CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED = 14,

    /// The query is not valid for the specified query language.

    CIM_ERR_INVALID_QUERY = 15,

    /// The extrinsic method could not be executed.

    CIM_ERR_METHOD_NOT_AVAILABLE = 16,

    /// The specified extrinsic method does not exist.

    CIM_ERR_METHOD_NOT_FOUND = 17
};

PEGASUS_COMMON_LINKAGE const char* cimStatusCodeToString(CIMStatusCode code);

PEGASUS_NAMESPACE_END

#endif /* Pegasus_CIMStatusCode_h */


text:
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\SecNews */
/* @var $form yii\widgets\ActiveForm */
?>

    <?php $form = ActiveForm::begin(); ?>

    <?php //= $form->field($model, 'id')->textInput() ?>

    <?= $form->field($model, 'header')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'sid')->textInput(['maxlength' => true]) ?>

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    <?php ActiveForm::end(); ?>


text:
LaTeXGoodies
========================================
Is a repository which contains LaTeX
preambles and style defintions, which is
usefull for books, reports, articles and
so on.

The repository contains
-----------------------------------------
*Preambles* for document setups

*lstlistings* style definitions for not
just one languages but common through
a document

*prog_langs* lstlistings style definitins
for specific languages.


text:
layout: postag
title: 'VERB'
shortdef: 'verb'
---

#### Description

Verbs typically inflect for tense and mood. Verbs signal events and actions. Verbs can constitute a minimal predicate in a clause, and govern the number and types of other constituents which may occur in the clause.

Irish is a VSO language, thus the verb comes first.

Irish verbs sometimes inflect for person in the form of synthetic verbs. (e.g. _ithim_ "I eat"; _ithimid_ "we eat")

There are four moods: indicative, imperative, conditional and subjunctive. Tenses include present habitual, simple past, past habitual and future.

There is an autonomous verb form, which most closely correlates to the English passive. However it is not technically a passive form as the subject is "understood" and the nominal argument is an object (e.g. _tugadh an liathróid dó_ "the ball was given to him" (lit. somebody gave the ball to him)).

##### Copula vs substantive verb 'to be'

There are two translations of the English verb `to be' in Irish:

* Copula `is'
* Substantive Verb `bí'

These forms behave very differently syntactically, yet both are labelled with the tag VERB.

***copula***
The copula construction follows a COP PRED SUBJ order. An example of copula use is identity constructions (equating two noun phrases). _Is múinteoir é_ "he is a teacher".
The copula is also part of the frequently used cleft/ fronting construction. These constructions are often used to show emphasis. (e.g. _Is leabhar a thug sí dom_ "It's a book she gave me").

***substantive bí***
The substantive verb behaves just like normal Irish verbs. It inflects for person, number and tense.
It can never be used for an identity construction with two noun phrases. If it is used for identity, it follows the pattern of Verb NP PP (e.g. _tá sé ina mhúinteoir_ "he is a teacher" (lit. He is in his teacher))
The pattern followed here is: VERB SUBJ PRED

The substantive verb is also used in conjunction with a verbal noun to form  progressive aspectual phrases.
(e.g. _tá sé ag rith_ "he is running" (lit. he is at running)).

#### Examples

* _D'imigh mé_ "I left"
* _D'imíomar_ "We left"
* _Imím_ "I leave"
* _Imímid_ "We leave"
* _Imeoidh mé_ "I will leave"
* _Imeoimid_ "We will leave"
* _Cuirtear an leabhar ar an mbord_ "the book was put on the table"

Source: Studies in Irish Syntax. Nancy Stenson (1981), Tübingen: Gunter Narr Verlag.


text:
#pragma once
#include <aws/ecs/ECS_EXPORTS.h>
#include <aws/core/utils/memory/stl/AWSString.h>
#include <aws/core/utils/memory/stl/AWSVector.h>
#include <utility>

namespace Aws
{
namespace Utils
{
namespace Json
{
  class JsonValue;
} // namespace Json
} // namespace Utils
namespace ECS
{
namespace Model
{

  /**
   *
Describes the resources available for a container instance.

See
   * Also:
   AWS API
   * Reference

   */
  class AWS_ECS_API Resource
  {
  public:
    Resource();
    Resource(const Aws::Utils::Json::JsonValue& jsonValue);
    Resource& operator=(const Aws::Utils::Json::JsonValue& jsonValue);
    Aws::Utils::Json::JsonValue Jsonize() const;

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline const Aws::String& GetName() const{ return m_name; }

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline void SetName(const Aws::String& value) { m_nameHasBeenSet = true; m_name = value; }

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline void SetName(Aws::String&& value) { m_nameHasBeenSet = true; m_name = std::move(value); }

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline void SetName(const char* value) { m_nameHasBeenSet = true; m_name.assign(value); }

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline Resource& WithName(const Aws::String& value) { SetName(value); return *this;}

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline Resource& WithName(Aws::String&& value) { SetName(std::move(value)); return *this;}

    /**
     *
The name of the resource, such as cpu, memory,
     * ports, or a user-defined resource.

     */
    inline Resource& WithName(const char* value) { SetName(value); return *this;}

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline const Aws::String& GetType() const{ return m_type; }

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline void SetType(const Aws::String& value) { m_typeHasBeenSet = true; m_type = value; }

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline void SetType(Aws::String&& value) { m_typeHasBeenSet = true; m_type = std::move(value); }

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline void SetType(const char* value) { m_typeHasBeenSet = true; m_type.assign(value); }

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline Resource& WithType(const Aws::String& value) { SetType(value); return *this;}

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline Resource& WithType(Aws::String&& value) { SetType(std::move(value)); return *this;}

    /**
     *
The type of the resource, such as INTEGER, DOUBLE,
     * LONG, or STRINGSET.

     */
    inline Resource& WithType(const char* value) { SetType(value); return *this;}

    /**
     *
When the doubleValue type is set, the value of the resource must
     * be a double precision floating-point type.

     */
    inline double GetDoubleValue() const{ return m_doubleValue; }

    /**
     *
When the doubleValue type is set, the value of the resource must
     * be a double precision floating-point type.

     */
    inline void SetDoubleValue(double value) { m_doubleValueHasBeenSet = true; m_doubleValue = value; }

    /**
     *
When the doubleValue type is set, the value of the resource must
     * be a double precision floating-point type.

     */
    inline Resource& WithDoubleValue(double value) { SetDoubleValue(value); return *this;}

    /**
     *
When the longValue type is set, the value of the resource must
     * be an extended precision floating-point type.

     */
    inline long long GetLongValue() const{ return m_longValue; }

    /**
     *
When the longValue type is set, the value of the resource must
     * be an extended precision floating-point type.

     */
    inline void SetLongValue(long long value) { m_longValueHasBeenSet = true; m_longValue = value; }

    /**
     *
When the longValue type is set, the value of the resource must
     * be an extended precision floating-point type.

     */
    inline Resource& WithLongValue(long long value) { SetLongValue(value); return *this;}

    /**
     *
When the integerValue type is set, the value of the resource
     * must be an integer.

     */
    inline int GetIntegerValue() const{ return m_integerValue; }

    /**
     *
When the integerValue type is set, the value of the resource
     * must be an integer.

     */
    inline void SetIntegerValue(int value) { m_integerValueHasBeenSet = true; m_integerValue = value; }

    /**
     *
When the integerValue type is set, the value of the resource
     * must be an integer.

     */
    inline Resource& WithIntegerValue(int value) { SetIntegerValue(value); return *this;}

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline const Aws::Vector<Aws::String>& GetStringSetValue() const{ return m_stringSetValue; }

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline void SetStringSetValue(const Aws::Vector<Aws::String>& value) { m_stringSetValueHasBeenSet = true; m_stringSetValue = value; }

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline void SetStringSetValue(Aws::Vector<Aws::String>&& value) { m_stringSetValueHasBeenSet = true; m_stringSetValue = std::move(value); }

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline Resource& WithStringSetValue(const Aws::Vector<Aws::String>& value) { SetStringSetValue(value); return *this;}

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline Resource& WithStringSetValue(Aws::Vector<Aws::String>&& value) { SetStringSetValue(std::move(value)); return *this;}

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline Resource& AddStringSetValue(const Aws::String& value) { m_stringSetValueHasBeenSet = true; m_stringSetValue.push_back(value); return *this; }

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline Resource& AddStringSetValue(Aws::String&& value) { m_stringSetValueHasBeenSet = true; m_stringSetValue.push_back(std::move(value)); return *this; }

    /**
     *
When the stringSetValue type is set, the value of the resource
     * must be a string type.

     */
    inline Resource& AddStringSetValue(const char* value) { m_stringSetValueHasBeenSet = true; m_stringSetValue.push_back(value); return *this; }

  private:
    Aws::String m_name;
    bool m_nameHasBeenSet;
    Aws::String m_type;
    bool m_typeHasBeenSet;
    double m_doubleValue;
    bool m_doubleValueHasBeenSet;
    long long m_longValue;
    bool m_longValueHasBeenSet;
    int m_integerValue;
    bool m_integerValueHasBeenSet;
    Aws::Vector<Aws::String> m_stringSetValue;
    bool m_stringSetValueHasBeenSet;
  };

} // namespace Model
} // namespace ECS
} // namespace Aws


text:
package service

import (
	"time"

	"github.com/control-center/serviced/coordinator/client"
	"github.com/control-center/serviced/domain/pool"
	"github.com/control-center/serviced/zzk"
)

// PoolListener implements zzk.Listener.  The PoolListener will watch
// pool nodes (/pools/poolid) for changes and then sync virtual IP
// assignments.
type PoolListener struct {
	Timeout      time.Duration
	synchronizer VirtualIPSynchronizer
	connection   client.Connection
}

// NewPoolListener instantiates a new PoolListener
func NewPoolListener(synchronizer VirtualIPSynchronizer) *PoolListener {
	return &PoolListener{synchronizer: synchronizer, Timeout: time.Second * 5}
}

// SetConnection sets the ZooKeeper connection.  It is part of the zzk.Listener
// interface.
func (l *PoolListener) SetConnection(connection client.Connection) {
	l.connection = connection
}

// GetPath returns the path for the pool being watched, /pools/poolid.  It is
// part of the zzk.Listener interface.
func (l *PoolListener) GetPath(nodes ...string) string {
	return Base().Pools().Path()
}

// Ready is part of the zzk.Listener interface.
func (l *PoolListener) Ready() (err error) { return nil }

// Done is part of the zzk.Listener interface.
func (l *PoolListener) Done() {}

// PostProcess is part of the zzk.Listener interface.
func (l *PoolListener) PostProcess(p map[string]struct{}) {}

// Spawn watches a pool and syncs its virtual IPs.
func (l *PoolListener) Spawn(shutdown <-chan interface{}, poolID string) {
	logger := plog.WithField("poolid", poolID)

	logger.Debug("Spawning pool listener")

	stop := make(chan struct{})
	defer func() { close(stop) }()

	timeout := time.NewTimer(1 * time.Second)
	timeout.Stop()

	for {
		poolPath := Base().Pools().ID(poolID)
		node := &PoolNode{ResourcePool: &pool.ResourcePool{}}

		var poolEvent, ipsEvent, poolExistsEvent, ipsExistsEvent <-chan client.Event

		poolExists, poolExistsEvent, err := l.connection.ExistsW(poolPath.Path(), stop)
		if poolExists && err == nil {
			poolEvent, err = l.connection.GetW(poolPath.Path(), node, stop)
			if err == client.ErrNoNode {
				close(stop)
				stop = make(chan struct{})
				continue
			} else if err != nil {
				logger.WithError(err).Error("Unable to watch pool")
				return
			}
		} else if err != nil {
			logger.WithError(err).Error("Unable to check if pool exists")
			return
		}

		children := []string{}
		if poolExists {
			var ipsExists bool
			ipsExists, ipsExistsEvent, err = l.connection.ExistsW(poolPath.IPs().Path(), stop)
			if ipsExists && err == nil {
				children, ipsEvent, err = l.connection.ChildrenW(poolPath.IPs().Path(), stop)
				if err != nil {
					logger.WithError(err).Error("Unable to watch IPs")
					return
				}
			} else if err != nil {
				logger.WithError(err).Error("Unable to watch IPs node")
				return
			}

			assignments, err := l.getAssignmentMap(children)
			if err != nil {
				logger.WithError(err).Error("Unable to get assignments")
				return
			}

			// The sync will add nodes to the ips path which will trigger an ipsEvent
			// causing the loop to occur twice.
			err = l.synchronizer.Sync(*node.ResourcePool, assignments)
			if syncError, ok := err.(SyncError); ok {
				logger.WithError(syncError).WithField("count", len(syncError)).
					Warn("Errors encountered while syncing virtual IPs")

				for _, e := range syncError {
					logger.WithError(e).Debug("Sync error")
				}

				timeout.Reset(l.Timeout)
			} else if err != nil {
				logger.WithError(err).Warn("Error Syncing")
				timeout.Reset(l.Timeout)
			}
		}

		select {
		case <-ipsEvent:
		case <-poolEvent:
		case <-poolExistsEvent:
		case <-ipsExistsEvent:
		case <-timeout.C:
		case <-shutdown:
			return
		}

		if !timeout.Stop() {
			select {
			case <-timeout.C:
			default:
			}
		}

		close(stop)
		stop = make(chan struct{})
	}
}

func (l *PoolListener) getAssignmentMap(hostIPs []string) (map[string]string, error) {
	assignments := make(map[string]string)
	for _, hostIP := range hostIPs {
		host, ip, err := ParseIPID(hostIP)
		if err != nil {
			return nil, err
		}
		assignments[ip] = host
	}
	return assignments, nil
}

func StartPoolListener(shutdown <-chan interface{}, connection client.Connection) {
	assignmentHandler := NewZKAssignmentHandler(
		&RandomHostSelectionStrategy{},
		NewRegisteredHostHandler(connection),
		connection)

	synchronizer := NewZKVirtualIPSynchronizer(assignmentHandler)
	poolListener := NewPoolListener(synchronizer)

	zzk.Start(shutdown, connection, poolListener)
}


text:
package ipc

// System imports
import (
	"net"
	"time"
	"errors"
)

// ipcAddr implements the net.Addr interface for GopherJS IPC connections.
type ipcAddr struct {
	endpoint string
}

func (*ipcAddr) Network() string {
	return "ipc";
}

func (a *ipcAddr) String() string {
	return a.endpoint
}

// ipcConn implements the net.Conn interface for GopherJS IPC connections.
type ipcConn struct {
	address *ipcAddr
	connectionId int
}

func (c *ipcConn) Read(b []byte) (int, error) {
	// Dispatch the request through the bridge
	resultChannel := global.bridge.ConnectionRead(c.connectionId, len(b))

	// Wait for the result
	result := <-resultChannel

	// We always copy the resultant bytes, regardless of errors
	copy(b, result.data)

	// All done
	return len(result.data), result.err
}

func (c *ipcConn) Write(b []byte) (int, error) {
	// Dispatch the request through the bridge
	resultChannel := global.bridge.ConnectionWrite(c.connectionId, b)

	// Wait for the result
	result := <-resultChannel

	// All done
	return result.count, result.err
}

func (c *ipcConn) Close() error {
	// If the connection is already closed, do nothing
	if c.connectionId == -1 {
		return nil
	}

	// Dispatch the request through the bridge
	resultChannel := global.bridge.ConnectionClose(c.connectionId)

	// Wait for the result
	result := <-resultChannel

	// If we were successful, mark the connection as closed
	if result.err == nil {
		c.connectionId = -1
	}

	// All done
	return result.err
}

func (c *ipcConn) LocalAddr() net.Addr {
	return c.address
}

func (c *ipcConn) RemoteAddr() net.Addr {
	return c.address
}

func (c *ipcConn) SetDeadline(t time.Time) error {
	// TODO: Implement.  See note in individual methods below.
	return errors.New("read/write deadlines not supported")
}

func (c *ipcConn) SetReadDeadline(t time.Time) error {
	// TODO: Implement.  We should store the deadline time, and then when making
	// read calls across the bridge, convert it to a timeout based on the start
	// time of the call.  This isn't perfect, and will slip a bit past the
	// deadline given the latency of the bridge, but I think it could still be
	// useful.  On POSIX, we can implement timeouts using SO_RCVTIMEO, and on
	// Windows we can create a CancellationTokenSource with a timeout and pass
	// a token from that to ReadAsync.
	return errors.New("read deadlines not supported")
}

func (c *ipcConn) SetWriteDeadline(t time.Time) error {
	// TODO: Implement.  We should store the deadline time, and then when making
	// write calls across the bridge, convert it to a timeout based on the start
	// time of the call.  This isn't perfect, and will slip a bit past the
	// deadline given the latency of the bridge, but I think it could still be
	// useful.  On POSIX, we can implement timeouts using SO_SNDTIMEO, and on
	// Windows we can create a CancellationTokenSource with a timeout and pass
	// a token from that to WriteAsync.
	return errors.New("write deadlines not supported")
}

// DialIPC establishes a new GopherJS IPC connection.  On POSIX systems, this is
// done using Unix domain sockets, and the endpoint argument should be the path
// of an existing Unix domain socket endpoint to connect to.  On Windows
// systems, this is done using named pipes, and the endpoint argument should be
// the name of an existing named pipe endpoint to connect to.
func DialIPC(endpoint string) (net.Conn, error) {
	// Dispatch the request through the bridge
	resultChannel := global.bridge.Connect(endpoint)

	// Wait for the result
	result := <-resultChannel

	// Watch for errors
	if result.err != nil {
		return nil, result.err
	}

	// All done
	return &ipcConn{
		address: &ipcAddr{endpoint: endpoint},
		connectionId: result.connectionId,
	}, nil
}

// ipcConn implements the net.Listener interface for GopherJS IPC connections.
type ipcListener struct {
	address *ipcAddr
	listenerId int
}

func (l *ipcListener) Accept() (net.Conn, error) {
	// Dispatch the request through the bridge
	resultChannel := global.bridge.ListenerAccept(l.listenerId)

	// Wait for the result
	result := <-resultChannel

	// Watch for errors
	if result.err != nil {
		return nil, result.err
	}

	// All done
	return &ipcConn{
		address: l.address,
		connectionId: result.connectionId,
	}, nil
}

func (l *ipcListener) Close() error {
	// If the listener is already closed, do nothing
	if l.listenerId == -1 {
		return nil
	}

	// Dispatch the request through the bridge
	resultChannel := global.bridge.ConnectionClose(l.listenerId)

	// Wait for the result
	result := <-resultChannel

	// If we were successful, mark the listener as closed
	if result.err == nil {
		l.listenerId = -1
	}

	// All done
	return result.err
}

func (l *ipcListener) Addr() net.Addr {
	return l.address
}

// ListenIPC establishes a new IPC connection listener.  On POSIX systems, this
// is done using Unix domain sockets, and the endpoint argument should be the
// path at which to create the endpoint.  The path should not be bound to an
// existing listener.  On Windows systems, this is done using named pipes, and
// the endpoint argument should be the name of a named pipe at which to create
// the endpoint.  The name should not be bound to an existing listener.
func ListenIPC(endpoint string) (net.Listener, error) {
	// Dispatch the request through the bridge
	resultChannel := global.bridge.Listen(endpoint)

	// Wait for the result
	result := <-resultChannel

	// Watch for errors
	if result.err != nil {
		return nil, result.err
	}

	// All done
	return &ipcListener{
		address: &ipcAddr{endpoint: endpoint},
		listenerId: result.listenerId,
	}, nil
}


text:
package com.prancingdonkey.product.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="product_type")
public class ProductType {

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	Long id;

	String name;

	String description;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}


text:
#ifndef RCS_JNI_ENV_WRAPPER_H_
#define RCS_JNI_ENV_WRAPPER_H_

#include <jni.h>

#include <exception>

#include "JavaLocalRef.h"

/**
 * @class JavaException
 */
class JavaException: public std::exception
{
};

/*
 * @class JNIEnvWrapper
 */
class JNIEnvWrapper
{
public:
    JNIEnvWrapper() noexcept : m_env{ } {}
    JNIEnvWrapper(JNIEnv* env) noexcept : m_env{ env } {}

    JNIEnvWrapper& operator=(JNIEnv* env) noexcept
    {
        m_env = env;
        return *this;
    }

    /**
     *  check the object is same or not
     *  compare the lhs and rhs object
     */
    jboolean IsSameObject(jobject lhs, jobject rhs)
    {
        return m_env->IsSameObject(lhs, rhs);
    }

    /* get object class
     * @param obj   object
     */
    jclass GetObjectClass(jobject obj)
    {
        auto ret = m_env->GetObjectClass(obj);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /**
     * This method is to find the class
     * @param name    name of class to find
     * @return the class
     */
    jclass FindClass(const char* name)
    {
        auto ret = m_env->FindClass(name);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /**
     * This method is to find class as global reference
     * @param name   name of reference to find
     * @return the new reference class
     */
    jclass FindClassAsGlobalRef(const char* name)
    {
        JavaLocalClass cls{ m_env, FindClass(name) };

        return NewGlobalRef(cls);
    }

    /**
     * This method is to create new global reference
     * @pram obj   new object to create
     * @return the new object
     */
    jobject NewGlobalRef(jobject obj)
    {
        auto ret = m_env->NewGlobalRef(obj);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    template<typename T>
    T NewGlobalRef(T obj)
    {
        auto ret = static_cast< T >(m_env->NewGlobalRef(obj));
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    template<typename T>
    T NewGlobalRef(const JavaLocalRef< T >& obj)
    {
        auto ret = static_cast< T >(m_env->NewGlobalRef(obj));
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** This method is to delete global reference */
    void DeleteGlobalRef(jobject obj)
    {
        m_env->DeleteGlobalRef(obj);
    }

    /**
     * This method is to create new object
     */
    jobject NewObject(jclass cls, jmethodID ctor, ...)
    {
        va_list args;
        va_start(args, ctor);
        auto ret = m_env->NewObjectV(cls, ctor,args);
        va_end(args);

        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** new string UTF */
    jstring NewStringUTF(const char* str)
    {
        auto ret = m_env->NewStringUTF(str);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get string UTF chars */
    const char* GetStringUTFChars(jstring str, jboolean* isCopy)
    {
        auto ret = m_env->GetStringUTFChars(str, isCopy);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** release string UTF chars */
    void ReleaseStringUTFChars(jstring str, const char* chars)
    {
        m_env->ReleaseStringUTFChars(str, chars);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** get constructor Id */
    jmethodID GetConstructorID(jclass cls, const char* sig)
    {
        auto ret = m_env->GetMethodID(cls, "<init>", sig);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get method Id */
    jmethodID GetMethodID(jclass cls, const char* name, const char* sig)
    {
        auto ret = m_env->GetMethodID(cls, name, sig);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get static method Id */
    jmethodID GetStaticMethodID(jclass cls, const char* name, const char* sig)
    {
        auto ret = m_env->GetStaticMethodID(cls, name, sig);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get field Id */
    jfieldID GetFieldID(jclass cls, const char* name, const char* sig)
    {
        auto ret = m_env->GetFieldID(cls, name, sig);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get static field Id */
    jfieldID GetStaticFieldID(jclass cls, const char* name, const char* sig)
    {
        auto ret = m_env->GetStaticFieldID(cls, name, sig);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get static object field */
    jobject GetStaticObjectField(jclass cls, jfieldID fieldId)
    {
        auto ret = m_env->GetStaticObjectField(cls, fieldId);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /* get static object filed */
    jobject GetStaticObjectField(jclass cls, const char* name, const char* sig)
    {
        return GetStaticObjectField(cls, GetStaticFieldID(cls, name, sig));
    }

    /** get Int filed */
    jint GetIntField(jobject obj, jfieldID fieldId)
    {
        auto ret = m_env->GetIntField(obj, fieldId);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get long field */
    jlong GetLongField(jobject obj, jfieldID fieldId)
    {
        auto ret = m_env->GetLongField(obj, fieldId);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** set long field */
    void SetLongField(jobject obj, jfieldID fieldId, jlong val)
    {
        m_env->SetLongField(obj, fieldId, val);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** get object field */
    jobject GetObjectField(jobject obj, jfieldID fieldId)
    {
        auto ret = m_env->GetObjectField(obj, fieldId);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** call static Int method */
    jint CallStaticIntMethod(jclass cls, jmethodID methodId, ...)
    {
        va_list args;
        va_start(args, methodId);
        auto ret = m_env->CallStaticIntMethodV(cls, methodId, args);
        va_end(args);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** call static object method */
    jobject CallStaticObjectMethod(jclass cls, jmethodID methodId, ...)
    {
        va_list args;
        va_start(args, methodId);
        auto ret = m_env->CallStaticObjectMethodV(cls, methodId, args);
        va_end(args);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** call void method */
    void CallVoidMethod(jobject obj, jmethodID methodId, ...)
    {
         va_list args;
         va_start(args, methodId);
         m_env->CallVoidMethodV(obj, methodId, args);
         va_end(args);
         if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** call boolean method */
    jboolean CallBooleanMethod(jobject obj, jmethodID methodId, ...)
    {
        va_list args;
        va_start(args, methodId);
        auto ret = m_env->CallBooleanMethodV(obj, methodId, args);
        va_end(args);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** call Int method */
    jint CallIntMethod(jobject obj, jmethodID methodId, ...)
    {
         va_list args;
         va_start(args, methodId);
         auto ret = m_env->CallIntMethod(obj, methodId, args);
         va_end(args);
         if (m_env->ExceptionCheck()) throw JavaException();
         return ret;
    }

     /** call double method */
     jdouble CallDoubleMethod(jobject obj, jmethodID methodId, ...)
     {
          va_list args;
          va_start(args ,methodId);
          auto ret = m_env->CallDoubleMethod(obj, methodId, args);
          va_end(args);
          if (m_env->ExceptionCheck()) throw JavaException();
          return ret;
     }

    /** call object method */
    jobject CallObjectMethod(jobject obj, jmethodID methodId, ...)
    {
        va_list args;
        va_start(args, methodId);
        auto ret = m_env->CallObjectMethodV(obj, methodId, args);
        va_end(args);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /*
     * create new boolean array
     *
     * @param len   size of array
     */
    jbooleanArray NewBooleanArray(jsize len)
    {
        auto ret = m_env->NewBooleanArray(len);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /*
     * create new Int array
     *
     * @param len   size of array
     */
    jintArray NewIntArray(jsize len)
    {
        auto ret = m_env->NewIntArray(len);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /*
     * create new double array
     *
     * @param len   size of array
     */
    jdoubleArray NewDoubleArray(jsize len)
    {
        auto ret = m_env->NewDoubleArray(len);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /*
     * create new byte array
     *
     * @param len   size of array
     */
    jbyteArray NewByteArray(jsize len)
    {
        auto ret = m_env->NewByteArray(len);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

  /* new object array */
  jobjectArray NewObjectArray(jsize len, jclass cls, jobject init)
    {
        auto ret = m_env->NewObjectArray(len, cls, init);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /**
     * get array length
     *
     * @param array   find length of this array
     *
     * @return size of array
     */
    jsize GetArrayLength(jarray array)
    {
        auto ret = m_env->GetArrayLength(array);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get object array element */
    jobject GetObjectArrayElement(jobjectArray array, jsize index)
    {
        auto ret = m_env->GetObjectArrayElement(array, index);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** get byte array element */
    jbyte *GetByteArrayElements(jbyteArray array, jboolean *value)
    {
        auto ret = m_env->GetByteArrayElements(array, value);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** set object array element */
    void SetObjectArrayElement(jobjectArray array, jsize index, jobject val)
    {
        m_env->SetObjectArrayElement(array, index, val);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** set boolean array region */
    void SetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, const jboolean* buf)
    {
        m_env->SetBooleanArrayRegion(array, start, len, buf);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** set Integer array region */
    void SetIntArrayRegion(jintArray array, jsize start, jsize len, const jint* buf)
    {
        m_env->SetIntArrayRegion(array, start, len, buf);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** set double array region */
    void SetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, const jdouble* buf)
    {
        m_env->SetDoubleArrayRegion(array, start, len, buf);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** set byte array region */
    void SetByteArrayRegion(jbyteArray array, jsize start, jsize len, const jbyte *buf)
    {
        m_env->SetByteArrayRegion(array, start, len, buf);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** get primitive array critical */
    void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy)
    {
        auto ret = m_env->GetPrimitiveArrayCritical(array, isCopy);
        if (m_env->ExceptionCheck()) throw JavaException();
        return ret;
    }

    /** release primitive array critical */
    void ReleasePrimitiveArrayCritical(jarray array, void* carray, int mode)
    {
        m_env->ReleasePrimitiveArrayCritical(array, carray, mode);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** release byte array element */
    void ReleaseByteArrayElements(jbyteArray array, jbyte* byteArray, int mode)
    {
        m_env->ReleaseByteArrayElements(array, byteArray, mode);
        if (m_env->ExceptionCheck()) throw JavaException();
    }

    /** throw exception */
    void ThrowNew(jclass cls, const char* msg)
    {
        m_env->ThrowNew(cls, msg);
        throw JavaException();
    }

    /** exception describe */
    void ExceptionDescribe() const noexcept
    {
        m_env->ExceptionDescribe();
    }

    /** exception clear */
    void ExceptionClear() noexcept
    {
        m_env->ExceptionClear();
    }

    /** exception check */
    jboolean ExceptionCheck() const noexcept
    {
        return m_env->ExceptionCheck();
    }

    operator bool() const noexcept
    {
        return m_env != nullptr;
    }

    /** get environment */
    JNIEnv* get() const
    {
        return m_env;
    }

private:
    JNIEnv* m_env;
};

#endif // RCS_JNI_ENV_WRAPPER_H_


text:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/*
 * \file sockutils.c
 *
 * The goal of this file is to provide a common set of primitives for socket
 * manipulation.
 *
 * Although the socket interface defined in the RFC 2553 (and its updates)
 * is excellent, there are still differences between the behavior of those
 * routines on UN*X and Windows, and between UN*Xes.
 *
 * These calls provide an interface similar to the socket interface, but
 * that hides the differences between operating systems.  It does not
 * attempt to significantly improve on the socket interface in other
 * ways.
 */

#include <string.h>	/* for strerror() */
#include <errno.h>	/* for the errno variable */
#include <stdio.h>	/* for the stderr file */
#include <stdlib.h>	/* for malloc() and free() */
#ifdef HAVE_LIMITS_H
#include <limits.h>
#else
#define INT_MAX		2147483647
#endif

#include "portability.h"
#include "sockutils.h"

#ifdef _WIN32
  /*
   * Winsock initialization.
   *
   * Ask for WinSock 2.2.
   */
  #define WINSOCK_MAJOR_VERSION 2
  #define WINSOCK_MINOR_VERSION 2

  static int sockcount = 0;	/*!< Variable that allows calling the WSAStartup() only one time */
#endif

/* Some minor differences between UNIX and Win32 */
#ifdef _WIN32
  #define SHUT_WR SD_SEND	/* The control code for shutdown() is different in Win32 */
#endif

/* Size of the buffer that has to keep error messages */
#define SOCK_ERRBUF_SIZE 1024

/* Constants; used in order to keep strings here */
#define SOCKET_NO_NAME_AVAILABLE "No name available"
#define SOCKET_NO_PORT_AVAILABLE "No port available"
#define SOCKET_NAME_NULL_DAD "Null address (possibly DAD Phase)"

/****************************************************
 *                                                  *
 * Locally defined functions                        *
 *                                                  *
 ****************************************************/

static int sock_ismcastaddr(const struct sockaddr *saddr);

/****************************************************
 *                                                  *
 * Function bodies                                  *
 *                                                  *
 ****************************************************/

/*
 * \brief It retrieves the error message after an error occurred in the socket interface.
 *
 * This function is defined because of the different way errors are returned in UNIX
 * and Win32. This function provides a consistent way to retrieve the error message
 * (after a socket error occurred) on all the platforms.
 *
 * \param caller: a pointer to a user-allocated string which contains a message that has
 * to be printed *before* the true error message. It could be, for example, 'this error
 * comes from the recv() call at line 31'. It may be NULL.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return No return values. The error message is returned in the 'string' parameter.
 */
void sock_geterror(const char *caller, char *errbuf, int errbuflen)
{
#ifdef _WIN32
	int retval;
	int code;
	TCHAR message[SOCK_ERRBUF_SIZE];	/* It will be char (if we're using ascii) or wchar_t (if we're using unicode) */

	if (errbuf == NULL)
		return;

	code = GetLastError();

	retval = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
		FORMAT_MESSAGE_MAX_WIDTH_MASK,
		NULL, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		message, sizeof(message) / sizeof(TCHAR), NULL);

	if (retval == 0)
	{
		if ((caller) && (*caller))
			pcap_snprintf(errbuf, errbuflen, "%sUnable to get the exact error message", caller);
		else
			pcap_snprintf(errbuf, errbuflen, "Unable to get the exact error message");
		return;
	}
	else
	{
		if ((caller) && (*caller))
			pcap_snprintf(errbuf, errbuflen, "%s%s (code %d)", caller, message, code);
		else
			pcap_snprintf(errbuf, errbuflen, "%s (code %d)", message, code);
	}
#else
	char *message;

	if (errbuf == NULL)
		return;

	message = strerror(errno);

	if ((caller) && (*caller))
		pcap_snprintf(errbuf, errbuflen, "%s%s (code %d)", caller, message, errno);
	else
		pcap_snprintf(errbuf, errbuflen, "%s (code %d)", message, errno);
#endif
}

/*
 * \brief It initializes sockets.
 *
 * This function is pretty useless on UNIX, since socket initialization is not required.
 * However it is required on Win32. In UNIX, this function appears to be completely empty.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
 * in the 'errbuf' variable.
 */
int sock_init(char *errbuf, int errbuflen)
{
#ifdef _WIN32
	if (sockcount == 0)
	{
		WSADATA wsaData;			/* helper variable needed to initialize Winsock */

		if (WSAStartup(MAKEWORD(WINSOCK_MAJOR_VERSION,
		    WINSOCK_MINOR_VERSION), &wsaData) != 0)
		{
			if (errbuf)
				pcap_snprintf(errbuf, errbuflen, "Failed to initialize Winsock\n");

			WSACleanup();

			return -1;
		}
	}

	sockcount++;
#endif

	return 0;
}

/*
 * \brief It deallocates sockets.
 *
 * This function is pretty useless on UNIX, since socket deallocation is not required.
 * However it is required on Win32. In UNIX, this function appears to be completely empty.
 *
 * \return No error values.
 */
void sock_cleanup(void)
{
#ifdef _WIN32
	sockcount--;

	if (sockcount == 0)
		WSACleanup();
#endif
}

/*
 * \brief It checks if the sockaddr variable contains a multicast address.
 *
 * \return '0' if the address is multicast, '-1' if it is not.
 */
static int sock_ismcastaddr(const struct sockaddr *saddr)
{
	if (saddr->sa_family == PF_INET)
	{
		struct sockaddr_in *saddr4 = (struct sockaddr_in *) saddr;
		if (IN_MULTICAST(ntohl(saddr4->sin_addr.s_addr))) return 0;
		else return -1;
	}
	else
	{
		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) saddr;
		if (IN6_IS_ADDR_MULTICAST(&saddr6->sin6_addr)) return 0;
		else return -1;
	}
}

/*
 * \brief It initializes a network connection both from the client and the server side.
 *
 * In case of a client socket, this function calls socket() and connect().
 * In the meanwhile, it checks for any socket error.
 * If an error occurs, it writes the error message into 'errbuf'.
 *
 * In case of a server socket, the function calls socket(), bind() and listen().
 *
 * This function is usually preceeded by the sock_initaddress().
 *
 * \param addrinfo: pointer to an addrinfo variable which will be used to
 * open the socket and such. This variable is the one returned by the previous call to
 * sock_initaddress().
 *
 * \param server: '1' if this is a server socket, '0' otherwise.
 *
 * \param nconn: number of the connections that are allowed to wait into the listen() call.
 * This value has no meanings in case of a client socket.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return the socket that has been opened (that has to be used in the following sockets calls)
 * if everything is fine, '0' if some errors occurred. The error message is returned
 * in the 'errbuf' variable.
 */
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen)
{
	SOCKET sock;

	sock = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
	if (sock == -1)
	{
		sock_geterror("socket(): ", errbuf, errbuflen);
		return -1;
	}

	/* This is a server socket */
	if (server)
	{
#ifdef BSD
		/*
		 * Force the use of IPv6-only addresses; in BSD you can accept both v4 and v6
		 * connections if you have a "NULL" pointer as the nodename in the getaddrinfo()
		 * This behavior is not clear in the RFC 2553, so each system implements the
		 * bind() differently from this point of view
		 */
		if (addrinfo->ai_family == PF_INET6)
		{
			int on;

			if (setsockopt(sock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (int)) == -1)
			{
				if (errbuf)
					pcap_snprintf(errbuf, errbuflen, "setsockopt(IPV6_BINDV6ONLY)");
				return -1;
			}
		}
#endif

		/* WARNING: if the address is a mcast one, I should place the proper Win32 code here */
		if (bind(sock, addrinfo->ai_addr, (int) addrinfo->ai_addrlen) != 0)
		{
			sock_geterror("bind(): ", errbuf, errbuflen);
			return -1;
		}

		if (addrinfo->ai_socktype == SOCK_STREAM)
			if (listen(sock, nconn) == -1)
			{
				sock_geterror("listen(): ", errbuf, errbuflen);
				return -1;
			}

		/* server side ended */
		return sock;
	}
	else	/* we're the client */
	{
		struct addrinfo *tempaddrinfo;
		char *errbufptr;
		size_t bufspaceleft;

		tempaddrinfo = addrinfo;
		errbufptr = errbuf;
		bufspaceleft = errbuflen;
		*errbufptr = 0;

		/*
		 * We have to loop though all the addinfo returned.
		 * For instance, we can have both IPv6 and IPv4 addresses, but the service we're trying
		 * to connect to is unavailable in IPv6, so we have to try in IPv4 as well
		 */
		while (tempaddrinfo)
		{

			if (connect(sock, tempaddrinfo->ai_addr, (int) tempaddrinfo->ai_addrlen) == -1)
			{
				size_t msglen;
				char TmpBuffer[100];
				char SocketErrorMessage[SOCK_ERRBUF_SIZE];

				/*
				 * We have to retrieve the error message before any other socket call completes, otherwise
				 * the error message is lost
				 */
				sock_geterror(NULL, SocketErrorMessage, sizeof(SocketErrorMessage));

				/* Returns the numeric address of the host that triggered the error */
				sock_getascii_addrport((struct sockaddr_storage *) tempaddrinfo->ai_addr, TmpBuffer, sizeof(TmpBuffer), NULL, 0, NI_NUMERICHOST, TmpBuffer, sizeof(TmpBuffer));

				pcap_snprintf(errbufptr, bufspaceleft,
				    "Is the server properly installed on %s?  connect() failed: %s", TmpBuffer, SocketErrorMessage);

				/* In case more then one 'connect' fails, we manage to keep all the error messages */
				msglen = strlen(errbufptr);

				errbufptr[msglen] = ' ';
				errbufptr[msglen + 1] = 0;

				bufspaceleft = bufspaceleft - (msglen + 1);
				errbufptr += (msglen + 1);

				tempaddrinfo = tempaddrinfo->ai_next;
			}
			else
				break;
		}

		/*
		 * Check how we exit from the previous loop
		 * If tempaddrinfo is equal to NULL, it means that all the connect() failed.
		 */
		if (tempaddrinfo == NULL)
		{
			closesocket(sock);
			return -1;
		}
		else
			return sock;
	}
}

/*
 * \brief Closes the present (TCP and UDP) socket connection.
 *
 * This function sends a shutdown() on the socket in order to disable send() calls
 * (while recv() ones are still allowed). Then, it closes the socket.
 *
 * \param sock: the socket identifier of the connection that has to be closed.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
 * in the 'errbuf' variable.
 */
int sock_close(SOCKET sock, char *errbuf, int errbuflen)
{
	/*
	 * SHUT_WR: subsequent calls to the send function are disallowed.
	 * For TCP sockets, a FIN will be sent after all data is sent and
	 * acknowledged by the Server.
	 */
	if (shutdown(sock, SHUT_WR))
	{
		sock_geterror("shutdown(): ", errbuf, errbuflen);
		/* close the socket anyway */
		closesocket(sock);
		return -1;
	}

	closesocket(sock);
	return 0;
}

/*
 * \brief Checks that the address, port and flags given are valids and it returns an 'addrinfo' structure.
 *
 * This function basically calls the getaddrinfo() calls, and it performs a set of sanity checks
 * to control that everything is fine (e.g. a TCP socket cannot have a mcast address, and such).
 * If an error occurs, it writes the error message into 'errbuf'.
 *
 * \param host: a pointer to a string identifying the host. It can be
 * a host name, a numeric literal address, or NULL or "" (useful
 * in case of a server socket which has to bind to all addresses).
 *
 * \param port: a pointer to a user-allocated buffer containing the network port to use.
 *
 * \param hints: an addrinfo variable (passed by reference) containing the flags needed to create the
 * addrinfo structure appropriately.
 *
 * \param addrinfo: it represents the true returning value. This is a pointer to an addrinfo variable
 * (passed by reference), which will be allocated by this function and returned back to the caller.
 * This variable will be used in the next sockets calls.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
 * in the 'errbuf' variable. The addrinfo variable that has to be used in the following sockets calls is
 * returned into the addrinfo parameter.
 *
 * \warning The 'addrinfo' variable has to be deleted by the programmer by calling freeaddrinfo() when
 * it is no longer needed.
 *
 * \warning This function requires the 'hints' variable as parameter. The semantic of this variable is the same
 * of the one of the corresponding variable used into the standard getaddrinfo() socket function. We suggest
 * the programmer to look at that function in order to set the 'hints' variable appropriately.
 */
int sock_initaddress(const char *host, const char *port,
    struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf, int errbuflen)
{
	int retval;

	retval = getaddrinfo(host, port, hints, addrinfo);
	if (retval != 0)
	{
		/*
		 * if the getaddrinfo() fails, you have to use gai_strerror(), instead of using the standard
		 * error routines (errno) in UNIX; Winsock suggests using the GetLastError() instead.
		 */
		if (errbuf)
		{
#ifdef _WIN32
			sock_geterror("getaddrinfo(): ", errbuf, errbuflen);
#else
			pcap_snprintf(errbuf, errbuflen, "getaddrinfo() %s", gai_strerror(retval));
#endif
		}
		return -1;
	}
	/*
	 * \warning SOCKET: I should check all the accept() in order to bind to all addresses in case
	 * addrinfo has more han one pointers
	 */

	/*
	 * This software only supports PF_INET and PF_INET6.
	 *
	 * XXX - should we just check that at least *one* address is
	 * either PF_INET or PF_INET6, and, when using the list,
	 * ignore all addresses that are neither?  (What, no IPX
	 * support? :-))
	 */
	if (((*addrinfo)->ai_family != PF_INET) &&
	    ((*addrinfo)->ai_family != PF_INET6))
	{
		if (errbuf)
			pcap_snprintf(errbuf, errbuflen, "getaddrinfo(): socket type not supported");
		return -1;
	}

	/*
	 * You can't do multicast (or broadcast) TCP.
	 */
	if (((*addrinfo)->ai_socktype == SOCK_STREAM) &&
	    (sock_ismcastaddr((*addrinfo)->ai_addr) == 0))
	{
		if (errbuf)
			pcap_snprintf(errbuf, errbuflen, "getaddrinfo(): multicast addresses are not valid when using TCP streams");
		return -1;
	}

	return 0;
}

/*
 * \brief It sends the amount of data contained into 'buffer' on the given socket.
 *
 * This function basically calls the send() socket function and it checks that all
 * the data specified in 'buffer' (of size 'size') will be sent. If an error occurs,
 * it writes the error message into 'errbuf'.
 * In case the socket buffer does not have enough space, it loops until all data
 * has been sent.
 *
 * \param socket: the connected socket currently opened.
 *
 * \param buffer: a char pointer to a user-allocated buffer in which data is contained.
 *
 * \param size: number of bytes that have to be sent.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
 * in the 'errbuf' variable.
 */
int sock_send(SOCKET socket, const char *buffer, int size, char *errbuf, int errbuflen)
{
	int nsent;

send:
#ifdef linux
	/*
	 * Another pain... in Linux there's this flag
	 * MSG_NOSIGNAL
	 * Requests not to send SIGPIPE on errors on stream-oriented
	 * sockets when the other end breaks the connection.
	 * The EPIPE error is still returned.
	 */
	nsent = send(socket, buffer, size, MSG_NOSIGNAL);
#else
	nsent = send(socket, buffer, size, 0);
#endif

	if (nsent == -1)
	{
		sock_geterror("send(): ", errbuf, errbuflen);
		return -1;
	}

	if (nsent != size)
	{
		size -= nsent;
		buffer += nsent;
		goto send;
	}

	return 0;
}

/*
 * \brief It copies the amount of data contained into 'buffer' into 'tempbuf'.
 * and it checks for buffer overflows.
 *
 * This function basically copies 'size' bytes of data contained into 'buffer'
 * into 'tempbuf', starting at offset 'offset'. Before that, it checks that the
 * resulting buffer will not be larger	than 'totsize'. Finally, it updates
 * the 'offset' variable in order to point to the first empty location of the buffer.
 *
 * In case the function is called with 'checkonly' equal to 1, it does not copy
 * the data into the buffer. It only checks for buffer overflows and it updates the
 * 'offset' variable. This mode can be useful when the buffer already contains the
 * data (maybe because the producer writes directly into the target buffer), so
 * only the buffer overflow check has to be made.
 * In this case, both 'buffer' and 'tempbuf' can be NULL values.
 *
 * This function is useful in case the userland application does not know immediately
 * all the data it has to write into the socket. This function provides a way to create
 * the "stream" step by step, appending the new data to the old one. Then, when all the
 * data has been bufferized, the application can call the sock_send() function.
 *
 * \param buffer: a char pointer to a user-allocated buffer that keeps the data
 * that has to be copied.
 *
 * \param size: number of bytes that have to be copied.
 *
 * \param tempbuf: user-allocated buffer (of size 'totsize') in which data
 * has to be copied.
 *
 * \param offset: an index into 'tempbuf' which keeps the location of its first
 * empty location.
 *
 * \param totsize: total size of the buffer in which data is being copied.
 *
 * \param checkonly: '1' if we do not want to copy data into the buffer and we
 * want just do a buffer ovreflow control, '0' if data has to be copied as well.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message
 * is returned in the 'errbuf' variable. When the function returns, 'tempbuf' will
 * have the new string appended, and 'offset' will keep the length of that buffer.
 * In case of 'checkonly == 1', data is not copied, but 'offset' is updated in any case.
 *
 * \warning This function assumes that the buffer in which data has to be stored is
 * large 'totbuf' bytes.
 *
 * \warning In case of 'checkonly', be carefully to call this function *before* copying
 * the data into the buffer. Otherwise, the control about the buffer overflow is useless.
 */
int sock_bufferize(const char *buffer, int size, char *tempbuf, int *offset, int totsize, int checkonly, char *errbuf, int errbuflen)
{
	if ((*offset + size) > totsize)
	{
		if (errbuf)
			pcap_snprintf(errbuf, errbuflen, "Not enough space in the temporary send buffer.");
		return -1;
	}

	if (!checkonly)
		memcpy(tempbuf + (*offset), buffer, size);

	(*offset) += size;

	return 0;
}

/*
 * \brief It waits on a connected socket and it manages to receive data.
 *
 * This function basically calls the recv() socket function and it checks that no
 * error occurred. If that happens, it writes the error message into 'errbuf'.
 *
 * This function changes its behavior according to the 'receiveall' flag: if we
 * want to receive exactly 'size' byte, it loops on the recv()	until all the requested
 * data is arrived. Otherwise, it returns the data currently available.
 *
 * In case the socket does not have enough data available, it cycles on the recv()
 * until the requested data (of size 'size') is arrived.
 * In this case, it blocks until the number of bytes read is equal to 'size'.
 *
 * \param sock: the connected socket currently opened.
 *
 * \param buffer: a char pointer to a user-allocated buffer in which data has to be stored
 *
 * \param size: size of the allocated buffer. WARNING: this indicates the number of bytes
 * that we are expecting to be read.
 *
 * \param receiveall: if '0' (or SOCK_RECEIVEALL_NO), it returns as soon as some data
 * is ready; otherwise, (or SOCK_RECEIVEALL_YES) it waits until 'size' data has been
 * received (in case the socket does not have enough data available).
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return the number of bytes read if everything is fine, '-1' if some errors occurred.
 * The error message is returned in the 'errbuf' variable.
 */

/*
 * On UN*X, recv() returns ssize_t.
 * On Windows, there *is* no ssize_t, and it returns an int.
 * Define ssize_t as int on Windows so we can use it as the return value
 * from recv().
 */
#ifdef _WIN32
typedef int ssize_t;
#endif

int sock_recv(SOCKET sock, void *buffer, size_t size, int receiveall,
    char *errbuf, int errbuflen)
{
	char *bufp = buffer;
	int remaining;
	ssize_t nread;

	if (size == 0)
	{
		SOCK_ASSERT("I have been requested to read zero bytes", 1);
		return 0;
	}
	if (size > INT_MAX)
	{
		pcap_snprintf(errbuf, errbuflen, "Can't read more than %u bytes with sock_recv",
		    INT_MAX);
		return -1;
	}

	bufp = (char *) buffer;
	remaining = (int) size;

	/*
	 * We don't use MSG_WAITALL because it's not supported in
	 * Win32.
	 */
	for (;;) {
		nread = recv(sock, bufp, remaining, 0);

		if (nread == -1)
		{
#ifndef _WIN32
			if (errno == EINTR)
				return -3;
#endif
			sock_geterror("recv(): ", errbuf, errbuflen);
			return -1;
		}

		if (nread == 0)
		{
			if (errbuf)
			{
				pcap_snprintf(errbuf, errbuflen,
				    "The other host terminated the connection.");
			}
			return -1;
		}

		/*
		 * Do we want to read the amount requested, or just return
		 * what we got?
		 */
		if (!receiveall)
		{
			/*
			 * Just return what we got.
			 */
			return (int) nread;
		}

		bufp += nread;
		remaining -= nread;

		if (remaining == 0)
			return (int) size;
	}
}

/*
 * \brief It discards N bytes that are currently waiting to be read on the current socket.
 *
 * This function is useful in case we receive a message we cannot understand (e.g.
 * wrong version number when receiving a network packet), so that we have to discard all
 * data before reading a new message.
 *
 * This function will read 'size' bytes from the socket and discard them.
 * It defines an internal buffer in which data will be copied; however, in case
 * this buffer is not large enough, it will cycle in order to read everything as well.
 *
 * \param sock: the connected socket currently opened.
 *
 * \param size: number of bytes that have to be discarded.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '0' if everything is fine, '-1' if some errors occurred.
 * The error message is returned in the 'errbuf' variable.
 */
int sock_discard(SOCKET sock, int size, char *errbuf, int errbuflen)
{
#define TEMP_BUF_SIZE 32768

	char buffer[TEMP_BUF_SIZE];		/* network buffer, to be used when the message is discarded */

	/*
	 * A static allocation avoids the need of a 'malloc()' each time we want to discard a message
	 * Our feeling is that a buffer if 32KB is enough for most of the application;
	 * in case this is not enough, the "while" loop discards the message by calling the
	 * sockrecv() several times.
	 * We do not want to create a bigger variable because this causes the program to exit on
	 * some platforms (e.g. BSD)
	 */
	while (size > TEMP_BUF_SIZE)
	{
		if (sock_recv(sock, buffer, TEMP_BUF_SIZE, SOCK_RECEIVEALL_YES, errbuf, errbuflen) == -1)
			return -1;

		size -= TEMP_BUF_SIZE;
	}

	/*
	 * If there is still data to be discarded
	 * In this case, the data can fit into the temporary buffer
	 */
	if (size)
	{
		if (sock_recv(sock, buffer, size, SOCK_RECEIVEALL_YES, errbuf, errbuflen) == -1)
			return -1;
	}

	SOCK_ASSERT("I'm currently discarding data\n", 1);

	return 0;
}

/*
 * \brief Checks that one host (identified by the sockaddr_storage structure) belongs to an 'allowed list'.
 *
 * This function is useful after an accept() call in order to check if the connecting
 * host is allowed to connect to me. To do that, we have a buffer that keeps the list of the
 * allowed host; this function checks the sockaddr_storage structure of the connecting host
 * against this host list, and it returns '0' is the host is included in this list.
 *
 * \param hostlist: pointer to a string that contains the list of the allowed host.
 *
 * \param sep: a string that keeps the separators used between the hosts (for example the
 * space character) in the host list.
 *
 * \param from: a sockaddr_storage structure, as it is returned by the accept() call.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return It returns:
 * - '1' if the host list is empty
 * - '0' if the host belongs to the host list (and therefore it is allowed to connect)
 * - '-1' in case the host does not belong to the host list (and therefore it is not allowed to connect
 * - '-2' in case or error. The error message is returned in the 'errbuf' variable.
 */
int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen)
{
	/* checks if the connecting host is among the ones allowed */
	if ((hostlist) && (hostlist[0]))
	{
		char *token;					/* temp, needed to separate items into the hostlist */
		struct addrinfo *addrinfo, *ai_next;
		char *temphostlist;
		char *lasts;

		/*
		 * The problem is that strtok modifies the original variable by putting '0' at the end of each token
		 * So, we have to create a new temporary string in which the original content is kept
		 */
		temphostlist = strdup(hostlist);
		if (temphostlist == NULL)
		{
			sock_geterror("sock_check_hostlist(), malloc() failed", errbuf, errbuflen);
			return -2;
		}

		token = pcap_strtok_r(temphostlist, sep, &lasts);

		/* it avoids a warning in the compilation ('addrinfo used but not initialized') */
		addrinfo = NULL;

		while (token != NULL)
		{
			struct addrinfo hints;
			int retval;

			addrinfo = NULL;
			memset(&hints, 0, sizeof(struct addrinfo));
			hints.ai_family = PF_UNSPEC;
			hints.ai_socktype = SOCK_STREAM;

			retval = getaddrinfo(token, "0", &hints, &addrinfo);
			if (retval != 0)
			{
				if (errbuf)
					pcap_snprintf(errbuf, errbuflen, "getaddrinfo() %s", gai_strerror(retval));

				SOCK_ASSERT(errbuf, 1);

				/* Get next token */
				token = pcap_strtok_r(NULL, sep, &lasts);
				continue;
			}

			/* ai_next is required to preserve the content of addrinfo, in order to deallocate it properly */
			ai_next = addrinfo;
			while (ai_next)
			{
				if (sock_cmpaddr(from, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
				{
					free(temphostlist);
					return 0;
				}

				/*
				 * If we are here, it means that the current address does not matches
				 * Let's try with the next one in the header chain
				 */
				ai_next = ai_next->ai_next;
			}

			freeaddrinfo(addrinfo);
			addrinfo = NULL;

			/* Get next token */
			token = pcap_strtok_r(NULL, sep, &lasts);
		}

		if (addrinfo)
		{
			freeaddrinfo(addrinfo);
			addrinfo = NULL;
		}

		if (errbuf)
			pcap_snprintf(errbuf, errbuflen, "The host is not in the allowed host list. Connection refused.");

		free(temphostlist);
		return -1;
	}

	/* No hostlist, so we have to return 'empty list' */
	return 1;
}

/*
 * \brief Compares two addresses contained into two sockaddr_storage structures.
 *
 * This function is useful to compare two addresses, given their internal representation,
 * i.e. an sockaddr_storage structure.
 *
 * The two structures do not need to be sockaddr_storage; you can have both 'sockaddr_in' and
 * sockaddr_in6, properly acsted in order to be compliant to the function interface.
 *
 * This function will return '0' if the two addresses matches, '-1' if not.
 *
 * \param first: a sockaddr_storage structure, (for example the one that is returned by an
 * accept() call), containing the first address to compare.
 *
 * \param second: a sockaddr_storage structure containing the second address to compare.
 *
 * \return '0' if the addresses are equal, '-1' if they are different.
 */
int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second)
{
	if (first->ss_family == second->ss_family)
	{
		if (first->ss_family == AF_INET)
		{
			if (memcmp(&(((struct sockaddr_in *) first)->sin_addr),
				&(((struct sockaddr_in *) second)->sin_addr),
				sizeof(struct in_addr)) == 0)
				return 0;
		}
		else /* address family is AF_INET6 */
		{
			if (memcmp(&(((struct sockaddr_in6 *) first)->sin6_addr),
				&(((struct sockaddr_in6 *) second)->sin6_addr),
				sizeof(struct in6_addr)) == 0)
				return 0;
		}
	}

	return -1;
}

/*
 * \brief It gets the address/port the system picked for this socket (on connected sockets).
 *
 * It is used to return the address and port the server picked for our socket on the local machine.
 * It works only on:
 * - connected sockets
 * - server sockets
 *
 * On unconnected client sockets it does not work because the system dynamically chooses a port
 * only when the socket calls a send() call.
 *
 * \param sock: the connected socket currently opened.
 *
 * \param address: it contains the address that will be returned by the function. This buffer
 * must be properly allocated by the user. The address can be either literal or numeric depending
 * on the value of 'Flags'.
 *
 * \param addrlen: the length of the 'address' buffer.
 *
 * \param port: it contains the port that will be returned by the function. This buffer
 * must be properly allocated by the user.
 *
 * \param portlen: the length of the 'port' buffer.
 *
 * \param flags: a set of flags (the ones defined into the getnameinfo() standard socket function)
 * that determine if the resulting address must be in numeric / literal form, and so on.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return It returns '-1' if this function succeeds, '0' otherwise.
 * The address and port corresponding are returned back in the buffers 'address' and 'port'.
 * In any case, the returned strings are '0' terminated.
 *
 * \warning If the socket is using a connectionless protocol, the address may not be available
 * until I/O occurs on the socket.
 */
int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen)
{
	struct sockaddr_storage mysockaddr;
	socklen_t sockaddrlen;

	sockaddrlen = sizeof(struct sockaddr_storage);

	if (getsockname(sock, (struct sockaddr *) &mysockaddr, &sockaddrlen) == -1)
	{
		sock_geterror("getsockname(): ", errbuf, errbuflen);
		return 0;
	}
	else
	{
		/* Returns the numeric address of the host that triggered the error */
		return sock_getascii_addrport(&mysockaddr, address, addrlen, port, portlen, flags, errbuf, errbuflen);
	}

	return 0;
}

/*
 * \brief It retrieves two strings containing the address and the port of a given 'sockaddr' variable.
 *
 * This function is basically an extended version of the inet_ntop(), which does not exist in
 * Winsock because the same result can be obtained by using the getnameinfo().
 * However, differently from inet_ntop(), this function is able to return also literal names
 * (e.g. 'localhost') dependently from the 'Flags' parameter.
 *
 * The function accepts a sockaddr_storage variable (which can be returned by several functions
 * like bind(), connect(), accept(), and more) and it transforms its content into a 'human'
 * form. So, for instance, it is able to translate an hex address (stored in binary form) into
 * a standard IPv6 address like "::1".
 *
 * The behavior of this function depends on the parameters we have in the 'Flags' variable, which
 * are the ones allowed in the standard getnameinfo() socket function.
 *
 * \param sockaddr: a 'sockaddr_in' or 'sockaddr_in6' structure containing the address that
 * need to be translated from network form into the presentation form. This structure must be
 * zero-ed prior using it, and the address family field must be filled with the proper value.
 * The user must cast any 'sockaddr_in' or 'sockaddr_in6' structures to 'sockaddr_storage' before
 * calling this function.
 *
 * \param address: it contains the address that will be returned by the function. This buffer
 * must be properly allocated by the user. The address can be either literal or numeric depending
 * on the value of 'Flags'.
 *
 * \param addrlen: the length of the 'address' buffer.
 *
 * \param port: it contains the port that will be returned by the function. This buffer
 * must be properly allocated by the user.
 *
 * \param portlen: the length of the 'port' buffer.
 *
 * \param flags: a set of flags (the ones defined into the getnameinfo() standard socket function)
 * that determine if the resulting address must be in numeric / literal form, and so on.
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return It returns '-1' if this function succeeds, '0' otherwise.
 * The address and port corresponding to the given SockAddr are returned back in the buffers 'address'
 * and 'port'.
 * In any case, the returned strings are '0' terminated.
 */
int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen)
{
	socklen_t sockaddrlen;
	int retval;					/* Variable that keeps the return value; */

	retval = -1;

#ifdef _WIN32
	if (sockaddr->ss_family == AF_INET)
		sockaddrlen = sizeof(struct sockaddr_in);
	else
		sockaddrlen = sizeof(struct sockaddr_in6);
#else
	sockaddrlen = sizeof(struct sockaddr_storage);
#endif

	if ((flags & NI_NUMERICHOST) == 0)	/* Check that we want literal names */
	{
		if ((sockaddr->ss_family == AF_INET6) &&
			(memcmp(&((struct sockaddr_in6 *) sockaddr)->sin6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sizeof(struct in6_addr)) == 0))
		{
			if (address)
				strlcpy(address, SOCKET_NAME_NULL_DAD, addrlen);
			return retval;
		}
	}

	if (getnameinfo((struct sockaddr *) sockaddr, sockaddrlen, address, addrlen, port, portlen, flags) != 0)
	{
		/* If the user wants to receive an error message */
		if (errbuf)
		{
			sock_geterror("getnameinfo(): ", errbuf, errbuflen);
			errbuf[errbuflen - 1] = 0;
		}

		if (address)
		{
			strlcpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen);
			address[addrlen - 1] = 0;
		}

		if (port)
		{
			strlcpy(port, SOCKET_NO_PORT_AVAILABLE, portlen);
			port[portlen - 1] = 0;
		}

		retval = 0;
	}

	return retval;
}

/*
 * \brief It translates an address from the 'presentation' form into the 'network' form.
 *
 * This function basically replaces inet_pton(), which does not exist in Winsock because
 * the same result can be obtained by using the getaddrinfo().
 * An additional advantage is that 'Address' can be both a numeric address (e.g. '127.0.0.1',
 * like in inet_pton() ) and a literal name (e.g. 'localhost').
 *
 * This function does the reverse job of sock_getascii_addrport().
 *
 * \param address: a zero-terminated string which contains the name you have to
 * translate. The name can be either literal (e.g. 'localhost') or numeric (e.g. '::1').
 *
 * \param sockaddr: a user-allocated sockaddr_storage structure which will contains the
 * 'network' form of the requested address.
 *
 * \param addr_family: a constant which can assume the following values:
 * - 'AF_INET' if we want to ping an IPv4 host
 * - 'AF_INET6' if we want to ping an IPv6 host
 * - 'AF_UNSPEC' if we do not have preferences about the protocol used to ping the host
 *
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
 * error message. This buffer has to be at least 'errbuflen' in length.
 * It can be NULL; in this case the error cannot be printed.
 *
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
 *
 * \return '-1' if the translation succeeded, '-2' if there was some non critical error, '0'
 * otherwise. In case it fails, the content of the SockAddr variable remains unchanged.
 * A 'non critical error' can occur in case the 'Address' is a literal name, which can be mapped
 * to several network addresses (e.g. 'foo.bar.com' => '10.2.2.2' and '10.2.2.3'). In this case
 * the content of the SockAddr parameter will be the address corresponding to the first mapping.
 *
 * \warning The sockaddr_storage structure MUST be allocated by the user.
 */
int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, int addr_family, char *errbuf, int errbuflen)
{
	int retval;
	struct addrinfo *addrinfo;
	struct addrinfo hints;

	memset(&hints, 0, sizeof(hints));

	hints.ai_family = addr_family;

	if ((retval = sock_initaddress(address, "22222" /* fake port */, &hints, &addrinfo, errbuf, errbuflen)) == -1)
		return 0;

	if (addrinfo->ai_family == PF_INET)
		memcpy(sockaddr, addrinfo->ai_addr, sizeof(struct sockaddr_in));
	else
		memcpy(sockaddr, addrinfo->ai_addr, sizeof(struct sockaddr_in6));

	if (addrinfo->ai_next != NULL)
	{
		freeaddrinfo(addrinfo);

		if (errbuf)
			pcap_snprintf(errbuf, errbuflen, "More than one socket requested; using the first one returned");
		return -2;
	}

	freeaddrinfo(addrinfo);
	return -1;
}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
null

#### Original name
Corethromyces jacobinus Thaxt.

### Remarks
null


text:
var san = require('../../../dist/san.ssr');
var Label = san.defineComponent({
    template: '{{text}}',

    initData: function () {
        return {
            title: 'title',
            text: 'text'
        };
    }
});

var MyComponent = san.defineComponent({
    components: {
        'ui-label': Label
    },

    template: '

<ui-label text="{{jokeName}}" class="{{labelClass}} my-label"></ui-label>
'
        + '
{{school}}{{company}}

',

    initData: function () {
        return {
            jokeName: 'airike',
            school: 'none',
        };
    }
});

exports = module.exports = MyComponent;


text:
rootpath="src/app/todomvc/examples"
find src/app/todomvc/examples/ -maxdepth 2 -name index.html -print0 | while IFS= read -r -d $'\0' line; do
    target=${line#${rootpath}/}
    target2=${line#${rootpath}/}
    sed -e 's@FILE_URL@'${target2}'@g' -e "s@FRAMEWORK@${target%/*}@g" tests/generation/template-clean.js > tests/clean_${target%/*}.js
done

nbTest=$(ls -1 tests/clean_*.js | wc -l)
echo $nbTest" test(s) generated"